PHP Conference Japan 2024

SplHeap 類別

(PHP 5 >= 5.3.0, PHP 7, PHP 8)

簡介

SplHeap 類別提供了堆積(Heap)的主要功能。

類別概要

abstract class SplHeap implements Iterator, Countable {
/* 方法 */
protected compare(mixed $value1, mixed $value2): int
public count(): int
public current(): mixed
公開 extract(): 混合
公開 insert(混合 $value):
公開 isEmpty(): 布林
公開 key(): 整數
公開 next():
公開 rewind():
公開 top(): 混合
公開 valid(): 布林
}

目錄

新增註釋

使用者貢獻的註釋 3 則註釋

Michelangelo van Dam
15 年前
為了讓大家了解 SplHeap 的用途,我寫了一個簡單的範例腳本,用來顯示比利時甲級足球聯賽 (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();
}
?>

結果輸出如下:
布魯日俱樂部 (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

希望這個例子能幫助大家更深入地理解和應用 SplHeap。
igorsantos07 at gmail dot com
10 年前
雖然 Michelangelo Van Dam 的例子 (http://br2.php.net/manual/en/class.splheap.php#93930) 很棒地展示了 SplHeap 的功能,但這個實現方式與 SplPriorityQueue 的功能完全相同——基於 SplMaxHeap。如果您打算複製該程式碼片段,請三思!已經有一個 SPL 類別可以完全滿足您的需求 :)
Anthony
8 年前
如果您想建立一個真正的基於樹的堆積 (Heap),您可以按照以下方式進行(使用 SplMinHeap 實現,如果您希望項目順序相反,則可以使用 SplMaxHeap)

我們嘗試表示的結構

1
|
+-----+--+--+-----+
| | | |
2 3 4 5
| | |
+ +-+-+ +
| | | |
7 6 8 9
|
+-+-+
| |
10 11

<?php
$h
= new SplMinHeap();

// [父節點, 子節點]
$h->insert([9, 11]);
$h->insert([0, 1]);
$h->insert([1, 2]);
$h->insert([1, 3]);
$h->insert([1, 4]);
$h->insert([1, 5]);
$h->insert([3, 6]);
$h->insert([2, 7]);
$h->insert([3, 8]);
$h->insert([5, 9]);
$h->insert([9, 10]);

for (
$h->top(); $h->valid(); $h->next()) {
list(
$parentId, $myId) = $h->current();
echo
"$myId ($parentId)\n";
}
?>

當您迭代堆積時,返回的數據將按照您閱讀書籍的方式讀取;即從左到右,從上到下。它不會遵循父子關係。

因此,上述程式碼將輸出以下內容

1 (0)
2 (1)
3 (1)
4 (1)
5 (1)
7 (2)
6 (3)
8 (3)
9 (5)
10 (9)
11 (9)
To Top