PHP | SplHeap current() Function

The SplHeap::current() function is an inbuilt function in PHP which is used to get the current element pointed by the iterator.

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.

Note: This article uses Max Heap which extends the SplHeap class.

Syntax:

mixed SplMaxHeap::current()

Parameters: This function does not accept any parameter.

Return Value: This function returns current node of heap data structure.

Below programs illustrate the SplMaxHeap::current() function in PHP:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Create a new empty Max Heap
$heap = new SplMaxHeap();
  
$heap->insert('System');
$heap->insert('gfg');
$heap->insert('ALGO');
$heap->insert('C');
  
// Move next node
$heap->next();
$heap->next();
  
echo $heap->current() . "\n";
?>

chevron_right


Output:

C

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
   
// Create a new empty Max Heap
$heap = new SplMaxHeap();
   
$heap->insert('GEEKS');
$heap->insert('gfg');
$heap->insert('DSA');
$heap->insert('ALGO');
$heap->insert('C');
  
// Iterate array and print values
while($heap->valid()) {
       
    // Print current value of index of the array
    echo $heap->current(). "\n";
       
    // Move next each time of iteration
    $heap->next();
}
  
?>

chevron_right


Output:

gfg
GEEKS
DSA
C
ALGO

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



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.