You are on page 1of 38

AVL Tree

• Debjit Mitra
• Jeet Saha
• Soham Das
• Sounava Ghosh
• Suraj Kumar Chouhan
AVL Tree
3

Binary Search Tree


4

Why do we need an AVL


Tree ?
5

AVL Tree AVL Tree – Rotation


6-17

Insertion In AVL Tree


18-32

Deletion In AVL Tree


33-36

Pros And Cons Of AVL Trees


37
AVL Tree

What is an AVL tree?

• Height balanced binary search trees.


• Height difference between heights
of left & right subtrees for every node is
less than or equal to 1.
• Balanced factor = Height of right subtree
– Height of left subtree
• Can be -1,0 or 1 for a node to be
balanced in a Binary Search Tree.
• Can be -1,0 or 1 for all nodes of an AVL
tree.
Binary Search Tree

Binary Search Tree is a node-based binary tree


data structure which has the following properties:-

• The left subtree of a node contains only nodes with


keys lesser than the node’s key.
• The right subtree of a node contains only nodes
with keys greater than the node’s key.
• The left and right subtree each must also be a
binary search tree.
• There shouldn’t be any duplicate nodes
WHY DO WE NEED AN AVL TREE ?

• Almost all the operations in a binary search tree are of order


O(n) where h is the height of the tree.
• If we don't plan our tree property , this height can get as high as
n where n is the number of nodes in BST(skewed tree).
• To guarantee an upper bound of O(log n) for all
these operations, we can use balanced tree.
AVL Tree - Rotation
BALANCED FACTOR = HEIGHT OF RIGHT SUBTREE – HEIGHT OF LEFT SUBTREE
CAN ONLY BE {-1,0,1}

There are four types of rotation :–



Right – Right {RR}

Left – Left {LL}

Right – Left {RL}

Left – Right {LR}
LEFT–LEFT {LL}
BALANCED FACTOR = HEIGHT OF RIGHT SUBTREE – HEIGHT
OF LEFT SUBTREE
CAN ONLY BE {-1,0,1}

• For Example – Z, Y,X


• Assuming- Z > Y >
X
Z

Y
LEFT–LEFT {LL}
BALANCED FACTOR = HEIGHT OF RIGHT SUBTREE – HEIGHT
OF LEFT SUBTREE
CAN ONLY BE {-1,0,1}

• For Example – Z, Y,
• X
Assuming- Z > Y > X
BF=> 2-0 =2
Z

Y
LEFT–LEFT {LL}
BALANCED FACTOR = HEIGHT OF RIGHT SUBTREE – HEIGHT
OF LEFT SUBTREE
CAN ONLY BE {-1,0,1}

• For Example – Z, Y,
• X
Assuming- Z > Y >
X
Z

Y
LEFT–LEFT {LL}
BALANCED FACTOR = HEIGHT OF RIGHT SUBTREE – HEIGHT
OF LEFT SUBTREE
CAN ONLY BE {-1,0,1}

• For Example – Z, Y,
• X
Assuming- Z > Y >
X

BF=> 1-1 = 0
Y

X Z
RIGHT-RIGHT {RR}
BALANCED FACTOR = HEIGHT OF RIGHT SUBTREE – HEIGHT
OF LEFT SUBTREE
CAN ONLY BE {-1,0,1}

• For Example – Z, Y,
• X
Assuming- X < Y < Z

Z
RIGHT-RIGHT {RR}
BALANCED FACTOR = HEIGHT OF RIGHT SUBTREE – HEIGHT
OF LEFT SUBTREE
CAN ONLY BE {-1,0,1}

• For Example – Z, Y,
• X
Assuming- X < Y < Z

BF => 0-2 = -2
X

Z
RIGHT-RIGHT {RR}
BALANCED FACTOR = HEIGHT OF RIGHT SUBTREE – HEIGHT
OF LEFT SUBTREE
CAN ONLY BE {-1,0,1}

• For Example – Z, Y,
• X
Assuming- X < Y < Z

Z
RIGHT-RIGHT {RR}
BALANCED FACTOR = HEIGHT OF RIGHT SUBTREE – HEIGHT
OF LEFT SUBTREE
CAN ONLY BE {-1,0,1}

• For Example – Z, Y,
• X
Assuming- X < Y < Z

BF => 1-1 = 0
Y

X Z
LEFT-RIGHT ROTATION

Double rotations are slightly complex version of


already explained versions of rotations. To
understand them better, we should take note of each
action performed while rotation. Let's first check
how to perform Left-Right rotation. A left-right
rotation is a combination of left rotation followed
by right rotation
RIGHT-LEFT ROTATION

The second type of double rotation is Right-


Left Rotation. It is a combination of right
rotation followed by left rotation.
State Action

A node has been inserted into the right subtree of the left subtree. This makes C an unbalanced node. These
scenarios cause AVL tree to perform left-right rotation.

We first perform the left rotation on the left subtree of C. This makes A, the left subtree of B.

Node C is still unbalanced, however now, it is because of the left-subtree of the left-subtree.

We shall now right-rotate the tree, making B the new root node of this subtree. C now becomes the right subtree of
its own left subtree.

The tree is now balanced.


INSERTION IN AVL TREE

Insertion in AVL tree follows two simple


steps. We Insert the node as we would in a
normal binary search tree and then look for
unbalances. If we come across an unbalance
then we rotate.
PROBLEM-
Construct AVL Tree for the following sequence of numbers- 50, 20, 60, 10, 8, 15, 32, 46, 11, 48

SOLUTION-

Step- 01: Insert 50

50

Tree is Balanced
SOLUTION-

Step- 02: Insert 20

• As 20<50, so insert
50
20 in 50’s left sub
tree.
20

Tree is Balanced
SOLUTION-

Step- 03: Insert 60

• As 60>50, so insert
60 in 50’s right sub 50
tree.
20 60

Tree is Balanced
SOLUTION-

Step- 04: Insert 10

• As 10 < 50, so insert


10 in 50’s left sub 50
tree.

• As 10 < 20, so insert 20 60


10 in 20’s left sub
tree
1
0
Tree is Balanced
SOLUTION-

Step- 05: Insert 8

• As 8 < 50, so insert 8


in 50’s left sub tree. 50
• As 8 < 20, so insert 8
in 20’s left sub tree.
• As 8 < 10, so insert 8 20 60
in 10’s left sub tree

10

Tree is Imbalanced
SOLUTION-

• Find the first imbalanced node on


the path from the newly inserted
node (node 8) to the root node.
• The first imbalanced node is node
20.
• Now, count three nodes from node
20 in the direction of leaf node.
• Then, use AVL tree rotation to
balance the tree.

• Following this, we have-

50 50
RR Rotation
1
20 60 60
0

1
0 8 20

8 Tree is balanced
Tree is Imbalanced
SOLUTION-

Step- 6: Insert 15

• As 15 < 50, so insert


15 in 50’s left sub 50
tree.
• As 15 > 10, so insert
15 in 10’s right sub 10 60
tree.
• As 15 < 20, so insert
15 in 20’s left sub 8 20
tree

15

Tree is Imbalanced
SOLUTION-
• Find the first imbalanced node on
the path from the newly inserted
node (node 15) to the root node.
• The first imbalanced node is node
50.
• Now, count three nodes from node
50 in the direction of leaf node.
• Then, use AVL tree rotation to
balance the tree.

• Following this, we have-

50
20

1
LR Rotation
60 1
0 50
0

8 20
1
8 60
5
1
5
Tree is balanced
Tree is Imbalanced
SOLUTION-
Step- 7: Insert 32

• As 32 > 20, so insert 32


in 20’s right sub tree.
• As 32 < 50, so insert 32 20
in 50’s left sub tree.

10 50

8 15 32 60

Tree is balanced
SOLUTION-

Step- 8: Insert 46

• As 46 > 20, so insert


46 in 20’s right sub
20
tree.
• As 46 < 50, so insert
46 in 50’s left sub
10 50
tree.
• As 46 > 32, so insert
46 in 32’s right sub
tree. 8 15 32 60

46

Tree is balanced
SOLUTION-
Step- 9: Insert 11

• As 11 < 20, so insert


11 in 20’s left sub 20
tree.
• As 11 > 10, so insert
11 in 10’s right sub
tree. 10 50
• As 11 > 15, so insert
11 in 15’s left sub
tree.
8 15 32 60

11 46

Tree is balanced
SOLUTION-
Step- 10: Insert 48

• As 48 > 20, so insert


48 in 20’s right sub
tree. 20
• As 48 < 50, so insert
48 in 50’s left sub tree.
• As 48 > 32, so insert
48 in 32’s right sub 10 50
tree.
• As 48 > 46, so insert
48 in 46’s right sub
tree. 8 15 32 60

11 46

48
Tree is imbalanced
SOLUTION-
• Find the first imbalanced node on
the path from the newly inserted
node (node 15) to the root node.
• The first imbalanced node is node
50.
• Now, count three nodes from node
50 in the direction of leaf node.
• Then, use AVL tree rotation to
balance the tree.

• Following this, we have-

20
20
10 50 LR Rotation
10 50

8 15 32 60
8 15 46 60
11 46
11
32 48
48

Tree is Imbalanced Tree is balanced


SOLUTION-

FINAL BALANCED TREE

• This is the final 20


balanced AVL
tree after
inserting all the 1
50
0
given elements.
1
8 46 60
5

11 32 48

Tree is balanced
DELETION OF A NODE IN AVL TREE

Deletion of a node in AVL tree follows two


simple steps. We delete the node as we
would in a normal binary search tree and
then look for imbalances. If we come
across an imbalance then we rotate.
SOLUTION-

BALANCED TREE

• In this AVL tree


we are now 20
deleting the
node 11.
1
50
0

1
8 46 60
5

11 32 48
SOLUTION-

• After the deletion


of node 11 from
the tree, it would
20
look like this.

1
50
0

1
8 46 60
5

32 48
SOLUTION-
BALANCED TREE
• Normally when we
delete a node , we
need to rotate the 20
tree for balancing but
here after deletion of
node 11, the tree is 1
50
not imbalanced. So 0
this is the final tree
after deleting node
1
11. 8 46 60
5

32 48

Tree is balanced
Pros And Cons Of AVL Trees
Arguments for AVL trees: Arguments against using AVL
trees:
• Search is O(log N) since • Difficult to program & debug;
AVL trees are always more space for balance factor.
balanced. • Asymptotically faster but
• Insertion and deletions are rebalancing costs time.
also O(log n) • Most large searches are done in
• The height balancing adds no database systems on disk and use
more than a constant factor other structures (e.g. 8-trees).
to the speed of insertion. • May be OK to have O(N) for a
single operation if total run time
for many consecutive operations
is fast (e.g. Splay trees).
Thank you

You might also like