21.5 Exercises
Last updated
Last updated
Represent the following heap as an array.
Consider the above heap. What range of values, when inserted, will cause the maximum number of swaps?
Consider an externally-chained hashmap. If there are many hash collisions, what data structure would be most efficient in storing the key-value pairs at each bucket?
Consider an array sorted in descending order. Is this a valid heap? If so, which type: min-heap, or max-heap?
Suppose that we store the root of a heap at the 0
th index in our array. For an arbitrary index k
, compute the indices of its children and parent.
Explain how you would implement a stack using a priority queue.
An LLRB would be the best choice. Even if all items end up in same bucket, the LLRB will be balanced, resulting in a search time where is the number of items in the bucket. Any other choice of data structure (linked list, array, binary search tree) could have linear search time in the word case within the bucket.