Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / PHP

SplMinHeap

Give the smallest value highest extraction priority.

Version: PHP 8.4; repository runtime, production runtime not attested

Syntax

new SplMinHeap()

Give the smallest value highest extraction priority. Inserting a new smaller value changes top immediately. extract removes one occurrence, so duplicate minima can be returned on consecutive calls. Empty-heap extraction raises RuntimeException.

$heap=new SplMinHeap(); foreach([9,3,3,5] as $n){$heap->insert($n);}
echo json_encode([$heap->top(),$heap->extract(),$heap->extract(),$heap->top()]);
[3,3,3,5]

Examples

<?php
$heap=new SplMinHeap(); foreach([9,3,3,5] as $n){$heap->insert($n);}
echo json_encode([$heap->top(),$heap->extract(),$heap->extract(),$heap->top()]);

Result

[3,3,3,5]

Sources