Php spl standard library-Data Structure heap (SplHeap) simple use instance, SplHeap

Source: Internet
Author: User
Tags spl

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 ;}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.