Avl excercise
-
Upload
shipra-swati -
Category
Engineering
-
view
260 -
download
6
Transcript of Avl excercise
AVL Tree
The AVL tree is supposed to be balanced if all nodes of the tree have balance factor of -1, 0
and 1.
The balance factor of the node depends on the difference of the height of left sub-tree and
right sub-tree. Balance factor = Right sub-tree – Left sub-tree
When the balance factor comes out to be different from -1, 0 and 1 then the tree is said to
be unbalanced. In this situation rotations are performed to balance the tree. There are
following type of rotations:
1. LL rotation (Right heavy)
2. RR rotation (Left heavy)
3. LR rotation or Double left (Right sub-tree is left heavy)
RR rotations followed by LL rotation
4. RL rotation or Double Right (Left sub-tree is right heavy)
LL rotations followed by RR rotation
Why -1 is considered??
In AVL every node keeps its height. The height of the tree is
H = max (hl, hr) + 1.
Hl = height of left sub-tree.
Hr = height of right sub-tree.
The height of the tree with a single node is 0. So, the formula of height H should satisfy it.
So, hl and hr are supposed to carry -1 in this type of scenario.
This height needs to be updated with each insertion and deletion.
Question: Insert in AVL Tree:
1. A Z B Y C X
2. A Z B Y C X D W E V F
3. A B C D E F G H I J K
4. A V L T R E I S O K
A Z B Y C X
Insert A
0
Insert Z
1
0
Insert B 2
-1
(RR rotation)
0
Tree is unbalance because of insertion of left child of right sub-tree i.e. right sub-tree is left heavy so
LR rotation is needed, which is RR rotation followed by LL rotation.
A
A
Z
A
Z
B
2
(LL rotation)
1
0
Now the tree needs LL rotation which results in following tree:
0
0 0
A
Z
B
Z
B
A
Insert Y
1
0 -1
0
2
Insert C
0 -2
(RR rotation)
-1
0
Z
B
A
Y
Z
B
A
Y
C
Inserting C makes the tree left heavy so RR rotation is needed which results in following tree
structure:
1
0 0
0 0
Insert X
2
0 -1
(RR rotation)
-1 0
0
Z
B
A Y
C
Z
B
A Y
C
X
Tree is unbalance because of insertion of left child of right sub-tree i.e. right sub-tree is left
heavy so LR rotation is needed, which is RR rotation followed by LL rotation.
2
(LL rotation)
0 1
0 1
0
0
-1 0
0 0 0
Y
B
A C
X
Z
Z
C
B Y
X A
A Z B Y C X D W E V F
Till A Z B Y C X, the procedure of node insertion and tree balancing will be carried out like
previous example.
Afterwards, that is for D W E V F, it is explained below.
Insert D
1
-1 -1
0 -1 0
0
Z
C
B Y
X A
D
Insert W
2
-1 -2
0 -1 0
1
(LL rotation)
0
Insertion of W makes the left sub-tree of X is right heavy. So, it needs RL rotation, which is LL
Rotation followed by RR Rotation.
Above structure is explaining the LL Rotation.
Z
C
B Y
X A
D
W
Here is the application of RR Rotation on the tree structure generated because of LL
Rotation on the unbalanced node.
2
-1 -2
0 -2 0
(RR Rotation)
-1
0
Z
C
B Y
X A
W
D
Here is the tree after the RR Rotation.
1
-1 -1
0 0 0
0 0
Z
C
B Y
W A
D X
Insert E
2
-1 -2 (Left heavy so RR rotation)
0 -1 0
1 0
0
Z
C
B Y
W A
D X
E
Tree after RR Rotation
1
-1 0
0 1 0
0
0 0
Y
C
B W
D A
E X Z
Insert V
2
-1 -1
0 2
(Right heavy so LL rotation) 1 0 0
0
Y
C
B W
D A
E X Z
V
After LL Rotation
1
-1 0
0 0 0
0
0 0 0
Y
C
B W
E A
V X Z
D
Insert F
2
-1 -1
(RR rotation)
0 1 0
-1
0 0 0
0
Insertion of F makes C unbalanced because its Right sub-tree becomes left heavy, so LR
rotation is needed, which is RR rotation followed by LL rotation.
Y
C
B W
E A
V X Z
D
F
RR Rotation is shown in earlier structure.
Here is the explanation of LL Rotation.
2
(LL Rotation)
-1 1
0 0 0
-1 0
0
0 0
W
C
B E
D A
V Y
X Z F
The final balanced tree structure is given in following diagram.
0
0
-1
-1 0
-1 0
0
0 0 0
E
C W
D B
V Y
X Z F A
A B C D E F G H I J K
Insert A
0
Insert B
1
0
Insert C
2
(Right heavy so LL Rotation)
1
0
A
A
B
A
B
C
Here is the tree structure after LL rotation.
0
0 0
Insert D
1
0 1
0
B
C A
B
C A
D
Insert E
2
0 2
(Right heavy so LL Rotation)
1
0
1
0 0
0
0
B
C A
D
E
B
D A
E C
Insert F
2
(Right heavy so LL Rotation)
0 1
1
0
0
The tree needs to go through LL Rotation for balancing.
Following is the tree structure that gets generated because of this rotation.
B
D A
E C
F
0
0 1
0
0
0
Insert G
1
0 2
(Right heavy so LL Rotation) 1
0
0
0
D
E B
F C A
D
E B
F C A
G
0
0 0
0 0 0 0
Insert H
1
0 1
0 0 0 1
0
D
F B
G C A E
D
F B
G C A E
H
Insert I
2
0 2
0 0 0 2
(Right heavy so LL Rotation)
1
0
D
F B
G C A E
H
I
Following is the tree that is obtained in order to balance the tree with the use of LL
Rotation:
1
0 1
0 0 0 0
0 0
D
F B
H C A E
I G
Insert J
2
0 2
(Right heavy so LL Rotation)
0 0 0 1
0 1
0
D
F B
H C A E
I G
J
1
0 0
0 0 0 1
0 0 0
D
H B
I C A F
J G E
Insert K
2
0 1
0 0 0 2
(Right heavy so LL Rotation)
0 0 1
0
D
H B
I C A F
J G E
K
Here is the final tree after the insertion of each desired nodes.
1
0 0
0
0 0 0
0
0 0
D
H B
J
C A F
K
G E
I
A V L T R E I S O K
Insert A
0
Insert V
1
0
Insert L
2
(Right sob-tree is left heavy, So LR Rotation
i.e. RR Rotation followed by LL rotation)
-1
(RR Rotation)
0
A
A
V
A
V
L
2
(LL Rotation)
1
0
0 0
Insert T
1
0 -1
0
A
L
V
L
V A
L
V A
T
Insert R
2
0 -2
(Left heavy so RR Rotation)
-1
0
1
0 0
0 0
L
V A
T
R
L
T A
R V
Insert E
0
1 0
0
0 0
Insert I
-1
2 0
1 0
0
Right heavy, so LL Rotation
L
T A
R V E
L
T A
R V E
I
0
0 0
0 0 0 0
Insert S
1
0 -1
0 0 1 0
0
L
T E
R V I
A
L
T E
R V I A
S
Insert O 1
0 -1
0 0
0 0
0 0
0
Insert K
1 -1
0 1 0 0
0 0 0
L
T
E
R V
I A
S O
L
T E
R V
I A
S O K