HomeLanguagesPHP SplHeap rewind() Function

PHP SplHeap rewind() Function

The SplHeap::rewind() function is an inbuilt function in PHP that is used to rewind the iterator to the beginning.

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:

void SplHeap::rewind()

Parameters: This function does not accept any parameter.

Return Value: This function does not return any value.

Below programs illustrate the SplHeap::rewind() function in PHP.

Example 1:

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'); 
  
// Use rewind() function 
$heap->rewind(); 
    
// Display the current element
var_dump($heap->current()); 
    
?>


Output:

string(6) "System"

Example 2:

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'); 
  
// Use rewind() function 
$heap->rewind(); 
    
// Display the current element
var_dump($heap); 
  
?>


Output:

object(SplMinHeap)#1 (3) {
  ["flags":"SplHeap":private]=>
  int(0)
  ["isCorrupted":"SplHeap":private]=>
  bool(false)
  ["heap":"SplHeap":private]=>
  array(6) {
    [0]=>
    string(4) "ALGO"
    [1]=>
    string(1) "C"
    [2]=>
    string(3) "GFG"
    [3]=>
    string(6) "System"
    [4]=>
    string(5) "Geeks"
    [5]=>
    string(13) "neveropen"
  }
}

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

RELATED ARTICLES

Most Popular

Dominic
32688 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
7057 POSTS0 COMMENTS
Nicole Veronica
12198 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12293 POSTS0 COMMENTS
Shaida Kate Naidoo
7182 POSTS0 COMMENTS
Ted Musemwa
7447 POSTS0 COMMENTS
Thapelo Manthata
7160 POSTS0 COMMENTS
Umr Jansen
7150 POSTS0 COMMENTS