btree - math.ucdavis.edulrademac/sp16_2331/btree.pdf · cse 2331 binary search trees 2 uuuuuuu uuu...
TRANSCRIPT
CSE 2331
Binary Search Trees
5.1
CSE 2331
Binary Search Trees
6
ooooooooooo
KKKKKKKKKK
5
����
��
????
??7
????
??
2 5 8
Binary search tree on (2, 5, 5, 6, 7, 8).
Binary Search Tree Property. Let node y be a descendant of
node x.
• If y is in the left subtree of x, then y.key ≤ x.key;
• If y is in the right subtree of x, then y.key ≥ x.key.
5.2
CSE 2331
Binary Search Trees
2UUUUUUUUUU
5
XXXXXXXXXXXXXX
7
jjjjjjjjjIII
I
5III
I 8
6
Another binary search tree on (2, 5, 5, 6, 7, 8).
Binary Search Tree Property. Let node y be a descendant of
node x.
• If y is in the left subtree of x, then y.key ≤ x.key;
• If y is in the right subtree of x, then y.key ≥ x.key.
5.3
CSE 2331
Binary Search Trees
6
pppppp GGG
G
5��� ===
7===
2 5 8
2LLLLL
5
TTTTTTTTT
7
pppppp ===
5===
8
6
A B
Binary search tree on (2, 5, 5, 6, 7, 8).
5.4
CSE 2331
Binary Search Tree: Exercise
v1
ffffffffffffffffffffff
XXXXXXXXXXXXXXXXXXXXXX
v2
QQQQQQQQQQQv3
hhhhhhhhhhhhhhhhh
v4
{{{{
{CC
CCC
v5
QQQQQQQQQQQ
v6 v7 v8
{{{{
{v9
Assign the following values to the tree nodes so that the tree is a
binary search tree:
7, 12, 14, 15, 18, 22, 25, 27, 30
5.5
CSE 2331
Inorder Tree Walk
6
mmmmmmmmmmm
NNNNNNNNN
5
{{{{
{{CC
CCCC
7
CCCC
CC
2 5 8
procedure InorderTreeWalk(x)
1 if (x 6= NIL) then
2 InorderTreeWalk(x.left);
3 print x.key;
4 InorderTreeWalk(x.right);
5 end
5.6
CSE 2331
Tree Search
15
hhhhhhhhhhhhhhhhhh
PPPPPPPPPP
6
uuuuuuuBB
BBB 18
xxxx
xxFF
FFFF
3
���� 77
778
KKKKKKK 17 20
2 4 13
����
9
5.7
CSE 2331
Tree Search
procedure TreeSearch(x,K)
1 if (x = NIL) or (K = x.key) then
2 return (x);
3 else if (K < x.key) then
4 TreeSearch(x.left, K);
5 else
6 TreeSearch(x.right, K);
7 end
5.8
CSE 2331
Iterative Tree Search
procedure IterativeTreeSearch(x,K)
1 while (x 6= NIL) and (K 6= x.key) do
2 if (K ≤ x.key) then
3 x← x.left;
4 else
5 x← x.right;
6 end
7 end
8 return (x);
5.9
CSE 2331
Complete Binary Tree
hhhhhhhhhhhWWWWWWWWWWW
pppppNNNNN
pppppNNNNN
��� === ��� === ��� === ��� ===
Definition. A complete binary tree is a binary tree where:
• All the internal nodes have EXACTLY two children;
• All the leaves are at the same distance from the root.
5.10
CSE 2331
“Balanced” Binary Search Tree
13
hhhhhhhhhhhhhhh
VVVVVVVVVVVVVV
4
wwwww MMMMMMM 18
mmmmmmmmJJJ
JJ
3
���
8
��� <<
< 15BB
B 20
2 6 9 17
Binary search tree on: (2, 3, 4, 6, 8, 9, 13, 15, 17, 18, 20)
5.11
CSE 2331
“Unbalanced” Binary Search Tree
18
ccccccccccccccccccccccccccccccccccEE
E
4
oooooo
oIII
II 20
2>>
> 6>>
>
3 8PPPPPPP
13||
| EEE
9 15EE
E
17
Binary search tree on: (2, 3, 4, 6, 8, 9, 13, 15, 17, 18, 20)
5.12
CSE 2331
Tree Minimum
15ooooo
TTTTTTTT
12
ggggggggggggg 19yyy LLL
L
4OOO
OOO 18 21RRRRRRR
7�� >>
25yyy
5 8 23
procedure TreeMin(x)
1 while (x.left 6= NIL) do
2 x← x.left;
3 end
4 return (x);
5.13
CSE 2331
Tree Maximum
15ooooo
TTTTTTTT
12
ggggggggggggg 19yyy LLL
L
4OOO
OOO 18 21RRRRRRR
7�� >>
25yyy
5 8 23
procedure TreeMax(x)
1 while (x.right 6= NIL) do
2 x← x.right;
3 end
4 return (x);
5.14
CSE 2331
Inorder
vj
ggggggggggggg
XXXXXXXXXXXXXXXX
ppppp GGGjjjjjjjjj
GGG
�� ==
vi
NNNNN�� ==
��
vk
==
• If node vi is in the subtree rooted at vj .Left, then vi ≺ vj .
• If node vk is in the subtree rooted at vj .Right, then vj ≺ vk.
Note: If node vi is in the subtree rooted at vj .Left and node vk is
in the subtree rooted at vj .Right, then vi ≺ vj ≺ vk.
5.15
CSE 2331
Inorder
v8
ggggggggggggg
XXXXXXXXXXXXXXXXv4
ppppp GGG
v13
jjjjjjjjjGGGv2
�� ==
v5
NNNNN
v10
�� ==
v14
v1 v3v7
��
v9 v11
==v6 v12
• If node vi is in the subtree rooted at vj .Left, then vi ≺ vj .
• If node vk is in the subtree rooted at vj .Right, then vj ≺ vk.
Inorder sequence of vertices: v1, v2, v3, . . . , v14.
5.16
CSE 2331
Inorder
v3
ggggggggggggg
XXXXXXXXXXXXXXXXv12
ppppp GGG
v13
jjjjjjjjjGGGv2
�� ==
v8
NNNNN
v1
�� ==
v6
v10 v5v7
��
v9 v11
==v14 v4
• If node vi is in the subtree rooted at vj .Left, then vi ≺ vj .
• If node vk is in the subtree rooted at vj .Right, then vj ≺ vk.
What is the inorder sequence of vertices?
5.17
CSE 2331
Tree Successor
v3
ggggggggggggg
XXXXXXXXXXXXXXXXv12
ppppp GGG
v13
jjjjjjjjjGGGv2
�� ==
v8
NNNNN
v1
�� ==
v6
v10 v5v7
��
v9 v11
==v14 v4
The successor of node vi is the next node in the inorder sequence.
5.18
CSE 2331
Inorder
15
fffffffffffff
ZZZZZZZZZZZZZZZZZZZ
6ppp
pp EEE29
hhhhhhhhhhJJJ
3�� ==
8OOOOO 22
zz DD31
2 4 13}}
19 25DD
9 26
If T is a binary search tree and all the values at nodes are different,
then nodes in the inorder sequence are ordered by increasing value.
5.19
CSE 2331
Tree Successor
15
fffffffffffff
ZZZZZZZZZZZZZZZZZZZ
6ppp
pp EEE29
hhhhhhhhhhJJJ
3�� ==
8OOOOO 22
zz DD31
2 4 13}}
19 25DD
9 26
If T is a binary search tree and all the values at nodes are different,
then the successor of node vi is the node with smallest value
greater than the value of vi.
5.20
CSE 2331
Tree Successor
v3
ffffffffffffYYYYYYYYYYYYYYY
v12
ooooo GGG
v13
jjjjjjjj GGGv2
�� ??
v8
OOOOO
v1
�� ??
v6
v10 v5v7
��
v9 v11
??v14 v4
Case I. x.right 6= NIL:
Return TreeMin(x.right );
Case II. x.right = NIL:
Return closest ancestor y of x where x is in left subtree of y.
5.21
CSE 2331
Tree Successor
procedure TreeSuccessor(x)
1 if (x.right 6= NIL) then return (TreeMin(x.right));
2 y ← x.parent;
3 while (y 6= NIL) and (x = y.right) do
4 x← y;
5 y ← y.parent;
6 end
7 return (y);
5.22
CSE 2331
Binary Search Trees:
Reporting, Searching and Counting
5.23
CSE 2331
Report All NodesReport all nodes of the following tree:
15
fffffffffffff
ZZZZZZZZZZZZZZZZZZZ
6ppp
pp EEE29
hhhhhhhhhhJJJ
3�� ==
8OOOOO 22
zz DD31
2 4 13}}
19 25DD
9 26
procedure InorderTreeWalk(x)
1 if (x 6= NIL) then2 InorderTreeWalk(x.left);
3 print x.key;
4 InorderTreeWalk(x.right);
5 end
5.24
CSE 2331
Report in Range
Report all nodes of the following tree with keys in range [8, 25]:(Range [8, 25] includes 8 and 25.)
15
fffffffffffff
ZZZZZZZZZZZZZZZZZZZ
6ppp
pp EEE29
hhhhhhhhhhJJJ
3�� ==
8OOOOO 22
zz DD31
2 4 13}}
19 25DD
9 26
5.25
CSE 2331
Report in Range
procedure TreeRangeReport(x, kmin, kmax)
1 if (x 6= NIL) then
2 if (kmin ≤ x.key) then
3 TreeRangeReport(x.left, kmin, kmax);
4 end
5 if (kmin ≤ x.key ≤ kmax) then print x.key;
6 if (x.key ≤ kmax) then
7 TreeRangeReport(x.right, kmin, kmax);
8 end
9 end
5.26
CSE 2331
Report in Range
Report all nodes of the following tree with keys in range[kmin, kmax]:
15
fffffffffffff
ZZZZZZZZZZZZZZZZZZZ
6ppp
pp EEE29
hhhhhhhhhhJJJ
3�� ==
8OOOOO 22
zz DD31
2 4 13}}
19 25DD
9 26
h = tree heightI = number of nodes reported.
Running time:
5.27
CSE 2331
Report in Range: Running Time
procedure TreeRangeReport(x, kmin, kmax)
1 if (x 6= NIL) then
2 if (kmin ≤ x.key) then
3 TreeRangeReport(x.left, kmin, kmax);
4 end
5 if (kmin ≤ x.key ≤ kmax) then print x.key;
6 if (x.key ≤ kmax) then
7 TreeRangeReport(x.right, kmin, kmax);
8 end
9 end
h = tree heightI = number of nodes reported.
Running time:
5.28
CSE 2331
Tree Minimum
20
eeeeeeeeeeeeeeee
ZZZZZZZZZZZZZZZZZZZZZ
8lllllll == 48
nnnnn
2KKK
K 11UUUUUUUUU 37
iiiiiiiii BB
5�� 99 17
nnnnn BB23
PPPPP 42
4 7 13BB
18 29}} BB
14 26 31
procedure TreeMin(x)
1 while (x.left 6= NIL) do
2 x← x.left;
3 end
4 return (x);
5.29
CSE 2331
Tree Minimum Greater Than or Equal to
Find minimum key greater than or equal to 12.
20
eeeeeeeeeeeeeeee
ZZZZZZZZZZZZZZZZZZZZZ
8lllllll == 48
nnnnn
2KKK
K 11UUUUUUUUU 37
iiiiiiiii BB
5�� 99 17
nnnnn BB23
PPPPP 42
4 7 13BB
18 29}} BB
14 26 31
5.30
CSE 2331
Tree Minimum Greater Than or Equal to
function TreeMinGE(T , K)
/* Return node with min key greater than or equal to K */
1 u← NIL;
2 v ← T.root;
3 while (v 6= NIL) do4 if (K ≤ v.key) then5 u← v;
6 v ← v.left;
7 else8 v ← v.right;
9 end
10 end
11 return (u);
5.31
CSE 2331
Tree Minimum Greater Than or Equal to
Find minimum key greater than or equal to 12.
20
eeeeeeeeeeeeeeee
ZZZZZZZZZZZZZZZZZZZZZ
8lllllll == 48
nnnnn
2KKK
K 11UUUUUUUUU 37
iiiiiiiii BB
5�� 99 17
nnnnn BB23
PPPPP 42
4 7 13BB
18 29}} BB
14 26 31
5.32
CSE 2331
Tree Minimum Greater Than or Equal to
Find minimum key greater than or equal to 28.
20
eeeeeeeeeeeeeeee
ZZZZZZZZZZZZZZZZZZZZZ
8lllllll == 48
nnnnn
2KKK
K 11UUUUUUUUU 37
iiiiiiiii BB
5�� 99 17
nnnnn BB23
PPPPP 42
4 7 13BB
18 29}} BB
14 26 31
5.33
CSE 2331
Count Nodes in Range
Count number of nodes with keys in range [6, 42].(Range [6, 42] includes 6 and 42.)
20
eeeeeeeeeeeeeeee
ZZZZZZZZZZZZZZZZZZZZZ
8lllllll == 48
nnnnn
2KKK
K 11UUUUUUUUU 37
iiiiiiiii BB
5�� 99 17
nnnnn BB23
PPPPP 42
4 7 13BB
18 29}} BB
14 26 31
5.34
CSE 2331
Report in Range
procedure TreeRangeReport(x, kmin, kmax)
1 if (x 6= NIL) then
2 if (kmin ≤ x.key) then
3 TreeRangeReport(x.left, kmin, kmax);
4 end
5 if (kmin ≤ x.key ≤ kmax) then print x.key;
6 if (x.key ≤ kmax) then
7 TreeRangeReport(x.right, kmin, kmax);
8 end
9 end
5.35
CSE 2331
Count Nodes Greater Than or Equal to
Count number of nodes with keys greater than or equal to 6.
20
eeeeeeeeeeeeeeee
ZZZZZZZZZZZZZZZZZZZZZ
8lllllll == 48
nnnnn
2KKK
K 11UUUUUUUUU 37
iiiiiiiii BB
5�� 99 17
nnnnn BB23
PPPPP 42
4 7 13BB
18 29}} BB
14 26 31
5.36
CSE 2331
Binary Search Tree: Counting
For each node u in the binary search tree, add a field u.size which
stores the number of nodes in the subtree rooted at u.
20
eeeeeeeeeeeeeeee
ZZZZZZZZZZZZZZZZZZZZZ
8lllllll == 48
nnnnn
2KKK
K 11UUUUUUUUU 37
iiiiiiiii BB
5�� 99 17
nnnnn BB23
PPPPP 42
4 7 13BB
18 29}} BB
14 26 31
5.37
CSE 2331
Compute Size
20
ddddddddddddddddd[[[[[[[[[[[[[[[[[[[[[[
8jjjjjjj CC 48
lllll
2OOOO 11
WWWWWWWWW 37ggggggggg FF
5�� ?? 17
lllll FF 23RRRRR 42
4 7 13 FF 18 29xx FF
14 26 31
procedure TreeComputeSize(x)
1 if (x 6= NIL) then2 TreeComputeSize (x.left);
3 TreeComputeSize (x.right);
4 x.size← 1;
5 if (x.left 6= NIL) then x.size← x.size + x.left.size;
6 if (x.right 6= NIL) then x.size← x.size + x.right.size;
7 end
5.38
CSE 2331
Count Nodes Greater Than or Equal to
Count number of nodes greater than or equal to 6.
2018
eeeeeeeeeeeeeeee
ZZZZZZZZZZZZZZZZZZZZZ
810
lllllll >> 487
nnnnn
24
LLLL 11
5
UUUUUUUUU 376
iiiiiiiii BB
53
�� :: 174
nnnnn BB23
4
PPPPP 421
41
71
132
BB18
129
3
|| BB
141
261
311
5.39
CSE 2331
Count Nodes Greater Than or Equal to
function TreeCountGE(x, K)
/* Return number of nodes with keys greater than or equalto K in subtree rooted at x */
1 count← 0;
2 v ← x;
3 while (v 6= NIL) do4 if (v.key ≥ K) then5 count← count + 1;
6 if (v.right 6= NIL) then count← count + v.right.size;
7 v ← v.left;
8 else9 v ← v.right;
10 end
11 end
12 return (count);
5.40
CSE 2331
Count Nodes Greater Than or Equal to
Count number of nodes greater than or equal to 6.Count number of nodes greater than or equal to 17.
2018
eeeeeeeeeeeeeeee
ZZZZZZZZZZZZZZZZZZZZZ
810
lllllll >> 487
nnnnn
24
LLLL 11
5
UUUUUUUUU 376
iiiiiiiii BB
53
�� :: 174
nnnnn BB23
4
PPPPP 421
41
71
132
BB18
129
3
|| BB
141
261
311
5.41
CSE 2331
Count Nodes Less Than or Equal to
Count number of nodes less than or equal to 42.
2018
eeeeeeeeeeeeeeee
ZZZZZZZZZZZZZZZZZZZZZ
810
lllllll >> 487
nnnnn
24
LLLL 11
5
UUUUUUUUU 376
iiiiiiiii BB
53
�� :: 174
nnnnn BB23
4
PPPPP 421
41
71
132
BB18
129
3
|| BB
141
261
311
5.42
CSE 2331
Count Number of Nodes Less Than or Equal to
function TreeCountLE(x, K)
/* Return number of nodes with keys less than or equal to K
in subtree rooted at x */
1 count← 0;
2 v ← x;
3 while (v 6= NIL) do4 if (v.key ≤ K) then5 count← count + 1;
6 if (v.left 6= NIL) then count← count + v.left.size;
7 v ← v.right;
8 else9 v ← v.left;
10 end
11 end
12 return (count);
5.43
CSE 2331
Count Nodes Less Than or Equal to
Count number of nodes less than or equal to 42.Count number of nodes less than or equal to 16.
2018
eeeeeeeeeeeeeeee
ZZZZZZZZZZZZZZZZZZZZZ
810
lllllll >> 487
nnnnn
24
LLLL 11
5
UUUUUUUUU 376
iiiiiiiii BB
53
�� :: 174
nnnnn BB23
4
PPPPP 421
41
71
132
BB18
129
3
|| BB
141
261
311
5.44
CSE 2331
Count Nodes in Range
Count number of nodes with keys in range [6, 42].(Range [6, 42] includes 6 and 42.)
20
eeeeeeeeeeeeeeee
ZZZZZZZZZZZZZZZZZZZZZ
8lllllll == 48
nnnnn
2KKK
K 11UUUUUUUUU 37
iiiiiiiii BB
5�� 99 17
nnnnn BB23
PPPPP 42
4 7 13BB
18 29}} BB
14 26 31
5.45
CSE 2331
Count Number of Nodes in Range
function TreeRangeCount(x, kmin, kmax)
/* Return number of nodes with keys in range [kmin, kmax] in
subtree rooted at x */
1 v ← x;
2 while (v 6= NIL) and (v.key 6∈ [kmin, kmax]) do
3 if (v.key ≤ kmin) then v ← v.right;
4 else if (v.key ≥ kmax) then v ← v.left;
5 end
6 if (v 6= NIL) then
7 countL ← TreeCountGE(v.left, kmin);
8 countR ← TreeCountLE(v.right, kmax);
9 return (1 + countL + countR);
10 else return (0);
5.46
CSE 2331
Binary Search Trees:
Insertion
5.47
CSE 2331
Tree Insert
15
eeeeeeeeeeeeeeeeeUUUUUUUUU
6mmmmmm
LLLL 18oooo OOOO
3{{{ CCC
8RRRRRR 17 20
2 4 13xxx
9
5.48
CSE 2331
Tree Insert
function LocateParent(T , z)
/* Return future parent of z in tree */
1 y ← NIL;
2 x← T.root;
3 while (x 6= NIL) do
4 y ← x;
5 if (z.key < x.key) then x← x.left;
6 else x← x.right;
7 end
8 return (y);
5.49
CSE 2331
Tree Insert
15
eeeeeeeeeeeeeeeeeUUUUUUUUU
6mmmmmm
LLLL 18oooo OOOO
3{{{ CCC
8RRRRRR 17 20
2 4 13xxx
9
function TreeInsert(T , z)
1 y ← LocateParent(T, z);
2 z.parent← y;
3 if (y = NIL) then T.root← z; /* tree T was empty*/
4 else if (z.key < y.key) then y.left← z;
5 else y.right← z;
5.50
CSE 2331
Tree Insert: Size
1511
eeeeeeeeeeeeeeeeeUUUUUUUUU
67
mmmmmmLLLL 18
3
oooo OOOO
33
{{{ CCC8
3
RRRRRR 171
201
21
41
132
xxx9
1
5.51
CSE 2331
Update Size
1511
eeeeeeeeeeeeeeeeeVVVVVVVVV
67
llllllMMMM 18
3
oooo PPPP
33
zz DD 83
RRRRRR 171
201
21
41
132
www9
1
function InsertAndUpdateSize(T , z)
1 TreeInsert(T , z);
2 z.size← 1;
3 y ← z.parent;
4 while (y 6= NIL) do5 y.size← y.size + 1;
6 y ← y.parent;
7 end
5.52
CSE 2331
Binary Search Trees:
Deletion
5.53
CSE 2331
Tree Delete
20
eeeeeeeeeeeeeeeeeee
YYYYYYYYYYYYYYYYYYYY
8vvv
v 9941
�� ==
255
11TTTTTTTT 37
hhhhhhhhhhhh 46
5 17ppppp ==
23NNNNN
13==
18 29�� ==
14 26 31
5.54
CSE 2331
Tree Delete
20
eeeeeeeeeeeeeeeeeee
YYYYYYYYYYYYYYYYYYYY
8vvv
v 9941
�� ==
255
11TTTTTTTT 37
hhhhhhhhhhhh 46
5 17ppppp ==
23NNNNN
13==
18 29�� ==
14 26 31
5.55
CSE 2331
Tree Delete
20
ddddddddddddddddddd
ZZZZZZZZZZZZZZZZZZZ
8ttt
t << 41~~ @@
2 88 11UUUUUUUU 37
gggggggggggg 46
5 17nnn
nn @@ 23PPP
PP
13 @@ 18 29~~ @@
14 26 31
Case I. Node z has zero children: Delete z.
Case II. Node z has one child: Replace z with its child.
Case III. Node z has two children:
Replace z with its successor y.
Replace y with its right child.
5.56
CSE 2331
Tree Delete: Exercise
Delete node x from the following tree:
4�� XXXXXXXXXXXXX
3 20
x
mmmmmm[[[[[[[[[[[[[[[[[[[[[[[[
9�� BB
42yy EE
7 12 39
eeeeeeeeeeeeeee 48
27RRRRRR
31yy EE
30 35
5.57
CSE 2331
Transplant
p
u
���� 22
22v
���� 22
22
Transplant
=⇒
p
u
���� 22
22v
���� 22
22
function Transplant(T , u, v)
/* Replace subtree rooted at u with subtree rooted at v. */
1 p← u.parent;
2 if (p = NIL) then T.root← v;
3 else if (u = p.left) then p.left← v;
4 else p.right← v;
5 if (v 6= NIL) then v.parent← p;
5.58
CSE 2331
Tree Delete
function TreeDelete(T , z)
1 if (z.left = NIL) then Transplant(T ,z,z.right);
2 else if (z.right = NIL) then Transplant(T ,z,z.left);
3 else
4 y ← TreeMin(z.right) ; /* y is the successor of z */
5 if (y 6= z.right) then
6 Transplant(T , y, y.right);
7 y.right← z.right;
8 y.right.parent← y;
9 end
10 Transplant(T , z, y);
11 y.left← z.left;
12 y.left.parent← y;
13 end
5.59
CSE 2331
Tree Delete
20
eeeeeeeeeeeeeeeeeee
YYYYYYYYYYYYYYYYYYYY
8vvv
v 9941
�� ==
255
11TTTTTTTT 37
hhhhhhhhhhhh 46
5 17ppppp ==
23NNNNN
13==
18 29�� ==
14 26 31
5.60
CSE 2331
Running Time Analysis: TreeDelete
function TreeDelete(T , z)
1 if (z.left = NIL) then Transplant(T ,z,z.right);
2 else if (z.right = NIL) then Transplant(T ,z,z.left);
3 else
4 y ← TreeMin(z.right) ; /* y is the successor of z */
5 if (y 6= z.right) then
6 Transplant(T , y, y.right);
7 y.right← z.right;
8 y.right.parent← y;
9 end
10 Transplant(T , z, y);
11 y.left← z.left;
12 y.left.parent← y;
13 end
5.61
CSE 2331
Tree Delete: Size
2020
eeeeeeeeeeeeeeeeeee
YYYYYYYYYYYYYYYYYYY
88
wwww 88
417
�� <<
22
4411
5
SSSSSSSS 375
hhhhhhhhhhhh 461
51
174
qqqqq <<
234
MMMMM
132
<<18
129
3
�� <<
141
261
311
5.62
CSE 2331
Update Size
2020
ddddddddddddddddddd
ZZZZZZZZZZZZZZZZZZZ
88
tttt << 41
7
~~ @@
22
88 115
UUUUUUUU 375
gggggggggggg 461
51
174
nnnnn @@ 23
4
PPPPP
132
@@ 181
293
~~ @@
141
261
311
function UpdateSize(T , z)
1 while (z 6= NIL) do2 z.size← 1;
3 if (z.left 6= NIL) then z.size← z.size + z.left.size;
4 if (z.right 6= NIL) then z.size← z.size + z.right.size;
5 z ← z.parent;
6 end
5.63
CSE 2331
Tree Delete
function TreeDeleteB(T , z)
1 if (z.left = NIL) then
2 Transplant(T ,z,z.right);
3 UpdateSize (T , z.parent);
4 else if (z.right = NIL) then
5 Transplant(T ,z,z.left);
6 UpdateSize (T , z.parent);
7 else
...
5.64
CSE 2331
Tree Delete (continued)
function TreeDeleteB(T , z)
...
7 else8 y ← TreeMin(z.right) ; /* y is the successor of z */
9 x← y.parent;
10 if (y 6= z.right) then11 Transplant(T , y, y.right);
12 y.right← z.right;
13 y.right.parent← y;
14 end
15 Transplant(T , z, y);
16 y.left← z.left;
17 y.left.parent← y;
18 UpdateSize (T , x);
19 end
5.65
CSE 2331
Balanced Search Trees
5.66
CSE 2331
“Balanced” Binary Search Tree
13
hhhhhhhhhhhhhhh
VVVVVVVVVVVVVV
4
wwwww MMMMMMM 18
mmmmmmmmJJJ
JJ
3
���
8
��� <<
< 15BB
B 20
2 6 9 17
Binary search tree on: (2, 3, 4, 6, 8, 9, 13, 15, 17, 18, 20)
5.67
CSE 2331
“Unbalanced” Binary Search Tree
18
ccccccccccccccccccccccccccccccccccEE
E
4
oooooo
oIII
II 20
2>>
> 6>>
>
3 8PPPPPPP
13||
| EEE
9 15EE
E
17
Binary search tree on: (2, 3, 4, 6, 8, 9, 13, 15, 17, 18, 20)
5.68
CSE 2331
Balanced Binary Search Trees
Balanced Binary Search Trees:
• AVL trees (height balanced trees);
• 2-3 Trees;
• Red-black trees;
• Splay trees (self-adjusting).
5.69
CSE 2331
Right Rotate
13
zzzz
zz ---
9
x
����� ,,
,15
7
����
10
4
��� ))
)
2 5
Right
Rotate on x
=⇒
13
����
� ---
7
���� 22
2215
4
��� ))
)9
x
,,,
2 5 10
5.70
CSE 2331
Right Rotate: Example II
13
zzzz
zz ---
9
����
�
x
,,,
15
7
���� 22
2210
4
��� ))
)8
2 5
Right
Rotate on x
=⇒
13
~~~~
~ ---
7
���� 33
3315
4
��� ))
)9
x
��� ,,
,
2 5 8 10
5.71
CSE 2331
Left Rotate
20
wwww
ww
6
88
88
3
x
��� **
*12
���
2 5 9
Left
Rotate on x
⇐=
20
oooooooooo
3
x
66
66
2 6
��� 88
88
5 12
���
9
5.72
CSE 2331
Rotations
p
x
~~~~
~//
//
y
00
0c
a b
Right Rotate on x
=⇒
Left Rotate on y
⇐=
p
y
���� @@
@@@
a x
11
1
b c
5.73
CSE 2331
Transplant
p
u
���� 22
22v
���� 22
22
Transplant
=⇒
p
u
���� 22
22v
���� 22
22
function Transplant(T , u, v)
/* Replace subtree rooted at u with subtree rooted at v. */
1 p← u.parent;
2 if (p = NIL) then T.root← v;
3 else if (u = p.left) then p.left← v;
4 else p.right← v;
5 if (v 6= NIL) then v.parent← p;
5.74
CSE 2331
Right Rotate
p
x
yyyy 33
3
y
44
c
a b
Right Rotate on x=⇒
p
y
��� EE
EE
a x�� 66
b c
function RightRotate(T , x)
1 y ← x.left;
2 b← y.right;
3 Transplant(T ,x,y);
4 x.left← b;
5 if (b 6= NIL) then b.parent← x;
6 y.right← x;
7 x.parent← y;
5.75
CSE 2331
Left Rotate
p
y
yyyy 33
3
x
��66
c
a b
Left Rotate on x⇐=
p
x
EE
EE
a y
44
b c
function LeftRotate(T , x)
1 y ← x.right;
2 b← y.left;
3 Transplant(T ,x,y);
4 x.right← b;
5 if (b 6= NIL) then b.parent← x;
6 y.left← x;
7 x.parent← y;
5.76
CSE 2331
Rotate Example
Apply rotation to reduce height of tree:
32
llllllllllll00
0
5
��� ==
=== 37
2 16
���
AAAA
A
11 22
��� 00
0
20 29
=⇒
5.77
CSE 2331
Rotate Example
Apply rotation to reduce height of tree:
32
jjjjjjjjjjjjjj00
0
5
���
NNNNNNNNN 37
2 16
}}}}
}00
0
11
��� 00
022
8 14
=⇒
5.78
CSE 2331
Right Rotate: Size
p
x
yyyy 33
3
y
44
c
a b
Right Rotate on x=⇒
p
y
��� EE
EE
a x�� 66
b c
function RightRotate(T , x)
1 y ← x.left;
2 b← y.right;
3 Transplant(T ,x,y);
4 x.left← b;
5 if (b 6= NIL) then b.parent← x;
6 y.right← x;
7 x.parent← y;
5.79