








Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!









1
2
For any node x, let y be a node in the^ left tree of x, then key[y]
≤^ key[x]. Likewise, if y is a node in the rightsubtree of x then key[x]
≤key[y].
5/30/2008^3 y[ ]^ y[y]
4
t root 10
leaf (^862)
leaf
leafleaf leaf
5/30/2008^5
Search 10
MinimumMaximum 14 7
MaximumPredecessorSuccessor 16
SuccessorInsertDelete
(^862)
Delete
5/30/2008^6
For any node x, let y be a node in the^ left tree of x, then key[y]
≤^ key[x]. Likewise, if y is a node in the rightsubtree of x then key[x]
≤key[y].
5/30/2008^7 y[ ]^ y[y]
x^7
y
z (^862) For any node x, let y be a node in the y
left tree of x, then key[y]
≤^ key[x]. Likewise, if z is a node in the rightsubtree of x then key[x]
≤key[z].
5/30/2008^8
Preorder(node x) 10
(^ ) If x ≠ NILprint(x) 14 7
print(x)Preorder(x
→left) Preorder(x
→right) 16
5
Preorder(x
→right)
5/30/2008^13
Preorder(node x) 10
If x^ ≠^ NILprint(x) 14 7
print(x)Preorder(x
→left) Preorder(x
→right) 16
5
Preorder(x
→right)
5/30/2008^14
Postorder(node x) 10
If x^ ≠^ NILPostorder(x
→left)
14 7
Postorder(x
left) Postorder(x
→right) print(x) 16
print(x)
(^862)
5/30/2008^15
Postorder(node x) 10
If x^ ≠^ NILPostorder(x
→left)
14 7
Postorder(x
left) Postorder(x
→right) print(x) 16
print(x)
(^862) 2,6,5,8,9,7,16,14,
What is the running 10
g time? 14 7
5/30/2008^17
5/30/2008^18
Minimum(node x)while x^ →
left^ ≠^ NIL 14 7
do x^ ←^ x
→left return x 16
return x
(^862)
5/30/2008^19
Minimum(node x)• while x
→^ left^ ≠^
-^ do x
←^ x→left
Maximum(node x)
(^862)
→^ right^ ≠
-^ do x
←^ x→right
25
The successor of x is the nodewith the smallest key greater 10
with the smallest key greaterthan key[x]. 14 7 x
5/30/2008^26
Successor(node x)if^ right
if x→right
≠^ NIL then return Minimum(x
→right) y^ ←^ x→p 10
y^ x^ p while y≠NIL and x==y
→right x^ ←^ y 14 7
y^ ←^ y→p return y 16
5
9 x (^862)
5/30/2008^27
Successor(node x)if^ right
if x→right
≠^ NIL then return Minimum(x
→right) y^ ←^ x→p 10
y^ x^ p while y≠NIL and x==y
→right x^ ←^ y 14 7
y^ ←^ y→p return y 16
5
9 x (^862)
Search, Minimum, Maximum,S^
ll^ i^ O(h) ti Successor all run in O(h) time,where h is the height of thecorresponding binary search tree.
29
Insert a new node z with key[z]=vinto a tree T. 10
into a tree T. 14 7
9.5z
5/30/2008^30
Insert(T,z)y^ ←^ NIL x 10
x^ ←^ root(T)While x^ ≠
x^14 7
y^ ←^ x if key[z] < key[x] 16
if key[z]^
key[x]then x ←^ x→leftelse x ←^ x→right
else x^ ←^
x→right z→p^ ←^ y 9.5z
5/30/2008^31
Insert(T,z)y^ ←^ NIL y x 10
x^ ←^ root(T)While x^ ≠
x^14 7
y^ ←^ x if key[z] < key[x] 16
if key[z]^
key[x]then x ←^ x→leftelse x ←^ x→right
else x^ ←
x→right z→p^ ←^ y 9.5z
Insert(T,z)y^ ←^ NIL
10
x^ ←^ root(T)While x^ ≠
y^ ←^ x if key[z] < key[x] y
if key[z]^
key[x]then x ←^ x→leftelse x ←^ x→right
y (^862)
else x^ ←^
x→right z→p^ ←^ y 9.5z
5/30/2008^37
Insert(T,z)y^ ←^ NIL
10
x^ ←^ root(T)While x^ ≠
y^ ←^ x if key[z] < key[x] y
if key[z]^
key[x]then x ←^ x→leftelse x ←^ x→right
y (^862)
else x^ ←
x→right z→p^ ←^ y If^ NIL th
t[T] 9.5z
If y = NIL then root[T]
←z else if key[z] < key [y]
5/30/2008^38 then y→left
←^ z else y→right
←^ z
39
z
z^
z
z
(^862) z
(^862) z
5/30/2008^41
XX^862 z
5/30/2008^42
z 14 7
z^16
5
5/30/2008^43
z X^14 7
z X^16
5
→right=NIL
2.^ then y
←^ z
3.^ else y
←^ Successor(z)
≠NIL
10
5.^ then x
←^ y→left
6.^ else x
←^ y→right 14 7
→p^ ←^ y→
p
z z
y 5
←^ x 11 l^ if
(^ )
l ft 16 (^62)
11.^ else if y = (y
→p)→left
12.^ then (y
→p)→left^
←^ x 13 else (y
p)^ right
x
8 y
13.^ else (y
→p)→right
←^ x 14.if y≠z 15 then key[z]
←^ key[y]
z y
5/30/2008^49
15.^ then key[z]
←^ key[y]
16.^ copy y’s data into z17 return y
→right=NIL
2.^ then y
←^ z
3.^ else y
←^ Successor(z)
≠NIL
10
5.^ then x
←^ y→left
6.^ else x
←^ y→right 14 7
→p^ ←^ y→
p
z z
y 5
←^ x 11 l^ if
(^ )
l ft (^16) x (^62)
11.^ else if y = (y
→p)→left
12.^ then (y
→p)→left^
←^ x 13 else (y
p)^ right
x
8 y
13.^ else (y
→p)→right
←^ x 14.if y≠z 15 then key[z]
←^ key[y]
z
x y NIL^
5/30/2008^50
15.^ then key[z]
←^ key[y]
16.^ copy y’s data into z17 return y
x=NIL
→right=NIL
2.^ then y
←^ z
3.^ else y
←^ Successor(z)
≠NIL
10
5.^ then x
←^ y→left
6.^ else x
←^ y→right 14 7
→p^ ←^ y→
p
z z
y 5
←^ x 11 l^ if
(^ )
l ft (^16) x (^62)
11.^ else if y = (y
→p)→left
12.^ then (y
→p)→left^
←^ x 13 else (y
p)^ right
x
8 y
13.^ else (y
→p)→right
←^ x 14.if y≠z 15 then key[z]
←^ key[y]
z
x y x=NIL
5/30/2008^51
15.^ then key[z]
←^ key[y]
16.^ copy y’s data into z17 return y
x NIL
→right=NIL
2.^ then y
←^ z
3.^ else y
←^ Successor(z)
≠NIL
10
5.^ then x
←^ y→left
6.^ else x
←^ y→right X^14 7
→p^ ←^ y→
p
z z
y^ X 5
←^ x 11 l^ if
(^ )
l ft (^16) x X 6 2
11.^ else if y = (y
→p)→left
12.^ then (y
→p)→left^
←^ x 13 else (y
p)^ right
x
X 8 y
13.^ else (y
→p)→right
←^ x 14.if y≠z 15 then key[z]
←^ key[y]
8.5^ x
5/30/2008^52
15.^ then key[z]
←^ key[y]
16.^ copy y’s data into z17 return y
→right=NIL
2.^ then y
←^ z
3.^ else y
←^ Successor(z)
≠NIL
10
5.^ then x
←^ y→left
6.^ else x
←^ y→right 14 7
→p^ ←^ y→
p
z z
y 5
←^ x 11 l^ if
(^ )
l ft (^16) x (^62)
11.^ else if y = (y
→p)→left
12.^ then (y
→p)→left^
←^ x 13 else (y
p)^ right
x
8 y
13.^ else (y
→p)→right
←^ x 14.if y≠z 15 then key[z]
←^ key[y]
8.5^ x
5/30/2008^53
15.^ then key[z]
←^ key[y]
16.^ copy y’s data into z17 return y
→right=NIL
2.^ then y
←^ z
3.^ else y
←^ Successor(z)
≠NIL
10
5.^ then x
←^ y→left
6.^ else x
←^ y→right 14 8
→p^ ←^ y→
p
z z
y 5
←^ x 11 l^ if
(^ )
l ft (^16) x (^62)
11.^ else if y = (y
→p)→left
12.^ then (y
→p)→left^
←^ x 13 else (y
p)^ right
x
8 y
13.^ else (y
→p)→right
←^ x 14.if y≠z 15 then key[z]
←^ key[y]
8.5^ x
5/30/2008^54
15.^ then key[z]
←^ key[y]
16.^ copy y’s data into z17 return y 1. if z→left=NIL or z
→right=NIL
2.^ then y
←^ z
3.^ else y
←^ Successor(z)
≠NIL
10
5.^ then x
←^ y→left
6.^ else x
←^ y→right
8
→p^ ←^ y→
p
z z
y 5
←^ x 11 l^ if
(^ )
l ft (^16) x (^62)
11.^ else if y = (y
→p)→left
12.^ then (y
→p)→left^
←^ x 13 else (y
p)^ right
x
y
13.^ else (y
→p)→right
←^ x 14.if y≠z 15 then key[z]
←^ key[y]
8.5^ x
5/30/2008^55
15.^ then key[z]
←^ key[y]
16.^ copy y’s data into z17 return y