English 中文(简体)
Design and Analysis of Algorithms

Selected Reading

DAA - Extract Method
  • 时间:2024-12-22

Design and Analysis Extract Method


Previous Page Next Page  

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