A heap (HEAP) is a data structure designed to implement a priority queue, which is implemented by constructing a two-fork heap (a binary tree). The largest heap of root nodes is called the maximum heap or the large root heap, and the smallest heap of the root node is called the minimum heap or the small Gan. Binary heaps are also often used for sorting (heap sorting).
As follows: Minimum heap (the priority of any node is not less than its child node)
Look at the implementation of PHP Splheap:
Obviously it is an abstract class, and the maximum heap (splmaxheap) and the smallest heap (splminheap) are inherited by it. Maximum heap and minimum heap with no additional method
The simple use of Splheap is as follows:
The class Mysimpleheap extends Splheap
{
//compare () method is used to compare the size of two elements, absolute their position in the heap public
function compare ($ Value1, $value 2) {return
($value 1-$value 2);
}
}
$obj = new Mysimpleheap ();
$obj->insert (4);
$obj->insert (8);
$obj->insert (1);
$obj->insert (0);
echo $obj->top (); 8
Echo $obj->count ();//4
foreach ($obj as $number) {
echo $number;
}