PHP SPL standard library data structure heap (SPLHEAP) Simple use instance
This article mainly introduces the PHP SPL standard library data structure heap (SPLHEAP) Simple Use example, this article also explains the maximum heap (splmaxheap), the minimum Heap (splminheap) of the relevant knowledge, the need for friends can refer to the following
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 the root node is called the largest heap or large heap, and the smallest heap of root nodes is called the minimum heap or small Gan. Binary heaps are also commonly used for sorting (heap sorting).
As follows: Minimum heap (the priority of any node is not less than its child nodes)
Look at the implementation of PHP Splheap:
Obviously it is an abstract class, and the maximum heap (splmaxheap) and the minimum heap (splminheap) are inherited by it. Maximum heap and minimum heap no additional methods
The simple use of Splheap is as follows:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
Class Mysimpleheap extends Splheap { The Compare () method is used to compare the size of two elements, absolutely their position in the heap Public Function 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; } |
http://www.bkjia.com/PHPjc/1000101.html www.bkjia.com true http://www.bkjia.com/PHPjc/1000101.html techarticle PHP SPL standard library data structure heap (SPLHEAP) Simple use examples This article mainly introduces the PHP SPL standard library data structure heap (SPLHEAP) Simple Use example, this article also explains the maximum heap ...