Download presentation
Presentation is loading. Please wait.
1
CS2420: Lecture 20 Vladimir Kulyukin Computer Science Department Utah State University
2
Outline HeapSort (Chapter 7)
3
The Heap Property A Heap is a Complete Binary Tree. All levels are filled except possibly the last one. The Heap Property: –Minimal heap property: for every node X with parent P, the key in P is smaller than or equal to the key in X. –Maximum heap property: for every node X with parent P, the key in P is greater than or equal to the key in X.
4
The Heap Property P X P X The minimum heap property The maximum heap property
5
Packing Heaps into Arrays 16 14 142 93 10 78 1 23 4 567 89 We can convert this heap into a one dimensional array by doing a level-order traversal on it.
6
Packing Heaps into Arrays 16 14 142 93 10 78 1 23 4 567 89 7814101693241 12 3 4 5 6 7 8 9 10 0 Note that the index count of the array starts with 1. We can convert this heap into an array by doing a level-order traversal on it.
7
Packing Heaps into Arrays 16 14 142 93 10 78 1 23 4 567 89 7814101693241 12 3 4 5 6 7 8 9 10 0
8
Interval Nodes and Leaves 16 14 142 93 10 78 1 23 4 567 89 7814101693241 12 3 4 5 6 7 8 9 10 0 Internal NodesLeaves
9
Leaves vs. Non-Leaves
10
Maintaining the Heap Property A heap is a container so items can be inserted and deleted at any time. The problem is that if we insert/delete an item, the heap property may be broken after the insertion/deletion. We need to make sure that the heap property is restored after every insertion and deletion.
11
Maintaining the Property: Max Heap Example 4 82 7 14 4 7281 0 1 2 3 4 5 6 7 1 Heap Property is broken.
12
Maintaining the Property: Example 4 82 7 14 4 7281 0 1 2 3 4 5 6 7 1 We swap 4 with the maximum of 14 and 7.
13
Maintaining the Property: Example 14 82 7 4 47281 0 1 2 3 4 5 6 7 1 We swap 4 with the maximum of 2 and 8.
14
Maintaining the Property: Example 14 42 7 8 87241 0 1 2 3 4 5 6 7 1 The heap property is now restored at every node.
15
RestoreHeap: Maintaining The Heap Property RestoreHeap(A, i) { int Max = 0; int LCH = LeftChild(i); int RCH = RightChild(i); If ( LCH A[i] ) { Max = LCH; } Else { Max = i; } If ( RCH A[MAX] ) { Max = RCH; } If ( Max != i ) { Swap(A[i], A[Max]); RestoreHeap(A, Max); }
16
RestoreHeap: Asymptotic Analysis
18
Building a Heap Given the dimensions of the array A, discard all the leaves. The leaf indices are [N/2 + 1, N]. Start from the Rightmost Inner (Non- Leaf) Node and go up the tree restoring the heap property at every inner node until you reach and restore the heap property at the root node.
19
Building a Heap
20
Building a Max Heap: Example 10 520 11152134 1 2 3 4567 There are 7 nodes in the heap; so the inner node range is [1, 3]. Thus, we start with node 3.
21
Building a Max Heap: Example 10 520 11152134 RestoreHeap(3) 1 23 4 5 67
22
Building a Max Heap: Example 10 534 11152120 1 23 4 5 67 RestoreHeap(2)
23
Building a Max Heap: Example 10 1534 1152120 1 23 4 5 67 RestoreHeap(1)
24
Building a Max Heap: Example 34 1510 1152120 1 2 3 4 5 67 RestoreHeap(3)
25
Building a Max Heap: Example 34 1521 1151020 1 2 3 4 5 67
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.