0% found this document useful (0 votes)
109 views5 pages

LT08

The document discusses algorithms for min-max heaps. A min-max heap supports deleteMin and deleteMax operations in O(logN) time by enforcing a heap ordering property. It can find the minimum and maximum elements in O(1) time by checking the root or root children. Insertion works by comparing a new element to its parent and swapping if needed to maintain the ordering property. Deletion removes an element and replaces it with the last element, then sifts it down while checking the ordering property. A min-max heap can be built in linear time using a technique that merges smaller max-heaps. Supporting deleteMin, deleteMax and merge can use a sorted dynamic array and linked list

Uploaded by

Jiya Ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
109 views5 pages

LT08

The document discusses algorithms for min-max heaps. A min-max heap supports deleteMin and deleteMax operations in O(logN) time by enforcing a heap ordering property. It can find the minimum and maximum elements in O(1) time by checking the root or root children. Insertion works by comparing a new element to its parent and swapping if needed to maintain the ordering property. Deletion removes an element and replaces it with the last element, then sifts it down while checking the ordering property. A min-max heap can be built in linear time using a technique that merges smaller max-heaps. Supporting deleteMin, deleteMax and merge can use a sorted dynamic array and linked list

Uploaded by

Jiya Ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

S haheed Zulfikar Ali Bhutto Institute of Science &

Technology
COMPUTER SCIENCE DEPARTMENT

DSA (Lab)
Lab Task #08

Submitted To: Adil Majeed

Student Name: Syed Iqbal Hussain

Section: BS-SE (3B)

Reg. Number: 1980143

DSA-Lab BSSE-3B SZABIST-ISB


S haheed Zulfikar Ali Bhutto Institute of Science &
Technology

DSA-Lab BSSE-3B SZABIST-ISB


S haheed Zulfikar Ali Bhutto Institute of Science &
Technology
COMPUTER SCIENCE DEPARTMENT

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.

a. How do we find the minimum and maximum elements?


The minimum element can be found simply if heap is not empty by seeing the root as it itself is the
minimum element. The maximum can be found by comparing the two child of the two root. The bigger
element between the two child nodes (of the root) is the max element.

b. Give an algorithm to insert a new node into the min-max heap.


Insertion in min-max heap:

To add an element to a Min-Max Heap we will perform the following operations:


Insert the required key into given Min-Max Heap. Compare this key with its parent. If it is found to be
smaller (greater) compared to its parent, then it is surely smaller (greater) than all other keys present at
nodes at max (min) level that are on the path from the present position of key to the root of heap. Now,
just check for nodes on Min (Max) levels. If the key added is in correct order then stop otherwise swap
that key with its parent.

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++;

Step 2: Create an if statement to check the max size of the node


if(*n == Max_size)
{
cout<<” The heap is full”;
exit(1);
}
parent = (*n) / 2;
if(!parent)
heap[1] = item;
Else switch(level(parent))

Step 3: Create an if statement to find the min element in the node.


case FALSE:
If (item.key < heap[parent].key)
{
heap[*n] = heap[parent]
verify_min(heap, parent, item)
}
DSA-Lab BSSE-3B SZABIST-ISB
S haheed Zulfikar Ali Bhutto Institute of Science &
else Technology
{
verify_max(heap, *n, item)
break;
}
case TRUE:

If item.key > heap[parent].key


heap[*n] = heap[parent]
verify_max(heap, parent, item)

Else
{
verify_min(heap, *n, item)
}

c. Give an algorithm to perform deleteMin and deleteMax.


Min Heap Deletion Algorithm:
1. Delete the node that contains the value you want deleted in the heap.
2. Replace the deleted node with the farthest right node.
3. Heapify (Fix the heap):
if the heap property holds true
then you are done.
else if the replacement node value <= its parent nodes value
then swap them, and repeat step 3.
else
swap the replacement node with the smallest child node, and
repeat step 3.

Max Heap Deletion Algorithm:


1. Delete the node that contains the value you want deleted in the heap.
2. Replace the deleted node with the farthest right node.
3. Heapify (Fix the heap):
if the heap property holds true
then you are done.
else if the replacement node value >= its parent nodes value
then swap them, and repeat step 3.
else
swap the replacement node with the largest child node, and
repeat step 3.

d. Can you build a min-max heap in linear time?

Yes, min max heap can build in O(n) time.

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.

Delete Max at O(LogN)


1. Sorted dynamic array, D, for from which you can do a find in O(lg n) time by binary search.
2. Sorted linked list, L, from which you can do a deleteMin in O(1) time and an insert given a
successor (or predecessor) reference in O(1) worst-case time.
3. Sorted list T of elements recently inserted into L, but not yet synced with D.

Delete Min at O(LogN)


1. Copy the last value in the array to the root.
2. Decrease heap’s size by 01.
3. Sift down root’s value.
Sifting is done as followings:
If current node has no child: sifting is over
If current node has one child: Check, if heap property if broken then swap current node value
and child value, sift down the child.
If current node has two child: find the smallest of them. If heap property is broken then swap
current nodes value and selected child value, sift down the value.

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.

DSA-Lab BSSE-3B SZABIST-ISB

You might also like