recategorized by
1,581 views
1 votes
1 votes

In the max heap Increase key procedure

IncreaseKey(int pos, int newValue)
{
   heap[pos] = newValue;
   while(left(pos) < heap.Length)
   {
      int smallest = left(pos);
      if(heap[right(pos)] < heap[left(pos)])
         smallest = right(pos);
      if(heap[pos] < heap[smallest])
      { 
         swap(smallest, pos);
         pos= smallest;
      }
      else return;
   }   
}

When the Max heap property is violated at a node x, we dont call MAX-HEAPIFY procedure to mend the Max-heap property, What is the reason behind it?

recategorized by

1 Answer

0 votes
0 votes
That work is already done is while loop.

We are comparing the increased key value with its parent. If it is greater than parent we swap them. This continues till we find a parent is greater than child compared.

Take an example and trace out the steps, it will help in understanding.

Related questions

2 votes
2 votes
1 answer
1
Thor-o-s asked Sep 1, 2022
423 views
Can anyone please explain how to find “ i “ smallest elements from an array whose elements are distinctPlease use max heap to explain the working input : n distinct e...
0 votes
0 votes
1 answer
2
1 votes
1 votes
0 answers
4
Balaji Jegan asked Jun 4, 2018
1,104 views
How many max-heaps can be formed with the following elements?$\{1,1,1,2,2,2,3,3,3,4,4,4\}$