SplHeap 堆

最后更新于:2022-04-02 02:26:07

[TOC] ## 实例 ``` class JupilerLeague extends SplHeap { public function compare($array1, $array2) { $values1 = array_values($array1)[0]; $values2 = array_values($array2)[0]; if ($values1 === $values2) return 0; return $values1 < $values2 ? -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)); //$heap->top(); while ($heap->valid()) { $current = $heap->current(); $team =key($current); $score =$current[$team]; 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 ```
';