If you wish to build a true tree based heap, you can do so as follows (implemented with SplMinHeap, but could be SplMaxHeap if you wish for the opposite order of items):
The stucture that we're trying to represent:
1
|
+-----+--+--+-----+
| | | |
2 3 4 5
| | |
+ +-+-+ +
| | | |
7 6 8 9
|
+-+-+
| |
10 11
<?php
$h = new SplMinHeap();
$h->insert([9, 11]);
$h->insert([0, 1]);
$h->insert([1, 2]);
$h->insert([1, 3]);
$h->insert([1, 4]);
$h->insert([1, 5]);
$h->insert([3, 6]);
$h->insert([2, 7]);
$h->insert([3, 8]);
$h->insert([5, 9]);
$h->insert([9, 10]);
for ($h->top(); $h->valid(); $h->next()) {
list($parentId, $myId) = $h->current();
echo "$myId ($parentId)\n";
}
?>
As you iterate over the heap, the return data will be read as if you're reading a book; ie left to right, top to bottom. It will NOT follow the relationships.
So, the above code will output the following:
1 (0)
2 (1)
3 (1)
4 (1)
5 (1)
7 (2)
6 (3)
8 (3)
9 (5)
10 (9)
11 (9)