Computer Science 2 Heaps 1 Learning Objectives Understand
Computer Science 2 • Heaps 1
Learning Objectives • Understand the properties of a heap • Understand how to add to a heap • Understand how to remove from a heap 2
Uses for Heaps • Priority Queue. Efficient way of having the first element on the top of the heap. • Heapsort: O(N log N) • Better worst case scenario than using a Tree to sort 3
Properties of a Heap • Full Tree • Parents are greater than their children. 4
Heaps: Examples wrong! 48 30 10 20 21 80 10 14 24 33 10 7 3 50 30 17 30 25 10 wrong! Are heaps BSTs ? ? 30 40 22 35 28 18 9 11 5
Inserting a node • Increase the size by 1 • Add it to the bottom of the heap • Reheap up. – While the child is greater than its parents • Switch them 6
Heaps: Insertion example 16 5 3 11 8 16 8 3 11 • Adding a new node with key = 8 at the “end” of this heap violates the order property. • New node has larger key than its parent, swap nodes to fix this. Is the order property restored? • Is 3 < 8? Yes. • Is 8 < 16? Yes. Is this a coincidence? 5 7
Heaps: Insertion example If the new node had key = 18, what would have happened? 16 5 11 18 3 Swap with the parent. 16 18 3 11 5 Is the order property restored? • Is 3 < 18? Yes. • Is 18 < 16? No. 8
Heaps: Insertion example Swap again: order property restored. 18 16 3 11 5 In general, we “bubble up” the new node until the order property is restored. 9
Your Turn • Create a heap by adding the following values • 20, 10, 30, 15 • Add the following to the heap • 40, 18, 32 10
Removing from a Heap • • Take off the top Move the bottom to the top Decrease the size of the heap Reheap Down – If the top < greatest child – Switch top and greatest child – Continue until the parent is not < it’s greatest child. 11
Heaps: Removing largest key The largest key is at the top. • Remove it. • Move “last” node to the top, to maintain shape. • ‘Re-heap down’ until the order property is restored. 18 16 3 11 5 12
Heaps: Removing largest key 5 16 16 11 3 5 11 3 Did we have to reheap down the left? Why? 13
Removal: another example c y p f d a p r b k f d c r k a r r p d a b p c f k b d k f c b a 14
Your Turn • Using the heap created previously • Show what it looks like after removing one item. 15
Implementing it into an array: Adding to a heap Increase the size by 1 Add it to the bottom of the heap Reheap up. While the child is greater than its parent Switch them Dry run. Add a z and a q to the heap. How do you find the parents of a child? … Child. Position / 2 Size 8 r p k f d c b 9 a 1 2 3 4 5 6 7 8 R P K D F C B A 10 16
Implementing it into an array: Removing How do you find the children? … parent. Position*2 and parent. Position*2 +1 Size 8 Removing Take off the top Move the bottom value to the top Decrease the size of the heap Reheap Down While there are children and added < greatest child Switch added and greatest child r p k d f 9 b c a 1 2 3 4 5 6 7 8 R P K D F C B A 10 17
Heaps of Fun Program • Menu – Add – Show All – Show and remove the top of the heap. • Push: – Implement a heap sort • Switch top and bottom • Rehead down, excluding the last element(s) 18
- Slides: 18