Title: Binary search trees
1Binary search trees
- Binhai Zhu
- Computer Science Department, Montana State
University -
2Definition
- Binary search tree
- (1) Each node, besides a key field and some
satellite data, contains left, right, and p
pointers that point to its left child, its right
child and its parent. - (2) The root is the only node whose parent
field is NIL. Of course, all the leaves nodes
have both NIL left field and NIL right field.
3Example
root
10
7
14
16
9
5
leaf
8
6
2
leaf
leaf
leaf
4Operations
Search
10
7
14
16
9
5
8
6
2
5Operations
Search Minimum Maximum Predecessor Successor Inser
t Delete
10
7
14
16
9
5
8
6
2
6Binary search tree property
10
7
14
16
9
5
8
6
2
For any node x, let y be a node in the left tree
of x, then keyy keyx. Likewise, if y is a
node in the right subtree of x then keyxkeyy.
7Binary search tree property
10
x
7
14
16
9
5
z
y
8
6
2
For any node x, let y be a node in the left tree
of x, then keyy keyx. Likewise, if z is a
node in the right subtree of x then keyxkeyz.
8Tree traversals
Inorder Preorder Postorder
10
7
14
16
9
5
8
6
2
9Tree traversals
Inorder(node x) If x ? NIL Inorder(x?left)
print(x) Inorder(x?right)
10
7
14
16
9
5
8
6
2
10Tree traversals
Inorder(node x) If x ? NIL Inorder(x?left)
print(x) Inorder(x?right)
10
7
14
16
9
5
8
6
2
2,5,6,7,8,9,10,14,16
11Tree traversals
Inorder(node x) If x ? NIL Inorder(x?left)
print(x) Inorder(x?right)
10
7
14
16
9
5
8
6
2
2,5,6,7,8,9,10,14,16 (thats exactly the sorted
ordering!)
12Tree traversals
Preorder(node x) If x ? NIL print(x)
Preorder(x?left) Preorder(x?right)
10
7
14
16
9
5
8
6
2
13Tree traversals
Preorder(node x) If x ? NIL print(x)
Preorder(x?left) Preorder(x?right)
10
7
14
16
9
5
8
6
2
10,7,5,2,6,9,8,14,16
14Tree traversals
Postorder(node x) If x ? NIL
Postorder(x?left) Postorder(x?right)
print(x)
10
7
14
16
9
5
8
6
2
15Tree traversals
Postorder(node x) If x ? NIL
Postorder(x?left) Postorder(x?right)
print(x)
10
7
14
16
9
5
8
6
2
2,6,5,8,9,7,16,14,10
16Tree traversals
What is the running time?
10
7
14
16
9
5
8
6
2
17Minimum and Maximum
10
7
14
16
9
5
8
6
2
18Minimum and Maximum
10
Minimum(node x) while x ? left ? NIL do x
? x?left return x
7
14
16
9
5
8
6
2
19Minimum and Maximum
10
- Minimum(node x)
- while x ? left ? NIL
- do x ? x?left
- return x
- Maximum(node x)
- while x ? right ? NIL
- do x ? x?right
- return x
7
14
16
9
5
8
6
2
20Search
x
10
7
14
16
9
5
k11?
8
6
2
k6
21Search
Search(node x, k) if x NIL or k keyx
then return x if x lt keyx then return
Search(x?left,k) else return
Search(x?right,k)
x
10
7
14
16
9
5
8
6
2
k6
22Search
Search(node x, k) if x NIL or k keyx
then return x if x lt keyx then return
Search(x?left,k) else return
Search(x?right,k) Iterative-Search(node x,k)
while x?NIL and k?keyx if k lt keyx
then x ? x?left else x ? x?right
return x
x
10
7
14
16
9
5
8
6
2
k6
23Successor
The successor of x is the node with the smallest
key greater than keyx.
10
7
14
x
16
9
5
8
6
2
24Successor
Successor(node x) if x?right ? NIL then
return Minimum(x?right) y ? x?p while y?NIL and
xy?right x ? y y ? y?p return y
10
7
14
16
9
5
x
8
6
2
25Successor
Successor(node x) if x?right ? NIL then
return Minimum(x?right) y ? x?p while y?NIL and
xy?right x ? y y ? y?p return y
10
7
14
16
9
5
x
8
6
2
Search, Minimum, Maximum, Successor all run in
O(h) time, where h is the height of the
corresponding binary search tree.
26Insertion
Insert a new node z with keyzv into a tree T.
10
7
14
16
9
5
9.5
8
6
2
z
27Insertion
Insert(T,z) y ? NIL x ? root(T) While x ? NIL
y ? x if keyz lt keyx then x
? x?left else x ? x?right z?p ? y
x
10
7
14
16
9
5
9.5
8
6
2
z
28Insertion
Insert(T,z) y ? NIL x ? root(T) While x ? NIL
y ? x if keyz lt keyx then x
? x?left else x ? x?right z?p ? y
y
x
10
7
14
16
9
5
9.5
8
6
2
z
29Insertion
Insert(T,z) y ? NIL x ? root(T) While x ? NIL
y ? x if keyz lt keyx then x
? x?left else x ? x?right z?p ? y
y
10
7
14
x
16
9
5
9.5
8
6
2
z
30Insertion
Insert(T,z) y ? NIL x ? root(T) While x ? NIL
y ? x if keyz lt keyx then x
? x?left else x ? x?right z?p ? y
10
y
7
14
x
16
9
5
9.5
8
6
2
z
31Insertion
Insert(T,z) y ? NIL x ? root(T) While x ? NIL
y ? x if keyz lt keyx then x
? x?left else x ? x?right z?p ? y
10
y
7
14
x
16
9
5
9.5
8
6
2
z
32Insertion
Insert(T,z) y ? NIL x ? root(T) While x ? NIL
y ? x if keyz lt keyx then x
? x?left else x ? x?right z?p ? y
10
7
14
y
16
9
5
x?NIL
9.5
8
6
2
z
33Insertion
Insert(T,z) y ? NIL x ? root(T) While x ? NIL
y ? x if keyz lt keyx then x
? x?left else x ? x?right z?p ? y
10
7
14
y
16
9
5
9.5
8
6
2
z
34Insertion
Insert(T,z) y ? NIL x ? root(T) While x ? NIL
y ? x if keyz lt keyx then x
? x?left else x ? x?right z?p ? y If y
NIL then rootT?z else if keyz lt key y
then y?left ? z else y?right ? z
10
7
14
y
16
9
5
9.5
8
6
2
z
35Deletion
10
z
z
7
14
16
9
5
8
6
2
z
36Deletion
10
7
14
16
9
5
8
6
2
z
37Deletion
10
7
14
16
9
5
X
8
6
2
z
38Deletion
10
z
7
14
16
9
5
8
6
2
39Deletion
10
X
z
7
14
X
16
9
5
8
6
2
40Deletion
10
z
7
14
16
9
5
8
6
2
41Deletion
10
z
7
14
16
9
5
8
6
2
Find the successor of z
42Deletion
10
z
8
14
16
9
5
8
6
2
Find the successor y of z Replace z with y Delete
y (careful, as y might have a right child)
43Deletion
- 1. if z?leftNIL or z?rightNIL
- then y ? z
- else y ? Successor(z)
- 4. if y?left?NIL
- then x ? y?left
- else x ? y?right
- 7. if x ? NIL
- then x?p ? y?p
- 9. if y?p NIL
- 10. then rootT ? x
- else if y (y?p)?left
- then (y?p)?left ? x
- else (y?p)?right ? x
- 14.if y?z
- then keyz ? keyy
- copy ys data into z
- 17.return y
10
z
7
z
14
9
16
5
8
6
2
z
8.5
44Deletion
- 1. if z?leftNIL or z?rightNIL
- then y ? z
- else y ? Successor(z)
- 4. if y?left?NIL
- then x ? y?left
- else x ? y?right
- 7. if x ? NIL
- then x?p ? y?p
- 9. if y?p NIL
- 10. then rootT ? x
- else if y (y?p)?left
- then (y?p)?left ? x
- else (y?p)?right ? x
- 14.if y?z
- then keyz ? keyy
- copy ys data into z
- 17.return y
10
z
7
z
14
y
9
16
5
8
y
6
2
z
8.5
y
45Deletion
- 1. if z?leftNIL or z?rightNIL
- then y ? z
- else y ? Successor(z)
- 4. if y?left?NIL
- then x ? y?left
- else x ? y?right
- 7. if x ? NIL
- then x?p ? y?p
- 9. if y?p NIL
- 10. then rootT ? x
- else if y (y?p)?left
- then (y?p)?left ? x
- else (y?p)?right ? x
- 14.if y?z
- then keyz ? keyy
- copy ys data into z
- 17.return y
10
z
7
z
14
y
9
16
x
5
8
y
6
2
z
8.5
x
y
xNIL
46Deletion
- 1. if z?leftNIL or z?rightNIL
- then y ? z
- else y ? Successor(z)
- 4. if y?left?NIL
- then x ? y?left
- else x ? y?right
- 7. if x ? NIL
- then x?p ? y?p
- 9. if y?p NIL
- 10. then rootT ? x
- else if y (y?p)?left
- then (y?p)?left ? x
- else (y?p)?right ? x
- 14.if y?z
- then keyz ? keyy
- copy ys data into z
- 17.return y
10
z
7
z
14
y
9
16
x
5
8
y
6
2
z
8.5
x
y
xNIL
47Deletion
- 1. if z?leftNIL or z?rightNIL
- then y ? z
- else y ? Successor(z)
- 4. if y?left?NIL
- then x ? y?left
- else x ? y?right
- 7. if x ? NIL
- then x?p ? y?p
- 9. if y?p NIL
- 10. then rootT ? x
- else if y (y?p)?left
- then (y?p)?left ? x
- else (y?p)?right ? x
- 14.if y?z
- then keyz ? keyy
- copy ys data into z
- 17.return y
10
X
z
7
z
14
y
X
9
16
x
5
X
8
y
6
2
8.5
x
48Deletion
- 1. if z?leftNIL or z?rightNIL
- then y ? z
- else y ? Successor(z)
- 4. if y?left?NIL
- then x ? y?left
- else x ? y?right
- 7. if x ? NIL
- then x?p ? y?p
- 9. if y?p NIL
- 10. then rootT ? x
- else if y (y?p)?left
- then (y?p)?left ? x
- else (y?p)?right ? x
- 14.if y?z
- then keyz ? keyy
- copy ys data into z
- 17.return y
10
z
7
z
14
y
9
16
x
5
8
y
6
2
8.5
x
49Deletion
- 1. if z?leftNIL or z?rightNIL
- then y ? z
- else y ? Successor(z)
- 4. if y?left?NIL
- then x ? y?left
- else x ? y?right
- 7. if x ? NIL
- then x?p ? y?p
- 9. if y?p NIL
- 10. then rootT ? x
- else if y (y?p)?left
- then (y?p)?left ? x
- else (y?p)?right ? x
- 14.if y?z
- then keyz ? keyy
- copy ys data into z
- 17.return y
10
z
8
z
14
y
9
16
x
5
8
y
6
2
8.5
x
50Deletion
- 1. if z?leftNIL or z?rightNIL
- then y ? z
- else y ? Successor(z)
- 4. if y?left?NIL
- then x ? y?left
- else x ? y?right
- 7. if x ? NIL
- then x?p ? y?p
- 9. if y?p NIL
- 10. then rootT ? x
- else if y (y?p)?left
- then (y?p)?left ? x
- else (y?p)?right ? x
- 14.if y?z
- then keyz ? keyy
- copy ys data into z
- 17.return y
10
z
8
z
y
9
16
x
5
y
6
2
8.5
x