recategorized by
1,623 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

438
views
1 answers
2 votes
Thor-o-s asked Sep 1, 2022
438 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...
775
views
1 answers
0 votes
atulcse asked Jan 16, 2022
775 views
Consider the following graph:Find the total number of max-heap possible orderings with elements 12, 10, 1, 5, 7, 9, 8 such that each element is filled in one node of the ...
2.8k
views
1 answers
1 votes
sripo asked Nov 15, 2018
2,830 views
This question is in CLRS,if we have a max heap it is always in sorted order(descending) order.And by extension if we have min heap the array is sorted in ascending order....
1.1k
views
0 answers
1 votes
Balaji Jegan asked Jun 4, 2018
1,125 views
How many max-heaps can be formed with the following elements?$\{1,1,1,2,2,2,3,3,3,4,4,4\}$