**Unformatted text preview: **a
heap must also be a heap.
As the largest value must be at the root, this
structure is the most efficient way of storing a
priority queue.
We are only interested in the highest priority,
provided that when the root is removed, the heap
can be remade efficiently. 3
4
0
8
9
5
6 3
4 0 8 9 5 6 We need only the exact number
need only the exact number
of array elements. Here's how we make a heap:
Step 1: build a balanced binary tree from the
data – not a search tree.
In fact we just put the values directly into the tree
In fact, we just put the values directly into the tree
(in
(in consecutive elements of the array).
Consider the values
3, 4, 0, 8, 9, 5, 6 3
4
0
8
9
5
6 3
4 0 8 9 5 6 We know where the last parent is.
know where the last parent is.
If there are n values, the last child is
at n-1, so nlast = (n-2)/2
nlast
(n- Consider the values
Step 2: Rearrange the contents into a heap.
3, 4, 0, 8, 9, 5, 6 Step 2: Rearrange the contents into a heap. We
start at the bottom, making each subtree a heap.
Let's use a slig...

View
Full
Document