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

Source: Internet
Author: User
Tags spl

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 ...

  • 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.