Professional Documents
Culture Documents
1
Heap Properties
• Completeness:
Complete tree: N = 2H − 1 (last level is full)
B C B C B C
D D E D E F
2
Heap Properties
• Key value order:
12 24 44
12 8 18 23
6 10
3
Insert Heap
• Insert the new node into the bottom
level, as far left as possible
12 24 44
12 8 18 23
6 10
4
Basic Heap Operations
42 42
21 32 21 32
16 12 20 30 16 25 20 30
13 15 10 25 13 15 10 12
42
Reheapup 25 32
16 21 20 30
13 15 10 12
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 10
21 32 21 32
16 12 20 30 16 12 20 30
13 15 10 13 15
16 12 20 30 16 12 20 30
13 15 13 15
swap with the larger of
32 its children
Reheapdow 21 30
n
16 12 20 10
13 15
7
Heap Data Structure
8
ReheapUp Algorithm
Algorithm reheapUp (ref heap <array>, val newNode
<index>)
Reestablishes heap by moving data in child up to its correct
location
Pre heap is an array containing an invalid heap
newNode is index location to new data in heap
Post 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
9
End reheapUp
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
Post Heap has been restored
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)
2 return
End reheapDown 11
Build Heap
Algorithm buildHeap (ref heap <array>, val size <integer>)
Given an array, rearrange data so that it forms a heap
Pre heap is an array containing an invalid heap
size is number of elements in array
Post array is now a heap
1 walker = 1 8 19 23 32 45 56 78
2 loop (walker <= size)
1 reheapUp (heap, walker)
2 walker = walker + 1
3 return 78 56 32 45 8 23 19
End buildHeap [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
Complexity of Heap
Operations
• ReheapUp: O(log2n)
• 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