Should the top-down and bottom-up methods generate the same heap. Why yes or why not?
Added by Jason F.
Step 1
- Both top-down (insertion-based) and bottom-up (heapify) aim to produce a valid heap (e.g., a max-heap or min-heap) from a given set of elements. Show more…
Show all steps
Your feedback will help us improve your experience
Akash M and 60 other AP CS educators are ready to help you.
Ask a new question
Labs
Want to see this concept in action?
Explore this concept interactively to see how it behaves as you change inputs.
Key Concepts
Recommended Videos
Function SkewedHeapMerge(h, h'): If h is empty, then return h' Else if h' is empty, then return h If the root of h' is smaller than the root of h, then exchange h and h' (* h holds the smallest root *) If right(h) is nil, then right(h) := h' (* last node, we don't swap *) Else right(h) := SkewedHeapMerge(right(h), h') Swap the left and right children of h Return h The above recursive routine can also be done iteratively. In fact, it can be done more efficiently than the leftist heap Merge (by a constant factor), because everything can be done in one pass while moving down the rightmost path. In the case of leftist heaps, we go down the rightmost path and then back up to recompute ranks. In leftist heaps, that also requires either a recursive algorithm or pointers from nodes to their parents. Since there is no balance condition, there's no guarantee that these trees will have O(log n) worst-case performance for the merge (and hence all of the other operations). But they do have good amortized performance. Here's the intuition for the above: In the previous merge algorithm, we only had to swap children when the right one had a larger rank than the left. In this merge algorithm, we always swap children, so we might actually replace a right child of "small" rank with one of "large" rank. But the next time we come down this path, we will correct that error because the right child will be swapped onto the left. (a) Show that a SkewedHeapMerge of two skewed heaps uses amortized time O(log^2n) by the use of the accounting method. (b) Show that Insert and DeleteMin have the same amortized time bounds.
Akash M.
Heaps: a. Draw the binary min heap that results from inserting 11, 9, 12, 14, 3, 15, 7, 8, 1 in that order into an initially empty binary heap. You do not need to show the array representation of the heap. You are required to show the intermediate trees and please circle your final result. b. Draw the binary heap that results from doing 2 deletemins on the heap you created in part a. You are only required to show the final tree, although if you draw intermediate trees, please circle your final result.
Klei Y.
Recommended Textbooks
Computer Science and Information Technology
Introduction to Programming Using Python
Computer Science - An Overview
Transcript
18,000,000+
Students on Numerade
Trusted by students at 8,000+ universities
Watch the video solution with this free unlock.
EMAIL
PASSWORD