Skip to content

Binary Heap

Hudson Cassidy edited this page Mar 28, 2021 · 2 revisions

The Binary Heap can be created as such

use PhpTrees\BinaryHeap;
$h = new BinaryHeap();  //with an optional initial value here as well

It can also be constructed from an existing array

$a = [1, 6, 5, 2, 8, 6, 3, 2];
$h = new BinaryHeap();
$h->constructFromArray($a);

Element can be added as such

$h = new BinaryHeap();
$h->insert(7);
//OR
$h->insertMultiple(1, 6, 3, 7, 7, 3, 2);

the minimum and maximum elements can be obtained, as well as the size like so

//.......
$h->getMinValue();
$h->getMaxValue();
$h->getSize();

and finally to delete the minimum element, and get the old minimum

//.......
$oldMin = $h->deleteMin();
Clone this wiki locally