struct BTNodeT {
datatype data;
keytype key;
BTNodeT * leftChild;
BTNodeT * rightChild;
}
Parent(i) = i/2 LeftChild(i) = 2i RightChild(i) = 2i+1
| Index | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Value | 1 | 2 | 3 | 10 | 3 | 7 | 11 | 15 | 17 | 20 | 9 | 15 | 8 | 16 |
HeapifyUp(H,i)
- While i > 1
- let j = parent(i)
- If H[i].key() < H[j].key()
- swap(H[i],H[j])
- i = j
- Else
- i = 1
- EndIF
- EndWhle
HeapifyDown(H,i)
- While 2*i < size
- left = LeftChild(i), right = RightCild(i);
- checkPos = left
- If right is valid and H[right].key < H[left].key
- checkPos = right
- EndIf
- If H[checkPos].key < H[i]
- swap items at checkpos and i
- i = checkpos
- Else
- i = size
- EndIF
- EndWhile
By Induction:
Prove true for a base case:
At level 0 a binary tree has at most 1 or 20 nodes. (the root)
Assume true for an arbitrary k > 0 (the base case.)
At level k, a binary tree has at most 2k nodes.
Prove true for for h = k+1
At level k there are at most 2k nodes (previous assumption)
Each of these nodes can have at least 2 children (definition of binary tree)
So there are at most 2*2k nodes at the next level.
2*2k = 2k+1
Observe from the previous proof that each level has at most 2h
So a tree of height h has levels from 0 to h
$$\sum_{i=0}^{h}2^i = 2^{h+1}-1 $$
Think of this a binary number of all 1
111 = 4 + 2 + 1 = 7 = 8-1 = 23-1
1111 = 8 + 4 + 2 + 1 = 15 = 16-1 = 24-1