Binary Search Trees: Lecture 10, Oct 28 2014
Binary Search Trees: Lecture 10, Oct 28 2014
In addition to insert/delete:
» Heaps supported min/max.
» Hashing supported search.
» What if we want both min/max/search, and also pred/succ ?
122
Examples
Legal Binary Search Trees: 25
8
9 30
3 25
8
1 5 9 30 5
Note that given Binary tree, can output sorted in O(n) time !
Gives lower bound on constructing Binary Tree.
(Compare with building a Heap)
123
1
Searching in Binary Tree
8
Check if “current node” is =x or =NIL.
Equal to x -- return,
Equal to NIL -- return “not found” 3 25
else:
if x current key ---- search in the left tree
otherwise ---- search in the right tree.
1 5 9 30
124
Insertion: search for key, and put it in the first empty space.
Sort: 9
» Insert item-by-item,
» in-order walk. 8
» (n2)… 5
125
2
Relation to Quicksort
Consider example: 3 1 8 2 6 7 5
» Quicksort chooses 3, then compares 1,8,2,6,7,5 to 3.
Then chooses 1, compares to 2
chooses 8, compares 6,7,5 to 8.
» Binary Tree: chooses 3, places as root
Then chooses 1, compares with 3, put in place.
chooses 8, compares with 3, put in place
etc…
» Overall, same comparisons, only different order !!
126
Successor/Predecessor
Min/max - go all the way left or all the way right.
Successor:
If right(x) != NIL, return TreeMin(right(x)) Easy case
y=parent(x)
while y != NIL & x=right(y)
x=y
y=parent(x) Go up the tree to the LEFT
return y
3 25
1 5 9 30
4 6
Successor of 6 is 8. Predecessor of 8 is 6.
What if we go “up left” and reach root, i.e. cannot ever go “up right” ?
127
3
Deletion
3 Cases:
» No children – simple delete, replace pointer from parent with null
» 1 child – delete, redirect pointer from parent to point to child
» 2 children – cannot simply delete
128
8
8
3 25
4 25
1 5 9 30
1 5 9 30
4 6
4 6
4.5
8 4.5
3 25
1 5 9 30
4.5 6
4.5 129
4
Summary
130
Red/Black trees
Red/Black property:
» Node is either black (default) or red.
» x red children(x) are black.
» Every simple root-leaf path has same # of black nodes.
» Root is black (for convenience).
» Add NIL to leaves so “real nodes” have 2 children, color leaves black.
131
5
Black height
132
Balancing
Observation: bh(root)h/2
THM: h 2 log(n+1)
Proof:
» Claim: #nodes in subtree rooted at x 2bh(x)-1.
» By induction on h(x). Base case is trivial. x
» if y black, bh(y)=bh(x)-1 y z
otherwise, bh(y)=bh(x)
» Thus, bh(y) bh(x)-1
133