Thursday, October 16, 2025
HomeLanguagesPHP SplHeap extract() Function

PHP SplHeap extract() Function

The SplHeap::extract() function is an inbuilt function in PHP which is used to extract a node from top of the heap and sift up.

Generally, the Heap Data Structure are of two types:

  • Max-Heap: In a Max-Heap, the key present at the root node must be greatest among the keys present at all of its children. The same property must be recursively true for all sub-trees in that Binary Tree.
  • Min-Heap: In a Min-Heap, the key present at the root node must be minimum among the keys present at all of its children. The same property must be recursively true for all sub-trees in that Binary Tree.

Syntax:

mixed SplHeap::extract()

Parameters: This function does not accept any parameter.

Return Value: This function returns the value of the extracted node.

Below programs illustrate the SplHeap::extract() function in PHP:

Example 1:

PHP




<?php 
  
// Create a new empty Min Heap 
$heap = new SplMinHeap(); 
  
$heap->insert('System'); 
$heap->insert('GFG'); 
$heap->insert('ALGO'); 
$heap->insert('C');
$heap->insert('Geeks'); 
$heap->insert('neveropen'); 
  
echo $heap->extract();
  
?>


Output:

ALGO

Example 2:

PHP




<?php 
  
// Create a new empty Max Heap 
$heap = new SplMaxHeap(); 
  
$heap->insert('System'); 
$heap->insert('GFG'); 
$heap->insert('ALGO'); 
$heap->insert('C');
$heap->insert('Geeks'); 
$heap->insert('neveropen'); 
  
echo $heap->extract();
  
?>


Output:

System

Reference: https://www.php.net/manual/en/splheap.extract.php

RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS