PHP SPL standard library data structure heap (SPLHEAP) Simple use instance _php instance

Source: Internet
Author: User
Tags spl

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:

The class Mysimpleheap extends Splheap
{
  //compare () method is used to compare the size of two elements, absolute their position in the heap public
  function compare ($ Value1, $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;
}

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.