The data structure heap (SplHeap) of the PHPSPL standard library is simple to use. Simple use of data structure heap (SplHeap) in PHPSPL standard library this article mainly introduces simple use of data structure heap (SplHeap) in PHPSPL standard library, this article also explains the data structure heap (SplHeap) simple use example of the max heap php spl standard library.
This article mainly introduces the simple use of the data structure heap (SplHeap) in the php spl standard library. It also describes the knowledge of the maximum heap (SplMaxHeap) and the minimum heap (SplMinHeap, for more information, see
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:
?
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 the two elements, which is definitely 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; } |
Http://www.bkjia.com/PHPjc/1000101.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1000101.htmlTechArticlePHP 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 instance, this article also explains the maximum heap...