Binary Search Trees Binary Search Trees
Comp 122, Spring 2004
Binary Trees
Recursive definition
1. An empty tree is a binary tree 2. A node with two child subtrees is a binary tree 3. Only what you get from 1 by a finite number of applications of 2 is a binary tree.
56 26 200
Is this a binary tree?
18 12 24
28
190
213
27
rees - 2
Comp 122, Spring 2004
Binary Search Trees
View today as data structures that can support dynamic set operations.
Search, Minimum, Maximum, Predecessor, Successor, Insert, and Delete.
Can be used to build
Dictionaries. Priority Queues.
Basic operations take time proportional to the height of the tree O(h).
Comp 122, Spring 2004
rees - 3
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).
rees - 4
Comp 122, Spring 2004
Binary Search Tree Property
Stored keys must satisfy the binary search tree property.
y in left subtree of x, then key[y] key[x]. y in right subtree of x, then key[y] key[x]. 26 18 28 190 56 200 213
12
24
27
rees - 5
Comp 122, Spring 2004
Inorder Traversal
The binary-search-tree property allows the keys of a binary search tree to be printed, in (monotonically increasing) order, recursively.
Inorder-Tree-Walk (x) Inorder-Tree-Walk (x) 1. if x NIL 1. if x NIL 2. then Inorder-Tree-Walk(left[p]) 2. then Inorder-Tree-Walk(left[p]) 3. print key[x] 3. print key[x] 4. Inorder-Tree-Walk(right[p]) 4. Inorder-Tree-Walk(right[p])
56 26 28 190 200
18
213
12
24
27
How long does the walk take? Can you prove its correctness?
Comp 122, Spring 2004
rees - 6
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).
Comp 122, Spring 2004
rees - 7
Querying a Binary Search Tree
All dynamic-set search operations can be supported in O(h) time. h = (lg n) for a balanced binary tree (and for an average tree built by adding nodes in random order.) h = (n) for an unbalanced tree that resembles a linear chain of n nodes in the worst case.
rees - 8
Comp 122, Spring 2004
Tree Search
Tree-Search(x, k) Tree-Search(x, k) 1. if x = NIL or k = key[x] 1. if x = NIL or k = key[x] 2. then return x 2. then return x 3. if k < key[x] 3. if k < key[x] 4. then return Tree-Search(left[x], k) 4. then return Tree-Search(left[x], k) 5. else return Tree-Search(right[x], k) 5. else return Tree-Search(right[x], k)
Running time: O(h) Aside: tail-recursion
Comp 122, Spring 2004 12 18
56 26 200
28
190
213
24
27
rees - 9
Iterative Tree Search
Iterative-Tree-Search(x, k) Iterative-Tree-Search(x, k) 1. while x NIL and k 1. while x NIL and k key[x] key[x] 2. do if k < key[x] 2. do if k < key[x] 3. then x left[x] 3. then x left[x] 4. else x right[x] 4. else x right[x] 5. return x 5. return x
56 26 200
18
28
190
213
12
24
27
The iterative tree search is more efficient on most computers. The recursive tree search is more straightforward.
Comp 122, Spring 2004
rees - 10
Finding Min & Max
The binary-search-tree property guarantees that: The minimum is located at the left-most node. The maximum is located at the right-most node.
Tree-Minimum(x) Tree-Minimum(x) 1. while left[x] NIL 1. while left[x] NIL 2. do x left[x] 2. do x left[x] 3. return x 3. return x Q: How long do they take?
Tree-Maximum(x) Tree-Maximum(x) 1. while right[x] NIL 1. while right[x] NIL 2. do x right[x] 2. do x right[x] 3. return x 3. return x
rees - 11
Comp 122, Spring 2004
Predecessor and Successor
Successor of node x is the node y such that key[y] is the smallest key greater than key[x]. The successor of the largest key is NIL. Search consists of two cases.
If node x has a non-empty right subtree, then xs successor is the minimum in the right subtree of x. If node x has an empty right subtree, then:
As long as we move to the left up the tree (move up through right children), we are visiting smaller keys. xs successor y is the node that x is the predecessor of (x is the maximum in ys left subtree). In other words, xs successor y, is the lowest ancestor of x whose left child is also an ancestor of x.
Comp 122, Spring 2004
rees - 12
Pseudo-code for Successor
Tree-Successor(x) Tree-Successor(x) if right[x] NIL if right[x] NIL 2. then return Tree-Minimum(right[x]) 2. then return Tree-Minimum(right[x]) 3. y p[x] 3. y p[x] 4. while yy NIL and xx= right[y] 4. while NIL and = right[y] 5. do xx yy 5. do 6. yy p[y] 6. p[y] 7. return yy 7. return Code for predecessor is symmetric. Running time: O(h)
Comp 122, Spring 2004 12 18
56 26 200
28
190
213
24
27
rees - 13
BST Insertion Pseudocode
Change the dynamic set represented by a BST. Ensure the binary-searchtree property holds after change. Insertion is easier than deletion. 56
26 200
18
28
190
213
12
24
27
Tree-Insert(T, z) Tree-Insert(T, z) 1. yy NIL 1. NIL 2. xx root[T] 2. root[T] 3. while xx NIL 3. while NIL 4. do yy xx 4. do 5. if key[z] < key[x] 5. if key[z] < key[x] 6. then xx left[x] 6. then left[x] 7. else xx right[x] 7. else right[x] 8. p[z] yy 8. p[z] 9. if yy= NIL 9. if = NIL 10. 10. then root[t] zz then root[t] 11. 11. else if key[z] < key[y] else if key[z] < key[y] 12. then left[y] zz 12. then left[y] 13. else right[y] zz 13. else right[y]
rees - 14
Comp 122, Spring 2004
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) Tree-Insert(T, z) 1. yy NIL 1. NIL 2. xx root[T] 2. root[T] 3. while xx NIL 3. while NIL 4. do yy xx 4. do 5. if key[z] < key[x] 5. if key[z] < key[x] 6. then xx left[x] 6. then left[x] 7. else xx right[x] 7. else right[x] 8. p[z] yy 8. p[z] 9. if yy= NIL 9. if = NIL 10. 10. then root[t] zz then root[t] 11. 11. else if key[z] < key[y] else if key[z] < key[y] 12. then left[y] zz 12. then left[y] 13. else right[y] zz 13. else right[y]
Comp 122, Spring 2004
rees - 15
Exercise: Sorting Using BSTs
Sort (A) for i 1 to n do tree-insert(A[i]) inorder-tree-walk(root)
What are the worst case and best case running times? In practice, how would this compare to other sorting algorithms?
rees - 16
Comp 122, Spring 2004
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
Comp 122, Spring 2004
rees - 17
Deletion Pseudocode
Tree-Delete(T, z) Tree-Delete(T, z) /* Determine which node to splice out: either zzor zs successor. */ /* Determine which node to splice out: either or zs successor. */ if left[z] = NIL or right[z] = NIL if left[z] = NIL or right[z] = NIL then yy zz then else yy Tree-Successor[z] else Tree-Successor[z] /* Set xxto aanon-NIL child of x, or to NIL if yyhas no children. */ /* Set to non-NIL child of x, or to NIL if has no children. */ 4. if left[y] NIL 4. if left[y] NIL 5. then xx left[y] 5. then left[y] 6. else xx right[y] 6. else right[y] /* yyis removed from the tree by manipulating pointers of p[y] and /* is removed from the tree by manipulating pointers of p[y] and xx*/ */ 7. if xx NIL 7. if NIL 8. then p[x] p[y] 8. then p[x] p[y] /* Continued on next slide */ /* Continued on next slide */
Comp 122, Spring 2004
rees - 18
Deletion Pseudocode
Tree-Delete(T, z) (Contd. from previous slide) Tree-Delete(T, z) (Contd. from previous slide) 9. if p[y] = NIL 9. if p[y] = NIL 10. then root[T] xx 10. then root[T] 11. else if yy left[p[i]] 11. else if left[p[i]] 12. then left[p[y]] xx 12. then left[p[y]] 13. else right[p[y]] xx 13. else right[p[y]] /* If zs successor was spliced out, copy its data into zz*/ /* If zs successor was spliced out, copy its data into */ 14. if yy zz 14. if 15. then key[z] key[y] 15. then key[z] key[y] 16. copy ys satellite data into z. 16. copy ys satellite data into z. 17. return yy 17. return
Comp 122, Spring 2004
rees - 19
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.
rees - 20
Comp 122, Spring 2004
Binary Search Trees
View today as data structures that can support dynamic set operations.
Search, Minimum, Maximum, Predecessor, Successor, Insert, and Delete.
Can be used to build
Dictionaries. Priority Queues.
Basic operations take time proportional to the height of the tree O(h).
Comp 122, Spring 2004
rees - 21
Red-black trees: Overview
Red-black trees are a variation of binary search trees to ensure that the tree is balanced.
Height is O(lg n), where n is the number of nodes.
Operations take O(lg n) time in the worst case.
rees - 22
Comp 122, Spring 2004
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 redblack tree T, with color[nil] = black. The roots parent is also nil[T ].
Comp 122, Spring 2004
rees - 23
Red-black Tree Example
26
17
41
30 38
47 50
nil[T]
Comp 122, Spring 2004
rees - 24
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.
Comp 122, Spring 2004
rees - 25
Height of a Red-black Tree
Height of a node:
Number of edges in a longest path to a leaf.
Black-height of a node x, bh(x):
bh(x) is the number of black nodes (including nil[T ]) on the path from x to leaf, not counting x.
Black-height of a red-black tree is the black-height of its root.
By Property 5, black height is well defined.
rees - 26
Comp 122, Spring 2004
Height of a Red-black Tree
Example: Height of a node:
Number of edges in a longest path to a leaf. 17
h=1 bh=1
26
h=4 bh=2 h=3 bh=2
41
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
47
h=2 bh=1
38
h=1 50 bh=1
nil[T]
Comp 122, Spring 2004
rees - 27
Hysteresis : or the value of lazyness
Hysteresis, n. [fr. Gr. to be behind, to lag.] a retardation of an effect when the forces acting upon a body are changed (as if from viscosity or internal friction); especially: a lagging in the values of resulting magnetization in a magnetic material (as iron) due to a changing magnetizing force
rees - 28
Comp 122, Spring 2004