Heaps, HeapSort, & Priority Queues

# Heaps, HeapSort, & Priority Queues

Télécharger la présentation

## Heaps, HeapSort, & Priority Queues

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
##### Presentation Transcript

1. Heaps, HeapSort, & Priority Queues Briana B. Morrison Adapted from Alan Eugenio, William J. Collins, & Michael Main

2. Topics • Heaps • Implementation • Insertion • Deletion • Applications • Priority Queue • HeapSort Heaps

3. Heaps A heap is a certain kind of complete binary tree. Heaps

4. Heaps Root A heap is a certain kind of complete binary tree. When a complete binary tree is built, its first node must be the root. Heaps

5. Heaps Complete binary tree. Left child of the root The second node is always the left child of the root. Heaps

6. Heaps Complete binary tree. Right child of the root The third node is always the right child of the root. Heaps

7. Heaps Complete binary tree. The next nodes always fill the next level from left-to-right. Heaps

8. Heaps Complete binary tree. The next nodes always fill the next level from left-to-right. Heaps

9. Heaps Complete binary tree. Heaps

10. Heaps 45 A heap is a certain kind of complete binary tree. 35 23 27 21 22 4 19 Each node in a heap contains a key that can be compared to other nodes' keys. Heaps

11. Heaps 45 A heap is a certain kind of complete binary tree. 35 23 27 21 22 4 19 * - Max heap requires >= Min heap requires <= The "heap property" requires that each node's key is >=* the keys of its children Heaps

12. A heap is a binary tree storing keys at its internal nodes and satisfying the following properties: Heap-Order: for every internal node v other than the root,key(v)key(parent(v)) Complete Binary Tree: let h be the height of the heap for i = 0, … , h - 1, there are 2i nodes of depth i at depth h- 1, the internal nodes are to the left of the external nodes The last node of a heap is the rightmost internal node of depth h- 1 What is a heap? (§7.3.1) 2 5 6 9 7 last node Heaps

13. Heaps

14. Height of a Heap • Theorem: A heap storing nkeys has height O(log n) Proof: (we apply the complete binary tree property) • Let h be the height of a heap storing n keys • Since there are 2i keys at depth i=0, … , h - 2 and at least one key at depth h - 1, we have n1 + 2 + 4 + … + 2h-2 + 1 • Thus, n2h-1 , i.e., hlog n + 1 depth keys 0 1 1 2 h-2 2h-2 h-1 1 Heaps

15. Heaps

16. Heaps

17. Heaps

18. Heaps

19. Adding a Node to a Heap 45 • Put the new node in the next available spot. • Push the new node upward, swapping with its parent until the new node reaches an acceptable location. 35 23 27 21 22 4 19 42 Heaps

20. Adding a Node to a Heap 45 • Put the new node in the next available spot. • Push the new node upward, swapping with its parent until the new node reaches an acceptable location. 35 23 42 21 22 4 19 27 Heaps

21. Adding a Node to a Heap 45 • Put the new node in the next available spot. • Push the new node upward, swapping with its parent until the new node reaches an acceptable location. 42 23 35 21 22 4 19 27 Heaps

22. Adding a Node to a Heap 45 • The parent has a key that is >= new node, or • The node reaches the root. • The process of pushing the new node upward is called trickle up, or reheapificationupward. 42 23 35 21 22 4 19 27 Heaps

23. Method insertItem of the priority queue ADT corresponds to the insertion of a key k to the heap The insertion algorithm consists of three steps Find the insertion node z (the new last node) Store k at z and expand z into an internal node Restore the heap-order property (discussed next) 2 5 6 9 7 Insertion into a Heap (§7.3.2) z insertion node 2 5 6 z 9 7 1 Heaps

24. Upheap • After the insertion of a new key k, the heap-order property may be violated • Algorithm upheap restores the heap-order property by swapping k along an upward path from the insertion node • Upheap terminates when the key k reaches the root or a node whose parent has a key smaller than or equal to k • Since a heap has height O(log n), upheap runs in O(log n) time 2 1 5 1 5 2 z z 9 7 6 9 7 6 Heaps

25. Heaps

26. Heaps

27. Heaps

28. Heaps

29. Heaps

30. Heaps

31. Heaps

32. Heaps

33. Heaps

34. Heaps

35. Deletion From A Heap The Top Item is Always Deleted.Known as a “pop” operation. Heaps

36. Popping from the Heap 45 • Move the last node onto the root. 42 23 35 21 22 4 19 27 Heaps

37. Popping from the Heap 27 • Move the last node onto the root. 42 23 35 21 22 4 19 Heaps

38. Popping from the Heap 27 • Move the last node onto the root. • Push the out-of-place node downward, swapping with its larger child until the new node reaches an acceptable location. 42 23 35 21 22 4 19 Heaps

39. Popping from the Heap 42 • Move the last node onto the root. • Push the out-of-place node downward, swapping with its larger child until the new node reaches an acceptable location. 27 23 35 21 22 4 19 Heaps

40. Popping from the Heap 42 • Move the last node onto the root. • Push the out-of-place node downward, swapping with its larger child until the new node reaches an acceptable location. 35 23 27 21 22 4 19 Heaps

41. Popping from the Heap 42 • The children all have keys <= the out-of-place node, or • The node reaches the leaf. • The process of pushing the new node downward is called trickle down, or reheapificationdownward. 35 23 27 21 22 4 19 Heaps

42. 2 5 6 9 7 Removal from a Heap (§7.3.2) • Method removeMin of the priority queue ADT corresponds to the removal of the root key from the heap • The removal algorithm consists of three steps • Replace the root key with the key of the last node w • Compress w and its children into a leaf • Restore the heap-order property (discussed next) w last node 7 5 6 w 9 Heaps

43. 5 7 6 w 9 Downheap • After replacing the root key with the key k of the last node, the heap-order property may be violated • Algorithm downheap restores the heap-order property by swapping key k along a downward path from the root • Upheap terminates when key k reaches a leaf or a node whose children have keys greater than or equal to k • Since a heap has height O(log n), downheap runs in O(log n) time 7 5 6 w 9 Heaps

44. Heaps

45. Heaps