Binary search trees - PowerPoint PPT Presentation

About This Presentation
Title:

Binary search trees

Description:

Of course, all the leaves nodes have both NIL left field and NIL right field. 12/22/09 ... z. 12/22/09. 8. Tree traversals. 16. 6. 14. 10. 5. 7. 9. 2. 8 ... – PowerPoint PPT presentation

Number of Views:18
Avg rating:3.0/5.0
Slides: 51
Provided by: csMon
Category:

less

Transcript and Presenter's Notes

Title: Binary search trees


1
Binary search trees
  • Binhai Zhu
  • Computer Science Department, Montana State
    University

2
Definition
  • 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.

3
Example
root
10
7
14
16
9
5
leaf
8
6
2
leaf
leaf
leaf
4
Operations
Search
10
7
14
16
9
5
8
6
2
5
Operations
Search Minimum Maximum Predecessor Successor Inser
t Delete
10
7
14
16
9
5
8
6
2
6
Binary 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.
7
Binary 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.
8
Tree traversals
Inorder Preorder Postorder
10
7
14
16
9
5
8
6
2
9
Tree traversals
Inorder(node x) If x ? NIL Inorder(x?left)
print(x) Inorder(x?right)
10
7
14
16
9
5
8
6
2
10
Tree 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
11
Tree 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!)
12
Tree traversals
Preorder(node x) If x ? NIL print(x)
Preorder(x?left) Preorder(x?right)
10
7
14
16
9
5
8
6
2
13
Tree 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
14
Tree traversals
Postorder(node x) If x ? NIL
Postorder(x?left) Postorder(x?right)
print(x)
10
7
14
16
9
5
8
6
2
15
Tree 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
16
Tree traversals
What is the running time?
10
7
14
16
9
5
8
6
2
17
Minimum and Maximum
10
7
14
16
9
5
8
6
2
18
Minimum and Maximum
10
Minimum(node x) while x ? left ? NIL do x
? x?left return x
7
14
16
9
5
8
6
2
19
Minimum 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
20
Search
x
10
7
14
16
9
5
k11?
8
6
2
k6
21
Search
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
22
Search
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
23
Successor
The successor of x is the node with the smallest
key greater than keyx.
10
7
14
x
16
9
5
8
6
2
24
Successor
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
25
Successor
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.
26
Insertion
Insert a new node z with keyzv into a tree T.

10
7
14
16
9
5
9.5
8
6
2
z
27
Insertion
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
28
Insertion
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
29
Insertion
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
30
Insertion
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
31
Insertion
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
32
Insertion
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
33
Insertion
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
34
Insertion
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
35
Deletion
10
z
z
7
14
16
9
5
8
6
2
z
36
Deletion
10
7
14
16
9
5
8
6
2
z
37
Deletion
10
7
14
16
9
5
X
8
6
2
z
38
Deletion
10
z
7
14
16
9
5
8
6
2
39
Deletion
10
X
z
7
14
X
16
9
5
8
6
2
40
Deletion
10
z
7
14
16
9
5
8
6
2
41
Deletion
10
z
7
14
16
9
5
8
6
2
Find the successor of z
42
Deletion
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)
43
Deletion
  • 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
44
Deletion
  • 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
45
Deletion
  • 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
46
Deletion
  • 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
47
Deletion
  • 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
48
Deletion
  • 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
49
Deletion
  • 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
50
Deletion
  • 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
Write a Comment
User Comments (0)
About PowerShow.com