1 / 32

Heaps

Heaps. Background: Priority Queues. Queues are a First-In, First-Out data structure; Priority Queues are similar, except those of highest priority must be removed first. How should we implement this?. Priority Queue Implementation. Arrays

Download Presentation

Heaps

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Heaps

  2. Background: Priority Queues. • Queues are a First-In, First-Out data structure; • Priority Queues are similar, except those of highest priority must be removed first. • How should we implement this?

  3. Priority Queue Implementation • Arrays • Inserts: O(logn) to find spot; O(n) to move all others down. • Deletes: O(1) to remove; O(n) to move others. • Linked Lists : • Inserts: O(n) to find spot; O(1) to do insert. • Deletes: O(1) to remove.

  4. Priority Queue Implementation • AVL-Trees • Inserts: O(logn) to find spot; O(logn) to do rotation(s). • Deletes: O(logn) to remove; O(logn) to do rotations. • Heaps : • Inserts: O(1) to find spot; O(logn) to do insert. • Deletes: O(1) to find; O(logn) to delete/rotate.

  5. Heaps Vs. AVL-Trees • AVL-Trees have a lot of overhead to balance. • AVL-Trees store the values in order. • Heaps only provide access to the least/greatest. • This requires less overhead and is easier to implement.

  6. What is a Heap? • BINARY MIN HEAP is a complete binary tree in which each node is empty or has a key value that is less than both of its children. Both children are Binary Min Heaps • A COMPLETE BINARY TREE is a binary tree where the root is either empty OR TL is a perfect tree of height h-1 and TR is a complete tree of height h-1 OR TL is a complete tree of height h-1 and TR is a perfecttree of height h-2

  7. More Definitions • A PERFECT TREE is one where the root is either empty or has HL – HR = 0 and both sub trees are also perfect trees. • i.e. the last level (and all levels) are completely filled.

  8. B C D E F G Heap as a Tree • As a tree, inserts would fill a level entirely, left to right before going on to the next level: A

  9. Heap Insert Algorithm • The previous slide discussed where the new node would go, but how to maintain the order of the heap? • Algorithm: • Compare new node to parent; if the new node is greater than the parent, stop. • Else, swap new node and parent. Repeat algorithm at parent (percolate up).

  10. Heap Example • Now let’s do an extended example, inserting a series of priority values into a heap. • We will be inserting 90, 50, 25, 10, 15, 20, 75. • Let’s start with 90:

  11. Heap Example 90 • 90 is no problem. • Now, insert 50: Left to insert: 50, 25, 10, 15, 20, 75

  12. Heap Example 90 • Does this require a swap? • Yes: 50 Left to insert: 25, 10, 15, 20, 75

  13. Heap Example 50 • Done. Now insert 25: 90 Left to insert: 25, 10, 15, 20, 75

  14. Heap Example 50 • Swap? • Yes: 90 25 Left to insert:10, 15, 20, 75

  15. Heap Example 25 • Now insert 10: 90 50 Left to insert:10, 15, 20, 75

  16. Heap Example 25 • Swap? • Yes: 90 50 10 Left to insert:15, 20, 75

  17. Heap Example 25 • Swap again? • Yes: 10 50 90 Left to insert:15, 20, 75

  18. Heap Example 10 • Done. Now insert 15: 25 50 90 Left to insert:15, 20, 75

  19. Heap Example 10 • Swap? • Yes: 25 50 90 15 Left to insert:20, 75

  20. Heap Example 10 • Swap again? • No. 15 is where it needs to be. Now 20: 15 50 90 25 Left to insert:20, 75

  21. Heap Example 10 • Swap? • Yes: 15 50 90 25 20 Left to insert:75

  22. Heap Example 10 • Swap again? • No. 20 is good. Finally, insert 75: 15 20 90 25 50 Left to insert:75

  23. Heap Example 10 • Swap? • No. 75 is good; we are done. 15 20 90 25 50 75 Left to insert:done

  24. Deleting from a Heap • The minimum value is at the root of the tree; thus, it can be found easily, but how do we rebalance? • Algorithm: • Remove Root’s value as minimum value • Replace root with last node; delete last. • If root is less than both its children, stop. • Else, swap root and min child; repeat from min child node (percolate down).

  25. Heap Delete Example 10 • First, 10 must be removed and replaced with 75: 15 20 90 25 50 75

  26. Heap Delete Example 75 • Is 75 less than both of its children? • No. Swap with minimum (15): 15 20 90 25 50

  27. Heap Delete Example 15 • Is 75 less than both of its children? • No. Swap with minimum (25): 75 20 90 25 50

  28. Heap Delete Example 15 • 75 is at the terminal level; done. 25 20 90 75 50

  29. Implementation Problem • The problem with the tree/pointer representation is how to maintain the “next” pointer. • I think this can be done by keeping a counter and manipulating it to decide left or right from the root. • But there is another way:

  30. Array Implementation • Any binary tree may be represented in an array. • The only questions really are • Where are the two children of a node? • Where is a node’s parent? • Answer (for a node at position i): • The two children are at 2*i and 2*i+1 • The parent is at i div 2.

  31. Array Example: • For this tree: • Here’s the array: 10 15 20 90 25 50 75 1 2 3 4 5 6 7 10 15 20 90 25 50 75

  32. The End Slide

More Related