Binary Trees
Binary Trees
Many of the slides are from Prof. Plaisteds resources at University of North Carolina at Chapel Hill
Basic operations take time proportional to the height of the tree O(h).
BST Representation
Represented by a linked data structure of nodes. root(T) points to the root of tree T. Each node contains fields:
key left pointer to left child: root of left subtree. right pointer to right child : root of right subtree. p pointer to parent. p[root[T]] = NIL (optional).
56 200
18
28
190
213
12
24
27
Inorder Traversal
The binary-search-tree property allows the keys of a binary search tree to be printed, in (monotonically increasing) order, recursively.
56 26 200
18
28
190
213
12
24
27
How long does the walk take? Can you prove its correctness?
Correctness of Inorder-Walk
Must prove that it prints all elements, in order, and that it terminates. By induction on size of tree. Size=0: Easy. Size >1:
Prints left subtree in order by induction. Prints root, which comes after all elements in left subtree (still in order). Prints right subtree in order (all elements come after root, so still in order).
Tree Search
Tree-Search(x, k) 1. if x = NIL or k = key[x] 2. then return x 3. if k < key[x] 4. then return Tree-Search(left[x], k) 5. else return Tree-Search(right[x], k)
26 28
56 200
18
190
213
24
27
18
28
190
213
12
24
27
The iterative tree search is more efficient on most computers. The recursive tree search is more straightforward.
Tree-Minimum(x) 1. while left[x] NIL 2. do x left[x] 3. return x Q: How long do they take?
56 26 200
18
28
190
213
24
27
Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
18
28
190
213
12
24
27
Tree-Insert(T, z) 1. y NIL 2. x root[T] 3. while x NIL 4. do y x 5. if key[z] < key[x] 6. then x left[x] 7. else x right[x] 8. p[z] y 9. if y = NIL 10. then root[t] z 11. else if key[z] < key[y] 12. then left[y] z 13. else right[y] z
Analysis of Insertion
Initialization: O(1)
While loop in lines 3-7 searches for place to insert z, maintaining parent y. This takes O(h) time.
Lines 8-13 insert the value: O(1) TOTAL: O(h) time to insert a node.
Tree-Insert(T, z) 1. y NIL 2. x root[T] 3. while x NIL 4. do y x 5. if key[z] < key[x] 6. then x left[x] 7. else x right[x] 8. p[z] y 9. if y = NIL 10. then root[t] z 11. else if key[z] < key[y] 12. then left[y] z 13. else right[y] z
Tree-Delete (T, x)
if x has no children case 0 then remove x if x has one child case 1 then make p[x] point to child if x has two children (subtrees) case 2 then swap x with its successor perform case 0 or case 1 to delete it TOTAL: O(h) time to delete a node
Do it on blackboard
Deletion Pseudocode
Tree-Delete(T, z) /* Determine which node to splice out: either z or zs successor. */ if left[z] = NIL or right[z] = NIL then y z else y Tree-Successor[z] /* Set x to a non-NIL child of y, or to NIL if y has no children. */ 4. if left[y] NIL 5. then x left[y] 6. else x right[y] /* y is removed from the tree by manipulating pointers of p[y] and x */ 7. if x NIL 8. then p[x] p[y] /* Continued on next slide */
Deletion Pseudocode
Tree-Delete(T, z) (Contd. from previous slide) 9. if p[y] = NIL 10. then root[T] x 11. else if y left[p[i]] 12. then left[p[y]] x 13. else right[p[y]] x /* If zs successor was spliced out, copy its data into z */ 14. if y z 15. then key[z] key[y] 16. copy ys satellite data into z. 17. return y
Correctness of Tree-Delete
How do we know case 2 should go to case 0 or case 1 instead of back to case 2? Because when x has 2 children, its successor is the minimum in its right subtree, and that successor has no left child (hence 0 or 1 child).
Equivalently, we could swap with predecessor instead of successor. It might be good to alternate to avoid creating lopsided tree.
Basic operations take time proportional to the height of the tree O(h).
Red-black Tree
Binary search tree + 1 bit per node: the attribute color, which is either red or black. All other attributes of BSTs are inherited: key, left, right, and p. All empty trees (leaves) are colored black.
We use a single sentinel, nil, for all the leaves of red-black tree T, with color[nil] = black. The roots parent is also nil[T ].
17
41
30 38
47 50
nil[T]
Red-black Properties
1. 2. 3. 4. Every node is either red or black. The root is black. Every leaf (nil) is black. If a node is red, then both its children are black.
5. For each node, all paths from the node to descendant leaves contain the same number of black nodes.
Black-height of a node bh(x) is the number of black nodes on path from x to leaf, not counting x.
h=2 30 bh=1
h=1 bh=1
h=2 47 bh=1
h=1 50 bh=1
38
nil[T]