Last active
January 4, 2016 20:19
-
-
Save simpleigh/8672982 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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