LT08
LT08
Technology
COMPUTER SCIENCE DEPARTMENT
DSA (Lab)
Lab Task #08
Q1. A min-max heap is a data structure that supports both deleteMin and deleteMax in O(logN) per
operation. The structure is identical to a binary heap, but the heaporder property is that for any node, X,
at even depth, the element stored at X is smaller than the parent but larger than the grandparent (where
this makes sense), and for any node, X, at odd depth, the element stored at X is larger than the parent
but smaller than the grandparent.
The following algorithm is used to insert a new node in to the min-max heap:
Step 1: Create a function to insert the new node in the min-max heap.
Function min_max_insert(element heap[], int *n, element item)
int parent;
*n++;
Else
{
verify_min(heap, *n, item)
}
a. There is an elegant linear-time algorithm for building a max-heap from a collection of values
that is asymptotically faster than just doing n bubble steps.
b. The idea is to build a forest of smaller max-heaps, then continuously merge them together
pairwise until all the elements are joined into a single max-heap.
c. Using a precise analysis, it can be shown that this algorithm runs in O(n) time with a very good
DSA-Lab BSSE-3B SZABIST-ISB
S haheed Zulfikar Ali Bhutto Institute of Science &
constant factor.
Technology
e. Suppose we would like to support deleteMin, deleteMax, and merge.
Merge:
In the merge function the two heaps are added to a new heap tree by tree in a merge sort kind.
We add the lowest order tree of either heap into the new heap, and if both orders are the same then we
merge (O(1)) it and insert (O(Log N)) it to the resulting tree after it has been built recursively.
Since, we will insert trees of lowest order first the merge will always be inserting a tree of equal or less
then order then the first tree in the new heap.