These lecture notes are based on chapter 9 in ``Data Structures, Algorithms, & Software Principles in C'' by Thomas Standish.
A complete binary tree is a tree that has all leaves on the same level or two adjacent levels, such that the leaves in the bottommost level are as far left as possible.
This is a complete binary tree.
9
/ \
5 8
/ \
3 4
This is not (leaves aren't to the left).
9
/ \
5 8
/ \ \
3 4 7
This is not (leaves aren't on adjacent levels).
9
/ \
5 7
/ \
3 3
/ \
1 4
Why should you care? Because we can efficiently store a complete binary tree in an array as follows.
A level-order labeled tree, level-order means visit nodes across rather than down the tree first.
a 1
/ \
2 b c 3
/ \
4 d e 5
Sequential representation
A[1] = a
A[2] = b
A[3] = c
A[4] = d
A[5] = e
Verify that the relationships listed above hold, i.e., parent of d at A[4] is A[4/2] = A[2] = b.
A heap is a complete binary tree with values stored in its nodes such that no child has a value bigger than the value of the parent.
Below is a heap.
9
/ \
8 2
/ \
6 4
A heap provides a representation for a priority queue. Example: messages processed by priority at a server
Removal causes heap to be reheapified. For example if we remove 9
?
/ \
8 2
/ \
6 4
then we reheapify by copying rightmost leaf to root (4 becomes the root)
4
/ \
8 2
/
6
and then we recursively reestablish the heap property as follows:
if the parent is greater than a child, swap the parent with the
highest priority child.
Keep swapping until no more swaps are possible.
So in the above tree, first we would swamp 4 with 8.
8
/ \
4 2
/
6
Then we would swap 4 with 6.
8
/ \
6 2
/
4
The final swap yields a heap!
The cost of removing an item (reheapifiying after removing the item) is O(log n). The algorithm just traverses one path in the tree, which is O(log n) in length. For each node on that path it performs at most two comparisons and one swap (3 operations -> constant time). So overall the algorithm has a worst case time complexity of O(log n).
Space complexity is O(n) since a sequential array representation can be used.
Consider three implementations of a priority queue
The primary characteristic of a binary search tree is that, for each node, keys in the left subtree are less than the key in the node which is in turn less than the keys in the right subtree. Below is an example of a binary search tree.
8
/ \
4 9
/ \
2 6
The tree shown below is not a binary search tree since 4 is to the right of 6.
8
/ \
6 9
/ \
2 4
Binary search trees support efficient retrieval of keys. How efficient?