Professional Documents
Culture Documents
Lecs 14
Lecs 14
Processor
Heap property
violated 11
19 13
18 21 19 17
43 23 26 29 31
Examples of Non-Heaps
17 13
18 21 19 17
43 26 29 31
Finding the Minimum Element
• The element with the smallest Minimum
priority always sits at the root of the element
heap.
• This is because if it were elsewhere, 11
it would have a parent with larger
priority, which would violate the heap
17 13
property.
• Hence, minimum() can be done in
O(1) time. 18 21 19 17
43 23 26 29 31
Height of a Heap
• Suppose a heap of n nodes has
height h.
• Recall: perfect binary tree of height 11
h has 2h+1 − 1 nodes.
• Hence, 2h-1 < n ≤ 2h+1 − 1. 17 13
• h = log2 n
18 21 19 17
43 23 26 29 31
Implementing Heaps
• Right (i) 1 2 3 4 5 6 7 8 9 10
return 2i+1
A 11 17 13 18 21 19 17 43 23 26
Level: 0 1 2 3
Heap property: A[i.parent] <= A[i]
Implementing Heaps
• Notice the implicit tree
links; children of node i 11
are 2i and 2i + 1
17 13
• Why is this useful?
18 21 19 17
• In a binary
representation, a
43 23 26
multiplication/division
by two is left/right shift.
1 2 3 4 5 6 7 8 9 10
• Adding 1 can be done
by adding the lowest A 11 17 13 18 21 19 17 43 23 26
bit. Level: 0 1 2 3
Insertion in a Heap
• Insert 12 • Only the nodes in the path from the
root to the newly inserted node are
• Insert 8
affected in the insertion process.
11 • The process of shifting the nodes is
required to restore the heap
17 13 property if it gets disturbed.
18 21 19 17
43 23 26 29 31 12 8
Delete-Min in a Heap
• The minimum element is the one at the top of the heap.
• Can be deleted and one of its children can be moved up to fill the space.
• Empty location moves down the tree.
• Might end up at any position on last level.
• Resulting tree would not be left filled.
Delete-Min in a Heap
10 11
18 21 13 12
43 23 26 29 31 19 17
Moving down the space created at the root would not necessarily create a heap
Delete-Min in a Heap
10 11
16 21 13 12
43 23 26 29 31 19 17
16 21 13 12
43 23 26 29 31 19
Running Time Analysis
• In Heapify, the element might be moved all the way to the last level.
• Hence Heapify requires O(log n) time.
• Delete-Min
• Replaces root node with last element- O(1) time
• Employs Heapify(1) - O(log n) time complexity
Running Time Analysis:
• A heap of n nodes has height O(log n).
• While inserting you might have to move the element all the way to the top.
• Hence at most O(log n) steps required.
Building a Heap
• Given an array A of elements
• Start heapifying the elements of the 23
array from the bottom and move up
• All leaves are heaps to begin with
43 26
• Start heapifying upper levels of the
tree
10 21 13 31
BUILD-HEAP (A)
1 for i ← [n/2] downto 1
2 do HEAPIFY (A, i) 16 12 8 29 11 19 17
Building a Heap: Analysis
• Running time: n calls to Heapify = n O(lg n) = O(n lg n)
• Provide a better O(n) bound
• Intuition: for most of the time, Heapify works on smaller than n element heaps
• Proof: Exercise
Insert and Delete-Min (Complexities)
• Insert:
• O(log n) to insert down the heap + O(log n) to restore the heap property =
O(log n)
• Heapify:
• O(log n) (restores heap property)
• Delete-Min:
• O(1) to remove the top most element + O(log n) to restore the heap
property = O(log n)
• Find-Min:
• O(1)
Heap Sort
• Create a heap
• Do delete-min repeatedly till heap becomes empty
• The order in which the elements come out is the sorted order
• Store the elements in a separate array
• Time complexity: O(n log n)
• Extra space required: O(n)
• Can sort “in-place” as well with only O(1) extra space
Thank You!