Skip to content

Instantly share code, notes, and snippets.

@simpleigh
Last active January 4, 2016 20:19
Show Gist options
  • Select an option

  • Save simpleigh/8672982 to your computer and use it in GitHub Desktop.

Select an option

Save simpleigh/8672982 to your computer and use it in GitHub Desktop.
<?php
// Really simple heap that expects to store numbers
class Heap extends SPLHeap
{
public function compare($value1, $value2)
{
return $value2 - $value1;
}
}
$heap = new Heap();
echo 'An empty heap contains ' . $heap->count() . ' entries.' . PHP_EOL . PHP_EOL;
// Add 20 random numbers
echo 'Inserting data...' . PHP_EOL . PHP_EOL;
for ($i = 0; $i < 20; $i++) {
$heap->insert(rand());
}
echo 'Now we have ' . $heap->count() . ' entries.' . PHP_EOL . PHP_EOL;
// Iterate over the heap, printing the numbers
echo 'Here they are:' . PHP_EOL . PHP_EOL;
foreach ($heap as $num) {
echo $num . PHP_EOL;
}
echo PHP_EOL;
echo 'Now we have ' . $heap->count() . ' entries.' . PHP_EOL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment