PHP的SPL标准库里面的堆(SplHeap)怎么使用
PHP的SPL标准库里面的堆(SplHeap)怎么使用
一、总结
1、因为SplHeap是抽象类,所以要先继承,实现里面的抽象方法compare后,才能new对象使用。
二、PHP的SPL标准库里面的堆(SplHeap)怎么使用
堆(Heap)就是为了实现优先队列而设计的一种数据结构,它是通过构造二叉堆(二叉树的一种)实现。根节点最大的堆叫做最大堆或大根堆,根节点最小的堆叫做最小堆或小根堆。二叉堆还常用于排序(堆排序)。
如下:最小堆(任意节点的优先级不小于它的子节点)
看看PHP
SplHeap的实现:
显然它是一个抽象类,最大堆(SplMaxHeap)和最小堆(SplMinHeap)就是继承它实现的。最大堆和最小堆并没有额外的方法
SplHeap的简单使用如下:
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 ; } |
三、参考手册SplHeap
简介¶
The SplHeap class provides the main functionalities of a Heap.
类摘要¶
}
Table of Contents¶
- SplHeap::compare — Compare elements in order to place them correctly in the heap while sifting up
- SplHeap::__construct — Constructs a new empty heap
- SplHeap::count — Counts the number of elements in the heap
- SplHeap::current — Return current node pointed by the iterator
- SplHeap::extract — Extracts a node from top of the heap and sift up
- SplHeap::insert — Inserts an element in the heap by sifting it up
- SplHeap::isCorrupted — Tells if the heap is in a corrupted state
- SplHeap::isEmpty — Checks whether the heap is empty
- SplHeap::key — Return current node index
- SplHeap::next — Move to the next node
- SplHeap::recoverFromCorruption — Recover from the corrupted state and allow further actions on the heap
- SplHeap::rewind — Rewind iterator back to the start (no-op)
- SplHeap::top — Peeks at the node from the top of the heap
- SplHeap::valid — Check whether the heap contains more nodes
实例
To have a good idea what you can do with SplHeap, I created a little example script that will show the rankings of Belgian soccer teams in the Jupiler League. <?php
/**
* A class that extends SplHeap for showing rankings in the Belgian
* soccer tournament JupilerLeague
*/
class JupilerLeague extends SplHeap
{
/**
* We modify the abstract method compare so we can sort our
* rankings using the values of a given array
*/
public function compare($array1, $array2)
{
$values1 = array_values($array1);
$values2 = array_values($array2);
if ($values1[0] === $values2[0]) return 0;
return $values1[0] < $values2[0] ? -1 : 1;
}
} // Let's populate our heap here (data of 2009)
$heap = new JupilerLeague();
$heap->insert(array ('AA Gent' => 15));
$heap->insert(array ('Anderlecht' => 20));
$heap->insert(array ('Cercle Brugge' => 11));
$heap->insert(array ('Charleroi' => 12));
$heap->insert(array ('Club Brugge' => 21));
$heap->insert(array ('G. Beerschot' => 15));
$heap->insert(array ('Kortrijk' => 10));
$heap->insert(array ('KV Mechelen' => 18));
$heap->insert(array ('Lokeren' => 10));
$heap->insert(array ('Moeskroen' => 7));
$heap->insert(array ('Racing Genk' => 11));
$heap->insert(array ('Roeselare' => 6));
$heap->insert(array ('Standard' => 20));
$heap->insert(array ('STVV' => 17));
$heap->insert(array ('Westerlo' => 10));
$heap->insert(array ('Zulte Waregem' => 15)); // For displaying the ranking we move up to the first node
$heap->top(); // Then we iterate through each node for displaying the result
while ($heap->valid()) {
list ($team, $score) = each ($heap->current());
echo $team . ': ' . $score . PHP_EOL;
$heap->next();
}
?> This results in the following output:
Club Brugge: 21
Anderlecht: 20
Standard: 20
KV Mechelen: 18
STVV: 17
Zulte Waregem: 15
AA Gent: 15
G. Beerschot: 15
Charleroi: 12
Racing Genk: 11
Cercle Brugge: 11
Kortrijk: 10
Lokeren: 10
Westerlo: 10
Moeskroen: 7
Roeselare: 6 Hope this example paved the way for more complex implementations of SplHeap.
四、测试题-简答题
1、优先队列的最主要作用是什么(两点)?
解答:a、实现优先队列 b、常用于排序(堆排序)
2、大根堆和小根堆的定义是什么?
解答:根节点最大的堆叫做最大堆或大根堆。
3、SplHeap是一个抽象类,那么我们要怎么使用它呢?
解答:继承,实现里面的抽象方法就可以使用了。
4、SplHeap里面的抽象方法有哪些?
解答:只有一个compare,abstract protected int compare ( mixed $value1
, mixed $value2
)
5、SplHeap里面的抽象方法compare方法怎么实现?
解答:就和普通方法的实现完全一样,因为会覆盖的。public
function
compare(
$value1
,
$value2
)
{}
6、最大堆(SplMaxHeap)和最小堆(SplMinHeap)怎么实现的?
解答:最大堆(SplMaxHeap)和最小堆(SplMinHeap)就是继承SplHeap抽象类而实现的
7、我们应该怎么使用别人的类呢(从抽象类和普通类来说)?
解答:先看别人的类是不是抽象类,是的话我们要继承才能使用,还要注意实现里面的抽象方法,不是的话,直接new对象就好。
8、堆的英文怎么说,php中的标准库中的堆怎么写类名?
解答:Heap,驼峰命名法SplHeap,类首字母大写。
9、SplHeap的最常用三个方法是什么?
解答:insert(),top(),count()。
10、SplHeap的compare方法的返回值我们怎么写?
解答:return
(
$value1
-
$value2
);
11、遍历堆的两种方法?
解答:foreach 和(valid()、current()、next())套件
12、PHP_EOF怎么使用?
解答:连接符.PHP_EOF
PHP的SPL标准库里面的堆(SplHeap)怎么使用的更多相关文章
- PHP SPL标准库之数据结构栈(SplStack)介绍(基础array已经可以解决很多问题了,现在开始解决问题)
PHP SPL标准库之数据结构栈(SplStack)介绍(基础array已经可以解决很多问题了,现在开始解决问题) 一.总结 SplStack就是继承双链表(SplDoublyLinkedList)实 ...
- php spl标准库简介(SPL是Standard PHP Library(PHP标准库)(直接看代码实例,特别方便)
php spl标准库简介(SPL是Standard PHP Library(PHP标准库)(直接看代码实例,特别方便) 一.总结 直接看代码实例,特别方便易懂 thinkphp控制器利眠宁不支持(说明 ...
- PHP SPL标准库-接口
PHP SPL标准库有一下接口: Countable OuterIterator RecursiveIterator SeekableIterator SplObserver SplSubject A ...
- PHP 设计模式 笔记与总结(3)SPL 标准库
SPL 库的使用(PHP 标准库) 1. SplStack,SplQueue,SplHeap,SplFixedArray 等数据结构类 ① 栈(SplStack)(先进后出的数据结构) index.p ...
- 【SPL标准库专题(1)】 SPL简介
什么是SPL SPL是Standard PHP Library(PHP标准库)的缩写. 根据官方定义,它是"a collection of interfaces and classes th ...
- PHP SPL标准库-数据结构
SPL是用于解决典型问题的一组接口与类的集合. 双向链表 SplDoublyLinkedList SplStack SplQueue 双链表是一种重要的线性存储结构,对于双链表中的每个节点,不仅仅存储 ...
- PHP的SPL标准库
1,简介 SPL,全称 Standard PHP Library 中文是 标准PHP类库.是php内置的一些拓展类和拓展接口,其内容包含数据结构.迭代器.接口.异常.SPL函数,文件处理等内容.SPL ...
- SPL标准库常用的数据结构
栈数据结构 $stack = new SplStack(); //栈数据结构->先进后出 2 $stack->push('data1'); //入栈 $stack->push('da ...
- SPL标准库-数据结构
数据结构:栈 );] = ;] = ;var_dump($array); 来自为知笔记(Wiz)
随机推荐
- BZOJ4864: [BeiJing 2017 Wc]神秘物质(Splay)
Description 21ZZ 年,冬. 小诚退休以后, 不知为何重新燃起了对物理学的兴趣. 他从研究所借了些实验仪器,整天研究各种微观粒子.这 一天, 小诚刚从研究所得到了一块奇异的陨石样本, 便 ...
- CF1009F Dominant Indices(树上DSU/长链剖分)
题目大意: 就是给你一棵以1为根的树,询问每一个节点的子树内节点数最多的深度(相对于这个子树根而言)若有多解,输出最小的. 解题思路: 这道题用树链剖分,两种思路: 1.树上DSU 首先想一下最暴力的 ...
- java 钩子方法
Runtime.getRuntime().addShutdownHook(shutdownHook); 这个方法的含义说明: 这个方法的意思就是在jvm中增加一个关闭的钩子,当jv ...
- hdu-3642--Get The Treasury-线段树求面积并
求空间中叠加3次及3次以上的体积. 由于|z|<=500.所以直接把z轴剥离出来1000层. 然后对于每一层进行线段树求面积并. #include<stdio.h> #include ...
- Android网络框架OkHttp之get请求(源码初识)
概括 OkHttp现在很火呀.于是上个星期就一直在学习OkHttp框架,虽然说起来已经有点晚上手了,貌似是2013年就推出了.但是现在它版本更加稳定了呀.这不,说着说着,OkHttp3.3版本在这几天 ...
- ORA-01665 control file is not a standby control file
ORA-01665错误处理 问题描述: 在备库启动至mount状态时,报如下错误: ORA-01665: control file is not a standby control file 解决办法 ...
- winform最大化后不遮挡任务栏
在窗体初始化后添加一句代码 this.MaximizedBounds = Screen.PrimaryScreen.WorkingArea;
- express,中间件(body-parser),req.body获取不到参数(含postman发请求的方法)
问题描述: 最近在做毕设,express 里边的中间件(body-parser)失效,req.body获取不到任何值,req.query能获取到值.一开始加body-parser中间件是有用的,直到昨 ...
- 最新GitHub新手使用教程(Linux/Ubuntu Git从安装到使用)——详细图解
说明:该篇博客是博主一字一码编写的,实属不易,请尊重原创,谢谢大家! 一.叙述 1.说明:需要在Windows 安装Git的同学,可以查看该篇博客 https://blog.csdn.net/qq_4 ...
- 去哪网实习总结:用到的easyui组件总结(JavaWeb)
本来是以做数据挖掘的目的进去哪网的,结构却成了系统开发... 只是还是比較认真的做了三个月,老师非常认同我的工作态度和成果.. . 实习立即就要结束了,总结一下几点之前没有注意过的变成习惯和问题,分享 ...