(Available since version 1.0)
Heap::update — Updates a node in the heap to a new value
Updates a node in the heap from its current value to a
new value, value2
. Where the
current value is given by executing
Heap::value() on
value1
.
If two nodes in the heap have the same value then only one will be changed.
value1
The current value.
value2
The new value.
No value is returned.
Throws SEIDS::Heaps::UpdateException when
value1
is not in the heap.
Example #1 Heap::update example (using a max-heap)
<?php
require 'vendor/autoload.php'; // A PSR-4 or PSR-0 autoloader
use SEIDS\Heaps\Pairing\MaxHeap;
$mh = new MaxHeap();
$mh->insert(1);
$mh->insert(2);
$mh->insert(3);
$mh->insert(4);
$mh->insert(5);
$mh2 = clone $mh;
$mh->update(2, 6);
$mh->update(4, 0);
foreach ($mh2 as $v) {
var_dump($v);
}
echo "\n";
foreach ($mh as $v) {
var_dump($v);
}
?>
The above example will output:
int(5) int(4) int(3) int(2) int(1) int(6) int(5) int(3) int(1) int(0)