PHPSPL standard library-data structure heap (SplHeap) simple use of instance _ PHP Tutorial

Source: Internet
Author: User
Tags spl
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...

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.