Php spl standard library-Data Structure heap (SplHeap) simple use instance, SplHeap
Heap is a data structure designed to achieve priority queues. It is implemented by constructing a binary Heap (a binary tree. The largest heap of the root node is called the largest heap or a large root heap. The smallest heap of the root node is called the smallest heap or a small root heap. Binary heap is also commonly used for sorting (heap sorting ).
The minimum heap is as follows (the priority of any node is not smaller than its subnode)
Let's take a look at the PHP SplHeap implementation:
Obviously, it is an abstract class. The maximum heap and the minimum heap inherit from it. There is no additional method for the max heap and Min heap.
The simple usage of SplHeap is as follows:
Class MySimpleHeap extends SplHeap {// compare () method is used to compare the size of two elements. It is absolutely their position in the heap public function compare ($ value1, $ value2) {return ($ value1-$ value2) ;}$ 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 ;}