DAA - Extract Method


Advertisements

Extract method is used to extract the root element of a Heap. Following is the algorithm.

Algorithm: Heap-Extract-Max (numbers[]) 
max = numbers[1] 
numbers[1] = numbers[heapsize] 
heapsize = heapsize – 1 
Max-Heapify (numbers[], 1) 
return max 

Example

Let us consider the same example discussed previously. Now we want to extract an element. This method will return the root element of the heap.

Method

After deletion of the root element, the last element will be moved to the root position.

Root Element

Now, Heapify function will be called. After Heapify, the following heap is generated.

Heapify
Advertisements