859 views

Uploaded by mrbkiter

save

- C Interviews Questions
- btree
- Gate Question Papers Download Computer Sciences 2004
- Data Structures
- c19-binomial
- Exercise 3 Tree
- Lecture5p_1pp
- DOC-41041
- Basic Comp 2
- 08-TreeBasics
- ee220s02lec6 (1)
- Karras BVH
- ME Instrument
- How to Create and Configure DME File - Application Server Infrastructure
- Analysis of Algorithm Final
- dm07-webmining
- clob
- DS LABmodel Questions R408
- BTrees
- Paging Load Overview PA1
- Synopsis of bus information system
- Words Tree
- No problem can't solve
- C Lang
- machine-learning-lecture-2-1205567853441106-4.pdf
- twenty question , john T .pdf
- BI Publisher Additional Features (2)
- CT
- Use Decision Trees to Make Important
- COmput Question
- Chiến lược biến thể để trị
- Approximation Algorithms
- 01_Ghichep
- Giải thuật quay lui Giải thuật nhánh-và-cận
- Lex
- FuncProg
- ADT_OOP
- 02_ToChucCongViec
- Vấn đề NP-đầy đủ
- Chapter1 - Overview
- Qui hoạch động
- GrammarParser
- tÌm HiỂu vỀ Analytic Hierachy Process ThÔng
- Control Structures
- Introduction
- definition about analysis and design Algorithm
- Hardware Basic
- Multiway Tree
- divide and conquer
- Introduction
- Hashing
- Graph
- Search Tree
- Linked List
- Sorting
- Binary Tree
- Chiến lược giảm để trị
- Recursion
- Chapter 8

You are on page 1of 17

**• A heap is a binary tree structure such that:
**

– The tree is complete or nearly complete. – The key value of each node is >= the key value of each of its descendents (max-heap).

1

Heap Properties

• Completeness:

Complete tree: N = 2H − 1 (last level is full) Nearly complete: the left

A B D C D B E

H = [log2N] + 1

**All nodes in the last level are on
**

A C D B E F

2

A C

Heap Properties

• Key value order:

12 12 24 8 6 18 10 44 23

3

Insert Heap

• Insert the new node into the bottom level, as far left as possible

12 12 24 8 6 18 10 44 23

**• and then reheap-up
**

4

**Basic Heap Operations
**

42 21 16 13 15 10 12 25 42 20 32 30 13 16 15 10 21 25 12 20 42 32 30

Reheapup

16 13 15

25 21 10 12 20

32 30

5

Delete Heap

• replace the deleted value by the value X of the last leaf (rightmost in the last level) • delete the last leaf

78 21 16 13 15 10 12 20 32 30 13 16 15 21 12 20 10 32 30

**• and then reheap-up, if X is ≥ its parent, or reheapdown, if X is < either of its children
**

6

**Basic Heap Operations
**

10 21 16 13 15 32 12 20

**swap with the larger of its children
**

32 30 13 16 15 21 12

32 10 20 30

swap with the larger of its children

Reheapdow n

16 13 15

21 12 20

30 10

7

**Heap Data Structure
**

78 56 45

[1 ]

[0 ]

8 23

32

**• Node i: left child 2i + 1, right 2i +2 • Node i: parent [(i − 1)/2]
**

19

[2 ]

[3 ]

78 56

[4 ]

32 45

[5 ]

8 23

[6 ]

19

• Left child j: right sibling j + 1 • Right child j: left sibling j − 1 • Heap size n: first leaf [n/2] • First leaf k: last nonleaf k − 1

[0 ]

[1 [2 [3 ] ] ]

[4 ]

[5 [6 ] ]

8

ReheapUp Algorithm

Algorithm reheapUp (ref heap <array>, val newNode <index>) Reestablishes heap by moving data in child up to its correct location Pre Post heap is an array containing an invalid heap newNode is index location to new data in heap Heap has been restored

1 if (newNode not 0) 1 parent = (newNode − 1)/2 2 if (heap[newNode].key > heap[parent].key) 1 swap (newNode, parent) 2 reheapup (heap, parent) 2 return End reheapUp

9

ReheapDown Algorithm

Algorithm reheapDown (ref heap <array>, val root <index>, val last <index>) Reestablishes heap by moving data in root down to its correct location Pre heap is an array containing an invalid heap root is root of heap last is an index to the last element in heap Heap has been restored

Post

10

ReheapDown Algorithm

1 if (root*2 + 1 <= last) 1 leftKey = heap[root*2 + 1].data.key 2 if (root*2 + 2 <= last) 1 rightKey = heap[root*2 + 2].data.key 3 else 1 rightKey = lowKey 4 if (leftKey > rightKey) 1 largeChildKey = leftKey 2 largeChildIndex = root*2 + 1 5 else 1 largeChildKey = rightKey 2 largeChildIndex = root*2 + 2 6 if (heap[root].data.key < heap[largerChildIndex].data.key) 1 swap (root, largerChildIndex) 2 reheapDown (heap, largerChildIndex, last) return reheapDown

11

2

End

Build Heap

Algorithm buildHeap (ref heap <array>, val size <integer>) Given an array, rearrange data so that it forms a heap Pre Post heap is an array containing an invalid heap size is number of elements in array array is now a heap

8 19 23 32 45 56 78

1 walker = 1 2 loop (walker <= size) 1 reheapUp (heap, walker) 2 walker = walker + 1 3 return End buildHeap

78

56

32

45

8

23

19

[0 ]

[1 [2 [3 ] ] ]

[4 ]

[5 [6 ] ]

12

Insert Heap

Algorithm insertHeap (ref heap <array>, ref last <index> val data <data type>) Inserts data into heap Pre heap is a valid heap last is index to last node in heap Post data have been inserted into heap Return true if successful; false if array is full 1 if (heap full) 1 return false 2 last = last + 1 3 heap[last] = data 4 reheapUp (heap, last) 5 return true End insertHeap

13

Delete Heap

Algorithm deleteHeap (ref heap <array>, ref last <index> ref dataOut <data type>) Deletes root of heap and passes data back to caller Pre heap is a valid heap last is index to last node in heap dataOut is reference parameter for output data Post root has been deleted from heap Return true if successful; false if array is empty 1 if (heap empty) 1 return false 2 dataOut = heap[0] 3 heap[0] = heap[last] 4 last = last - 1 5 reheapDown (heap, 0, last) 6 return true End deleteHeap

14

• ReheapUp: O(log2n)

Complexity of Heap Operations

**• ReheapDown: O(log2n) • BuildHeap: O(nlog2n) • InsertHeap: O(log2n) • DeleteHeap: O(log2n)
**

15

Heap Applications

• Selection Algorithms: to determine the kth largest element in an unsorted list

– Creat a heap – Delete k - 1 elements from the heap – The desired element will be at the top

16

Heap Applications

• Priority Queues: each element has a priority to be dequeued

– Use a heap to represent a queue – Elements are enqueued according to their priorities – Top element is dequeued first

17

- C Interviews QuestionsUploaded byKundan Chaudhary
- btreeUploaded byAlexis Mamani
- Gate Question Papers Download Computer Sciences 2004Uploaded bySoumyabrata
- Data StructuresUploaded bySantosh Sardar
- c19-binomialUploaded byapi-238070943
- Exercise 3 TreeUploaded byHải Nam
- Lecture5p_1ppUploaded byMrZaggy
- DOC-41041Uploaded byjustforregister
- Basic Comp 2Uploaded byNilam Kumar
- 08-TreeBasicsUploaded byArihant Khanagond
- ee220s02lec6 (1)Uploaded byanony_1
- Karras BVHUploaded bycmaestrofdez
- ME InstrumentUploaded byFerozMujawar
- How to Create and Configure DME File - Application Server InfrastructureUploaded byIJGV
- Analysis of Algorithm FinalUploaded byJohnny Wang
- dm07-webminingUploaded byLuong Hong Giang
- clobUploaded byAnupam Majumdar
- DS LABmodel Questions R408Uploaded bySaravanan Sar
- BTreesUploaded bycyrene20
- Paging Load Overview PA1Uploaded byMohamed Abdel Monem
- Synopsis of bus information systemUploaded byjay20074u
- Words TreeUploaded byDimaz Kurniawan
- No problem can't solveUploaded byPhúc Minh
- C LangUploaded byvizu504
- machine-learning-lecture-2-1205567853441106-4.pdfUploaded byGayathri
- twenty question , john T .pdfUploaded byJumrotul Mu'arofah
- BI Publisher Additional Features (2)Uploaded bypmarun83
- CTUploaded bykumudha
- Use Decision Trees to Make ImportantUploaded byPhebe Munigeti
- COmput QuestionUploaded bychanpreet

- Chiến lược biến thể để trịUploaded bymrbkiter
- Approximation AlgorithmsUploaded bymrbkiter
- 01_GhichepUploaded bymrbkiter
- Giải thuật quay lui Giải thuật nhánh-và-cậnUploaded bymrbkiter
- LexUploaded bymrbkiter
- FuncProgUploaded bymrbkiter
- ADT_OOPUploaded bymrbkiter
- 02_ToChucCongViecUploaded bymrbkiter
- Vấn đề NP-đầy đủUploaded bymrbkiter
- Chapter1 - OverviewUploaded bymrbkiter
- Qui hoạch độngUploaded bymrbkiter
- GrammarParserUploaded bymrbkiter
- tÌm HiỂu vỀ Analytic Hierachy Process ThÔngUploaded bymrbkiter
- Control StructuresUploaded bymrbkiter
- IntroductionUploaded bymrbkiter
- definition about analysis and design AlgorithmUploaded bymrbkiter
- Hardware BasicUploaded bymrbkiter
- Multiway TreeUploaded bymrbkiter
- divide and conquerUploaded bymrbkiter
- IntroductionUploaded bymrbkiter
- HashingUploaded bymrbkiter
- GraphUploaded bymrbkiter
- Search TreeUploaded bymrbkiter
- Linked ListUploaded bymrbkiter
- SortingUploaded bymrbkiter
- Binary TreeUploaded bymrbkiter
- Chiến lược giảm để trịUploaded bymrbkiter
- RecursionUploaded bymrbkiter
- Chapter 8Uploaded bymrbkiter