Heap::update

(Available since version 1.0)

Heap::updateUpdates a node in the heap to a new value

Description

public void Heap::update ( mixed $value1 , mixed $value2 )

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.

Note:

Runs in Θ(log(n)), where n is the size of the heap.

Parameters

value1

The current value.

value2

The new value.

Return Values

No value is returned.

Errors/Exceptions

Throws SEIDS::Heaps::UpdateException when value1 is not in the heap.

Examples

Example #1 Heap::update example (using a max-heap)

<?php
require 'vendor/autoload.php'// A PSR-4 or PSR-0 autoloader
use SEIDS\Heaps\Binary\MaxHeap;

$mh = new MaxHeap();

$mh->insert(1);
$mh->insert(2);
$mh->insert(3);
$mh->insert(4);
$mh->insert(5);

$mh2 = clone $mh;

$mh->update(26);
$mh->update(40);

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)

To Top