## CSE Binary Search Trees 5.1

CSE 2331 Binary Search Trees 5.1 CSE 2331 Binary Search Trees 6 KK o o KKK o o o KKK o o o KK ooo 5? 7? ? ??  ??  ??  ?  ? ?  2 5 8 Binary ...
Author: Valerie Long
CSE 2331

Binary Search Trees

5.1

CSE 2331

Binary Search Trees 6 KK o o KKK o o o KKK o o o KK ooo 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 2 UUUUU UUUU U

5 XXXXXX XXXXX XXX j 7 III j j j j I jjjj 5 II 8 II 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 GG p p GG p p p p 5= 7= = ==  = 2 5 8

A

2 LL LLL

5 TTTT TTTT T 7= p p == p p p p 5= 8 == 6 B

Binary search tree on (2, 5, 5, 6, 7, 8).

5.4

CSE 2331

Binary Search Tree: Exercise

v2

fff f f f f fff f f f f fff f f f f f QQQ QQQ QQQ QQ v4 CC { CC { C v7 v6 {{{

v1 XXXXX XXXXX XXXXX XXXXX XX hh h h h hhh h h h v5 hh hhhh QQQ QQQ QQQ QQ v8 {{ { v9 {{

v3

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 m 6 NNN m m NNN mm m m NNN m m m m 5 C 7 C CC C { CC { CC { CC { C { { 2 5 8 procedure InorderTreeWalk(x) 1 2

if (x 6= NIL) then InorderTreeWalk(x.left);

3

print x.key;

4

InorderTreeWalk(x.right);

5

end

5.6

CSE 2331

Tree Search

hh 15 PPPP h h h PPP hhh h h h PPP h hh h h h h 6 18 F B u FF B x x BB uu FF x u x u F B x x uu 37 8 KK 17 20 K 7 KK  77 KK   2 4 13    9

5.7

CSE 2331

Tree Search

procedure TreeSearch(x,K) 1 2 3 4

if (x = NIL) or (K = x.key) then return (x); else if (K < x.key) then TreeSearch(x.left, K);

6

else TreeSearch(x.right, K);

7

end

5

5.8

CSE 2331

Iterative Tree Search

procedure IterativeTreeSearch(x,K) 1 2 3

while (x 6= NIL) and (K 6= x.key) do if (K ≤ x.key) then x ← x.left;

5

else x ← x.right;

6

end

4

7

end

8

return (x);

5.9

CSE 2331

Complete Binary Tree

hhh h h h hhhh h NNN p p NN ppp === ===      

WWWW WWWW WWW NNN p p NN ppp === ===      

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

h 13 VVVVVV h h h h VVVV hh h h h VVVV h hhhh 4 MM 18 JJ m m w M JJJ MMM w mm w m m w M m w m 3 8< 15 B 20 BB   6> >> >> 3 8 PPP PPP P 13 E EE | || 9 15 E EE 17

Binary search tree on: (2, 3, 4, 6, 8, 9, 13, 15, 17, 18, 20) 5.12

CSE 2331

Tree Minimum 15 TTTT o o TTTT ooo 19 LL gg 12 g g g LL g y g y g g y g g gg 4 OOO 18 21 RRR RRRR OOO 7> 25 > y yy 5 8 23 procedure TreeMin(x) 2

while (x.left 6= NIL) do x ← x.left;

3

end

4

return (x);

1

5.13

CSE 2331

Tree Maximum 15 TTTT o o TTTT ooo 19 LL gg 12 g g g LL g y g y g g y g g gg 4 OOO 18 21 RRR RRRR OOO 7> 25 > y yy 5 8 23 procedure TreeMax(x) 2

while (x.right 6= NIL) do x ← x.right;

3

end

4

return (x);

1

5.14

CSE 2331

Inorder



pp p p p ==

ggg g g g g g ggggg GG G vi NNN NN 

vj XXXXXX XXXXXX XXXX jj j j j jjjj =  = vk ==

GG G

• 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

v4 v2 v1



pp p p p == v

3

v8 XXXXXX g g g g XXXXXX g g g v13 g g XXXX gggg GG jjj GGG v14 j v10 j G v5 j jjj NNN v7 v9  == v11 NN == v v6  12

• 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

v12 v2 v10



pp p p p == v

5

v3 XXXXXX g g g g XXXXXX g g g v13 g g XXXX gggg GG jjj GGG v6 j v1 j G v8 j jjj NNN v7 v9  == v11 NN == v v14  4

• 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

v12 v2 v10



pp p p p == v

5

v3 XXXXXX g g g g XXXXXX g g g v13 g g X g X g X g X g GG jj GGG v j j v1 j G v8 6 jjjj NNN v v9  == v11 NN7 == v v14  4

The successor of node vi is the next node in the inorder sequence.

5.18

CSE 2331

Inorder

ff 15 ZZZZZZZZZZ f f f f ZZZZZZZ ff ZZ fffff 6 h 29 JJ E h p h E h p h E J h ppp hhhh 3= 8 OOO 22 D 31 = D O z  O z  2 4 13 19 25 D D }} 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

f 15 ZZZZZZZZZ f f f f ZZZZZZZ fff f f ZZZ f f f hh 29 JJJ p 6 EEE h h p h p h pp hhhh 3= 8 OOO 22 D 31 = D O z O  z 2 4 13 19 25 D D } } 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

v12 v2 v10



oo o o o ?? v5

v3 YYYYYY f f f f YYYYYY f ff f f YYY v13 f f f GGG v8 jj GGG v6 j v1 j j jjj OOO v7 v9  ?? v11 OO ?? v4 v14 

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 x ← y;

4 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 Nodes Report all nodes of the following tree: ZZZZZZZ 15 f f f f ZZZZZZZ ff f f f ZZZZZ f fff h 29 JJ h p 6 EEE h h p h J p pp hhhhh 3= 8 OOO 22 D 31 = D OO zz  2 4 13 19 25 D D } } 9 26

1 2 3 4 5

procedure InorderTreeWalk(x) if (x 6= NIL) then InorderTreeWalk(x.left); print x.key; InorderTreeWalk(x.right); 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.)

f 15 ZZZZZZZZZ f f f f ZZZZZZZ fff f f ZZZ f f f 6 h 29 JJ E h p h E h p h E J ppp hhhhh 3= 8 OOO 22 D 31 = D O z O z  2 4 13 19 25 D D }} 9 26

5.25

CSE 2331

Report in Range

procedure TreeRangeReport(x, kmin , kmax ) 1 2 3

if (x 6= NIL) then if (kmin ≤ x.key) then TreeRangeReport(x.left, kmin , kmax );

4

end

5

if (kmin ≤ x.key ≤ kmax ) then print x.key;

6 7

if (x.key ≤ kmax ) then 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 ]:

f 15 ZZZZZZZZZ f f f f ZZZZZZZ fff f f ZZZ f f f 6 h 29 JJ E h p h E h p E J hh ppp hhhh 3= 8 OOO 22 D 31 = D O z O  z 2 4 13 19 25 D D }} 9 26 h = tree height I = number of nodes reported. Running time: 5.27

CSE 2331

Report in Range: Running Time procedure TreeRangeReport(x, kmin , kmax ) 1 2 3

if (x 6= NIL) then if (kmin ≤ x.key) then TreeRangeReport(x.left, kmin , kmax );

4

end

5

if (kmin ≤ x.key ≤ kmax ) then print x.key;

6 7

if (x.key ≤ kmax ) then TreeRangeReport(x.right, kmin , kmax );

8

end

9

end

h = tree height I = number of nodes reported. Running time: 5.28

CSE 2331

Tree Minimum e 20 ZZZZZZZZZ e e e e e ZZZZZZZ ee e e e e ZZZZZ e ee e 48 l 8 == n l n l l nnn lll 2 KK 11 UUUU ii 37 BB i UUUU i KK i U iiii 59 17 BB 23 PPP 42 n n PP  nnn 4 7 13 B 18 29 B B }} B 14 26 31 procedure TreeMin(x) 2

while (x.left 6= NIL) do x ← x.left;

3

end

4

return (x);

1

5.29

CSE 2331

Tree Minimum Greater Than or Equal to

Find minimum key greater than or equal to 12.

e 20 ZZZZZZZZZ e e e e e ZZZZZZZ eee e e e ZZZZZ e e e e n 48 ll 8 == n l l n nn lll 2 KK 11 UUUU i 37 BB i i UUUU i KK i U iiii 59 17 B 23 PPP 42 n B n P n  P nn 4 7 13 B 18 29 B B }} B 14 26 31

5.30

CSE 2331

Tree Minimum Greater Than or Equal to

1 2 3 4 5 6 7 8 9 10 11

function TreeMinGE(T , K) /* Return node with min key greater than or equal to K u ← NIL; v ← T.root; while (v 6= NIL) do if (K ≤ v.key) then u ← v; v ← v.left; else v ← v.right; end end return (u);

*/

5.31

CSE 2331

Tree Minimum Greater Than or Equal to

Find minimum key greater than or equal to 12.

e 20 ZZZZZZZZZ e e e e e ZZZZZZZ eee e e e ZZZZZ e e e e n 48 ll 8 == n l l n nn lll 2 KK 11 UUUU i 37 BB i i UUUU i KK i U iiii 59 17 B 23 PPP 42 n B n P n  P nn 4 7 13 B 18 29 B B }} B 14 26 31

5.32

CSE 2331

Tree Minimum Greater Than or Equal to

Find minimum key greater than or equal to 28.

e 20 ZZZZZZZZZ e e e e e ZZZZZZZ eee e e e ZZZZZ e e e e n 48 ll 8 == n l l n nn lll 2 KK 11 UUUU i 37 BB i i UUUU i KK i U iiii 59 17 B 23 PPP 42 n B n P n  P nn 4 7 13 B 18 29 B B }} B 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.)

e 20 ZZZZZZZZZ e e e e e ZZZZZZZ eee e e e ZZZZZ e e e e n 48 ll 8 == n l l n nn lll 2 KK 11 UUUU i 37 BB i i UUUU i KK i U iiii 59 17 B 23 PPP 42 n B n P n  P nn 4 7 13 B 18 29 B B }} B 14 26 31

5.34

CSE 2331

Report in Range

procedure TreeRangeReport(x, kmin , kmax ) 1 2 3

if (x 6= NIL) then if (kmin ≤ x.key) then TreeRangeReport(x.left, kmin , kmax );

4

end

5

if (kmin ≤ x.key ≤ kmax ) then print x.key;

6 7

if (x.key ≤ kmax ) then 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.

e 20 ZZZZZZZZZ e e e e e ZZZZZZZ eee e e e ZZZZZ e e e e n 48 ll 8 == n l l n nn lll 2 KK 11 UUUU i 37 BB i i UUUU i KK i U iiii 59 17 B 23 PPP 42 n B n P n  P nn 4 7 13 B 18 29 B B }} B 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.

e 20 ZZZZZZZZZ e e e e e ZZZZZZZ eee e e e ZZZZZ e e e e n 48 ll 8 == n l l n nn lll 2 KK 11 UUUU i 37 BB i i UUUU i KK i U iiii 59 17 B 23 PPP 42 n B n P n  P nn 4 7 13 BB 18 29 BB }} 14 26 31

5.37

CSE 2331

Compute Size dd 20 [[[[[[[[[[[[ d d d d d d [[[[[[[[[ ddddddd d [ 48 d 8 j l C j l j j l j l l jj 2 OO 11 WWWWW 37 F ggg g OO WWWW g g g g g 5 ? 17 23 42 R RRRR l F l l ll 4 7 13 F 18 29 F x 14

1 2 3 4 5 6 7

26

31

procedure TreeComputeSize(x) if (x 6= NIL) then TreeComputeSize (x.left); TreeComputeSize (x.right); x.size ← 1; if (x.left 6= NIL) then x.size ← x.size + x.left.size; if (x.right 6= NIL) then x.size ← x.size + x.right.size; end 5.38

CSE 2331

Count Nodes Greater Than or Equal to

Count number of nodes greater than or equal to 6. 18

ee 20 ZZZZZZZZZZZ e e e e e ZZZZZZZZ 10 7 eee e e e e Z e Z e 8 48 l > n l > n l 4 5 6 nnn llll 2 LL 11 UUUU i 37 BB i i UUUU i LL 3 4 4 1 i U iiii 5: 17 B 23 PP 42 n P B n 1  1 2 1 3 PP nnn 4 7 13 B 18 29 B B 1 B 1 1 | | 14

26

31

5.39

CSE 2331

Count Nodes Greater Than or Equal to

1 2 3 4 5 6 7 8 9 10 11 12

function TreeCountGE(x, K) /* Return number of nodes with keys greater than or equal to K in subtree rooted at x */ count ← 0; v ← x; while (v 6= NIL) do if (v.key ≥ K) then count ← count + 1; if (v.right 6= NIL) then count ← count + v.right.size; v ← v.left; else v ← v.right; end end 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. 18

e 20 ZZZZZZZZZZ e e e e e ZZZZZZZZ 10 7 eeee e e e Z e Z e Z e 8 48 l > n l > n l 4 5 6 nnn llll 2 LL 11 UUUU i 37 BB i i UUUU i LL 3 4 4 1 i U iiii 5: 17 B 23 PP 42 n P B n 1  1 2 1 3 PP nnn 4 7 13 B 18 29 B B 1 B 1 1 | | 14

26

31

5.41

CSE 2331

Count Nodes Less Than or Equal to Count number of nodes less than or equal to 42.

18

e 20 ZZZZZZZZZZ e e e e e ZZZZZZZZ 10 7 eeee e e e Z e Z e Z e 8 48 l > n l > n l 4 5 6 nnn llll 2 LL 11 UUUU i 37 BB i i UUUU i LL 3 4 4 1 i U iiii 5: 17 B 23 PP 42 n P B n 1  1 2 1 3 PP nnn 4 7 13 B 18 29 B B 1 B 1 1 | | 14

26

31

5.42

CSE 2331

Count Number of Nodes Less Than or Equal to

1 2 3 4 5 6 7 8 9 10 11 12

function TreeCountLE(x, K) /* Return number of nodes with keys less than or equal to K in subtree rooted at x */ count ← 0; v ← x; while (v 6= NIL) do if (v.key ≤ K) then count ← count + 1; if (v.left 6= NIL) then count ← count + v.left.size; v ← v.right; else v ← v.left; end end 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. 18

e 20 ZZZZZZZZZZ e e e e e ZZZZZZZZ 10 7 eeee e e e Z e Z e Z e 8 48 l > n l > n l 4 5 6 nnn llll 2 LL 11 UUUU i 37 BB i i UUUU i LL 3 4 4 1 i U iiii 5: 17 B 23 PP 42 n P B n 1  1 2 1 3 PP nnn 4 7 13 B 18 29 B B 1 B 1 1 | | 14

26

31

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.)

e 20 ZZZZZZZZZ e e e e e ZZZZZZZ eee e e e ZZZZZ e e e e n 48 ll 8 == n l l n nn lll 2 KK 11 UUUU i 37 BB i i UUUU i KK i U iiii 59 17 B 23 PPP 42 n B n P n  P nn 4 7 13 B 18 29 B B }} B 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 3

while (v 6= NIL) and (v.key 6∈ [kmin , kmax ]) do 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 countL ← TreeCountGE(v.left, kmin );

7 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

eee 15 UUUUU e e e e e UUUU eee e e e e e e 6 18 OO L m L o m L OO o m L oo mmm 3 CC 8 RRR 17 20 R { C R { R { 2 4 13 x xx 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 y ← x;

4 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 ee 15 UUUUU e e e e e UUUU eeee e e e e e e 6 18 OO L m L o m L OO o m L oo mmm 3 CC 8 RRR 17 20 R { C R R {{ 2 4 13 x xx 9

1 2 3 4 5

function TreeInsert(T , z) y ← LocateParent(T, z); z.parent ← y; if (y = NIL) then T.root ← z; /* tree T was empty*/ else if (z.key < y.key) then y.left ← z; else y.right ← z; 5.50

CSE 2331

Tree Insert: Size

11 eeee 15 UUUUUU e e e e 7 3 e UUU ee e e e e e e 6 18 OO L m L o m L OO o m 3 1 1 L 3 oo mmm 3 8 RRR 17 20 RRR 1 {{{ CCC 1 2 2 4 13 1 xxx 9

5.51

CSE 2331

Update Size 11 ee 15 VVVVV e e e e e e 7 3 VVVV e e e e e e e ee 6 M MMM 3 o 18 PPPP ll 3 1 1 l o l o o ll 3 8 RRR 17 20 RRR 1 zz DD 1 2 2 4 13 1 www 9

1 2 3 4 5 6 7

function InsertAndUpdateSize(T , z) TreeInsert(T , z); z.size ← 1; y ← z.parent; while (y 6= NIL) do y.size ← y.size + 1; y ← y.parent; end 5.52

CSE 2331

Binary Search Trees: Deletion

5.53

CSE 2331

Tree Delete

e 20 YYYYYYYY e e e e e YYYYYYY eeee e e e YYYYY e e eee e 89 41 = v 9 v  = vv 25 11 TTT 46 h 37 h h h TTT 5 h hh h TT h h h h 5 17 23 NNN p == p NN p pp 13 = 18 29 = =  = 14 26 31

5.54

CSE 2331

Tree Delete

e 20 YYYYYYYY e e e e e YYYYYYY eeee e e e YYYYY e e eee e 89 41 = v 9 v  = vv 25 11 TTT 46 h 37 h h h TTT 5 h hh h TT h h h h 5 17 23 NNN p == p NN p pp 13 = 18 29 = =  = 14 26 31

5.55

CSE 2331

Tree Delete dd 20 ZZZZZZZZZ d d d d d d ZZZZZZZ dd d d d d d ZZZ d dd d 8< 41 @ t t ~ tt 28 11 UUU 46 gg 37 g g UUUU g g g U ggggg 5 17 @ 23 PP n PPP n nnn 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:

3

4 XXXXXX XXXXX x XX [[[[[[[[ 20 m [[[[[[[[ m m m [[[[[[[[ mm 9 B 42 E B E y y 7 12 48 ee 39 e e e e e ee eeeeee 27 RRR RRR 31 E E yy 30 35

5.57

CSE 2331

Transplant p u2 22 2

p v2 22 2

Transplant =⇒

u2 22 2

v2 22 2

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 1 2 3 4 5 6 7 8 9 10 11 12 13

function TreeDelete(T , z) if (z.left = NIL) then Transplant(T ,z,z.right); else if (z.right = NIL) then Transplant(T ,z,z.left); else y ← TreeMin(z.right) ; /* y is the successor of z */ if (y 6= z.right) then Transplant(T , y, y.right); y.right ← z.right; y.right.parent ← y; end Transplant(T , z, y); y.left ← z.left; y.left.parent ← y; end 5.59

CSE 2331

Tree Delete

e 20 YYYYYYYY e e e e e YYYYYYY eeee e e e YYYYY e e eee e 89 41 = v 9 v  = vv 25 11 TTT 46 h 37 h h h TTT 5 h hh h TT h h h h 5 17 23 NNN p == p NN p pp 13 = 18 29 = =  = 14 26 31

5.60

CSE 2331

Running Time Analysis: TreeDelete 1 2 3 4 5 6 7 8 9 10 11 12 13

function TreeDelete(T , z) if (z.left = NIL) then Transplant(T ,z,z.right); else if (z.right = NIL) then Transplant(T ,z,z.left); else y ← TreeMin(z.right) ; /* y is the successor of z */ if (y 6= z.right) then Transplant(T , y, y.right); y.right ← z.right; y.right.parent ← y; end Transplant(T , z, y); y.left ← z.left; y.left.parent ← y; end 5.61

CSE 2331

Tree Delete: Size

20 ee 20 YYYYYYYY e e e e e YYYYYY ee e 8 7 e e e Y e Y e Y e Y e Y e e 8 41 < 8 w 8 2 www 5 5  < 1 24 11 SSS 46 hh 37 h h S h S h h 1 4 4 SSS hhhhh 5 17 < 23 MM q MMM < q 2 1 3 qqq 13 < 18 29 < < 1 1  < 1 14 26 31

5.62

CSE 2331

Update Size 20

dd 20 ZZZZZZZZZ d d d d d d ZZZZZZZ dd 8 7 d d d d d Z d Z d Z d d 8 41 @ < t t 2 5 5 1 ~ tt 28 11 UUU 46 gg 37 g g UUUU g g 1 4 4 g U ggggg 5 17 @ 23 PP n PPP n 2 1 3 nnn 13 @ 18 29 @ 1 1 1 ~ 14

1 2 3 4 5 6

26

31

function UpdateSize(T , z) while (z 6= NIL) do z.size ← 1; if (z.left 6= NIL) then z.size ← z.size + z.left.size; if (z.right 6= NIL) then z.size ← z.size + z.right.size; z ← z.parent; end 5.63

CSE 2331

Tree Delete

1 2 3 4 5 6 7

function TreeDeleteB(T , z) if (z.left = NIL) then Transplant(T ,z,z.right); UpdateSize (T , z.parent); else if (z.right = NIL) then Transplant(T ,z,z.left); UpdateSize (T , z.parent); else ...

5.64

CSE 2331

Tree Delete (continued)

7 8 9 10 11 12 13 14 15 16 17 18 19

function TreeDeleteB(T , z) ... else y ← TreeMin(z.right) ; /* y is the successor of z */ x ← y.parent; if (y 6= z.right) then Transplant(T , y, y.right); y.right ← z.right; y.right.parent ← y; end Transplant(T , z, y); y.left ← z.left; y.left.parent ← y; UpdateSize (T , x); end 5.65

CSE 2331

Balanced Search Trees

5.66

CSE 2331

“Balanced” Binary Search Tree

h 13 VVVVVV h h h h VVVV hh h h h VVVV h hhhh 4 MM 18 JJ m m w M JJJ MMM w mm w m m w M m w m 3 8< 15 B 20 BB   6> >> >> 3 8 PPP PPP P 13 E EE | || 9 15 E EE 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

x

zz z z zz

9 ,,   ,   7 10 4

2

 ))) 

13 -15 Right Rotate on x =⇒ 2

13  --   72 15 222 x 4 9 ,, )  )) ,  5

10

5

5.70

CSE 2331

Right Rotate: Example II

x

2

zz z z zz

9 ,,   ,   72 10 222 4 8 )   ))

13 -15 Right Rotate on x =⇒ 2

13 ~ --~ ~ ~~ 73 15 333 x 4 9 )  ))  ,,,   5

8

10

5

5.71

CSE 2331

Left Rotate

ww w w ww 68 88 8 x 3 12 *   **   2 5 9

20 Left Rotate on x ⇐=

20 o o oo o o x o ooo 36

66

6

2 68  888  5 12   9

5.72

CSE 2331

Rotations

p x // ~ ~ // ~ ~~ y c 0

00

a b

p

Right Rotate on x =⇒

Left Rotate on y ⇐=

a

[email protected] @@ @@    b

x

111

c

5.73

CSE 2331

Transplant p u2 22 2

p v2 22 2

Transplant =⇒

u2 22 2

v2 22 2

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 x3 y 33 y yy y c 4 4 a b

1 2 3 4 5 6 7

p Right Rotate on x =⇒ a

y E EE E b

x6  6

c

function RightRotate(T , x) y ← x.left; b ← y.right; Transplant(T ,x,y); x.left ← b; if (b 6= NIL) then b.parent ← x; y.right ← x; x.parent ← y; 5.75

CSE 2331

Left Rotate p y yy y y x6 6  a b

1 2 3 4 5 6 7

p 33 3

c

Left Rotate on x ⇐=

a

xE

EEE

b

y

44

c

function LeftRotate(T , x) y ← x.right; b ← y.left; Transplant(T ,x,y); x.right ← b; if (b 6= NIL) then b.parent ← x; y.left ← x; x.parent ← y; 5.76

CSE 2331

Rotate Example Apply rotation to reduce height of tree:

l 320 l l 00 lll l l l lll 5= 37 =   === 2 16 A AA  AA  11 22  000  20

=⇒

29

5.77

CSE 2331

Rotate Example Apply rotation to reduce height of tree:

j 320 j j j 00 jj j j j j jjjj 5 NN 37 N NNN   NNN 2 16 } 00 } 0 } }} 11 22 0   00 8

=⇒

14

5.78

CSE 2331

Right Rotate: Size p x3 y 33 y yy y c 4 4 a b

1 2 3 4 5 6 7

p Right Rotate on x =⇒ a

y E EE E b

x6  6

c

function RightRotate(T , x) y ← x.left; b ← y.right; Transplant(T ,x,y); x.left ← b; if (b 6= NIL) then b.parent ← x; y.right ← x; x.parent ← y; 5.79