PHP SplHeap __construct() Function
Last Updated :
07 Mar, 2024
The SplHeap::__construct() function is an inbuilt function in PHP that is used to create the heap data structure.
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:
public SplHeap::__construct()
Parameters: This function does not accept any parameter.
Return Value: This function does not return any value.
Below programs illustrate the SplMaxHeap::__construct() function in PHP:
Example 1:
PHP
<?php
$heap = new SplMinHeap();
$heap ->insert( 'System' );
$heap ->insert( 'GFG' );
$heap ->insert( 'ALGO' );
$heap ->insert( 'C' );
$heap ->insert( 'Geeks' );
$heap ->insert( 'GeeksforGeeks' );
for ( $heap ->top(); $heap ->valid(); $heap ->next()) {
echo $heap ->current() . "\n" ;
}
?>
|
Output:
ALGO
C
GFG
Geeks
GeeksforGeeks
System
Example 2:
PHP
<?php
$heap = new SplMaxHeap();
$heap ->insert( 'System' );
$heap ->insert( 'GFG' );
$heap ->insert( 'ALGO' );
$heap ->insert( 'C' );
$heap ->insert( 'Geeks' );
$heap ->insert( 'GeeksforGeeks' );
for ( $heap ->top(); $heap ->valid(); $heap ->next()) {
echo $heap ->current() . "\n" ;
}
?>
|
Output:
System
GeeksforGeeks
Geeks
GFG
C
ALGO
Share your thoughts in the comments
Please Login to comment...