Open In App

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:



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 
  
// 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('GeeksforGeeks'); 
  
echo $heap->extract();
  
?>

Output:

ALGO

Example 2:




<?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('GeeksforGeeks'); 
  
echo $heap->extract();
  
?>

Output:

System

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


Article Tags :