Professional Documents
Culture Documents
Balanced Trees
Balanced Trees
STRUCTURES
UNIT -4
CLASS NOTES
BALANCED TREES
search Tree
Binary
10
"
4 II
c to find element 2
/ ,
4 traversals needed
3
Best case :
oclogcn))
t
2
°
The tree above is BST but it is not efficient
a a
very
BST for
searching
added
If elements randomly then
searching BST
°
are a
,
is efficient than
more an
array
°
If elements are added in order
,
BSI are a slower
implementation of linear
arrays /
lists
can as bad as n -
I for n elements
Balanced BST -
AVL Trees
I
Here the
height of the tree is
always login) (empty
• → -
°
AVL tree : GM Adelson - Velskii , EM Landis (two Russian
mathematicians )
unbatanced ③
¢
(3 -
O)
10
°
Cet ) - C D)
-
4 ca -
th) 11
(C D
- -
(D)
⑦
I
/ (O -
C- I ))
C D)
3
(C -
D -
-
2
AVL Tree
O
O
1
2
11
O
O
l 10
3
ROTATION OF TREES
°
To convert a BST to an AVL Tree
Left rotation rotation (
single rotation) double rotations
right
°
, ,
Left Rotation
an
into subtree
•
Performed when new
key inserted right of
right
child of a node whose balance factor was -
I before
insertion
•
Tree is
heavy on
right side
1) Insert 1
:
2) Insert 2
-
I
3) Insert 3
-
g c- unbalanced
1
rotate left
f -
I
perform
into subtree of of
•
New
key inserted right right child a
node whose balance factor was -
I before insertion
•
Perform LCH as balance factor of I
changed from -
I to
-2
4) Perform Left Rotation
LCD
0
o o
1 3
Rotation
Right
an
Tree is
heavy left side
°
on
1) Insert 3
:
Insert
2) 2
3
O
2
3) Insert l
z ←
unbalanced
"
I
2
rotate
right
0
°
Need to balance
•
Perform 1213) as balance factor of 3
changed from 1 to 2
:
O
O
l 3
Left
Right Rotation
°
Left rotation on the left subtree of r (the one that
becomes unbalanced is r)
followed rotation of r
Then
by right
•
2) Insert 2
2
r -7
3
-
I
left
J
subtree l
of r
f
v
o
2
3) LCD
r →
3
"
I
2
rotate
right
0
4) RCD
:
O
O
l 3
°
We performed LRB)
Rotation
Right Left
left of child of
New node inserted into subtree
right
•
a
becomes unbalanced is r)
2) Insert 2
-2
r -3 l
3
of
a
t
3) 1213)
-
2
r → I
rotate left
f -
I
4) Lcl )
:
O
O
l 3
•
we performed RLCD
INSERTING A NODE INTO AN AVLTREE
Question I
1) Insert I
0
2) Insert 2
-
l
3) Insert 3
-2
f -
i perform
left
2
rotation
0
3
O
O o
I 3
4) Insert 4
-
O -
I 3
5) Insert 5
-2
2
nearest to
←
O newly -
inserted
-2
1 3
node : perform
43)
f -
437 4
5
:
O o
l 4
O O
3 5
6) Insert 6
make -2
child \
new
left , root inserted into
/
Q
of new
root u right subtree
of child
O -
l right
I 4
o -
I 42)
42)
3 5
make
j child 0
right
of 4 's new
6
left child
4
note :
I
we
only
O
performed left
-
2 5 rotation as
elements were
O O O
read in
ascending
6 order
I 3
Question 2
6,5 , 4,3 , 2 I -
make AVL
,
:
I
I
5 RCG )
2
4
52 O
4 6
I I
144 )
3
:
I
O
o
3 G
O O
2 4
2
i
I o
Rls)
3 6
I 0
2 4
T
O left node of
I
3 's new
right
child
3
note : we
only
I 0
performed right
2 rotation as
s
elements were
read
descending
° O in
o
order
I 4 6
Question 3
5
, 6,8 , 3,2 , 4,7 -
make AVL
2
-
5 45 )
f -
l
I 0
5 8
62 O
5 8
l T
Rts)
3
:
2
subtree
I o
rightleft
-
3 of
g
child
O l
2 5
4213)
O
437 Eg Rcb)
) v
5 8
32 4
-
I
O -
2
3 6 ← r
° left subtree
0 I
of
2 4 g right
child
o d RLCG )
RCS ) 46 )
g
66
2 4
78
O
O
o
3
7
O o o O
2 4 6 8
Question 4
Is the
following tree an AVL ?
"
50
o / t o
30 70
o l l ,
ol l o
20 35 60 80
' '
is is to
40
yes
Question 5
2 left subtree of
left node
( / L o
30 70 12150)
-
l l l - i
o l l o
20 35 60 80
°
/ l
15
lo
25 I
-
40
'
④
O
30
-
l / t
o
20 50
l / L
o
l y
- I o
15 25 35 70
\ o
lo o / l o
28 40 Go 80
Question 6
'
13
/
/ \ -
I
10 15
/ L \ O
Of o
11 16
041 \
06
°
it is an AVL
•
insert 7
V z
I 13
right subtree
z / \ - , of left node
to 15
\ / \ 4240)
j
o
o
O 11 16
4/16-1
'
⑤
.
45 ) 13
z I
lol \
-
15 o
1
61 \ 116
'
l l O
'I
s >
°
/
4
RC lo)
I
0 13 -
I
/ \
6 15
( / \ o L o
- 16
S 10
o
1 01 to
4 7 11
°
Perform standard BST deletion of a node w
of for
Let be the
larger height child Cor
equal)
•
y z
any
Rebalance tree
by performing appropriate rotations
°
the on
subtree rooted at 2
°
Rotation depends on one of four possible cases Wrt the
of y and
alignment x, z
and
1 Left left case
y left of left of Rt)
: z a
y :
-
.
right case :
y z
right of
y : LRC 2)
y
.
of and left of
4
Right left case y right : 2 I
y
: RLG)
. -
Question 7
50
,
I \ ,
40 60
\ / \ o o /
30 45 55 → w to be deleted
←
Delete would from
as
you
a
regular BST
.
•
Travel upwards and find first unbalanced node
2 ← Z
Y ) ④
v 1)
V
L o
040
60,11
x
no
⑧ 45
0 /
10
•
is left of and a left of Rt)
y z
y :
40
l l
30 50
Question 8
10
51 420
3
/
Ig 15/125
'
11 is
6
O
10
51 Lab
-
1
! Yo if
°
last
of '
j '
is
Question 9
Delete 4 -
I
ill ,
3 8
/ lo l l ,
/ ,
11
2 4 7
ol il lo
il 6 10 12
°
/
9
-
l
Z
5
y 121 I -
,
left -
left :
3 8 1213)
t 11
x , I "
,
I 2 7
11
°
I o
l ,
/ to
, 10 12
6
01
9
Z
-2 ←
5
right right
-
Y
° / y ←
-
I
45 )
2 8
115
"
°
l l id
I '
11
y
o
l ,
I to
10 12
6
°
I
9
8
°
/ \ i
5 11
' '
01 y
il lo
2 10 12
of
0,1 lj 6
ol
g
Question 10
Delete 80
100
2¥ 450
l l l y
,
50
,
② 120 180
o l y l l l
30g 110 130 200
K
/
125
1170 ) -
2
- Z
f
100
y right ofz
Y left of y
o / \ I f a
50 150
° / lo -
I / \ y
RL ( 100)
30 70 120 ← x 180
o l l l \ o
o l
125
121150)
100
/ L
50 120
1 1 \
30 70 (
lol 150
l l
130 180
I i
200
125
4100)
O
120
I / \ o
100 150
o / Lo l l l -
,
50 110 130 180
0301 to 125
lo lo
200
Question 11
Delete 40 delete ,
¢ 50
' '
O
40 60
i
'
'
30 ¥ sit
←
Z
50
Y 121 \ RC45)
45 60
" t l
,
l
J 30
55
o 1
10
50
l l
30 60
I ' 1
10 45 55