Home > Backend Development > PHP Tutorial > Simple usage example of data structure heap (SplHeap) of PHP SPL standard library_PHP tutorial

Simple usage example of data structure heap (SplHeap) of PHP SPL standard library_PHP tutorial

WBOY
Release: 2016-07-13 09:53:43
Original
874 people have browsed it

A simple usage example of the data structure heap (SplHeap) of the PHP SPL standard library

This article mainly introduces a simple usage example of the data structure heap (SplHeap) of the PHP SPL standard library. This article also explains the relevant knowledge of the maximum heap (SplMaxHeap) and the minimum heap (SplMinHeap). Friends in need can refer to it

Heap is a data structure designed to implement priority queues. It is implemented by constructing a binary heap (a type of binary tree). The heap with the largest root node is called the maximum heap or large root heap, and the heap with the smallest root node is called the minimum heap or small root heap. Binary heaps are also commonly used for sorting (heap sort).

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 minimum heap (SplMinHeap) are implemented by inheriting it. There are no additional methods for max-heap and min-heap

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

{

//compare()方法用来比较两个元素的大小,绝对他们在堆中的位置

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;

}

1 2

34 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 and determine their positions 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 usage example This article mainly introduces the PHP SPL standard library A simple example of using the data structure heap (SplHeap). This article also explains the maximum heap...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template