1 / 14

CS 261

CS 261. Binary Heaps. A Different Type of Heap. Start with the same heap order property, but build it out of binomial trees Use a collection of heaps Merge heaps of the same size. Binomial Tree. A recursive data structure

tanika
Download Presentation

CS 261

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. CS 261 Binary Heaps

  2. A Different Type of Heap • Start with the same heap order property, but build it out of binomial trees • Use a collection of heaps • Merge heaps of the same size

  3. Binomial Tree • A recursive data structure • A node has binomial trees of lower order as children, from order k down to order 0

  4. A picture!

  5. Overcoming Limitations of the Tree • Can only keep things which are a sum of a power of 2 • But if we keep a list of trees we can get any number of nodes • Like binary numbers

  6. What it looks like

  7. Our Powers Combined! • Merging is a key operation • Many other operations will call merge as a subroutine • Takes two heaps and merges them

  8. Merging Trees • If we have two trees of the same size how do we merge them? • Just make the one with the bigger key the leftmost child of the root of the other

  9. Merging heaps • Look at each tree in the list from smallest to biggest • If only one heap has a tree of order n, add it to the merged heap • Otherwise merge the two trees into a tree of order n+1 and merge that into the new heap

  10. Moar Pictures! (Merging)

  11. Adding To a Heap • How do we add to a binomial heap? • Can we just use merge somehow?

  12. Finding the Minimum • Is it still O(1)? • If it is not, can we be tricky and make it so?

  13. Removing the minimum • The minimum will always be a root • Remove that root, then merge the resulting heap with the old heap

  14. It’s All log(n) • Or maybe find minimum is O(1) if we are smart about it

More Related