The SplHeap::valid() function is an inbuilt function in PHP which is used to check whether the heap contains more nodes.
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:
bool SplHeap::valid()
Parameters: This function does not accept any parameters.
Return Value: This function returns true if the heap contains any more nodes, false otherwise.Â
Below programs illustrate the SplHeap::valid() 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');   // Loop to display the current element of heap for ($heap->top(); $heap->valid(); $heap->next()) {     echo $heap->current() . "\n"; }   ?> |
ALGO C GFG Geeks neveropen System
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');   // Loop to display the current element of heap for ($heap->top(); $heap->valid(); $heap->next()) {     echo $heap->current() . "\n"; }   ?> |
System neveropen Geeks GFG C ALGO
