This article mainly introduced the PHP SPL standard database data structure heap (SPLHEAP) Simple Use example, this article also explained the maximum heap (splmaxheap), the minimum heap (splminheap) The correlation knowledge, needs the friend may refer to under
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:
?
1 2 3 4 5 6 7 8 9 A |
class Mysimpleheap extends Splheap {//compare () method is used to compare the size of two elements, and absolute their position in the heap is public fun ction Compare ($value 1, $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;} |