0% found this document useful (0 votes)
13 views

AVLtree

Uploaded by

veenajain1506
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

AVLtree

Uploaded by

veenajain1506
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

DATA STRUCTURES

AVL TREES
OBJECTIVE

“ To understand the concept of AVL Trees.



01
Introduction

02
03 Balance Factor

03 Types of Rotation

04
04 Examples-Insertion in a Binary Search Tree

05

06
05
Introduction
⚫ AVL tree is a self-balancing binary search tree invented by G.M. Adelson-
Velsky and E.M. Landis in 1962.
⚫ The tree is named AVL in honour of its inventors.In an AVL tree, the heights
of the two sub-trees of a node may differ by at most one. Due to this property,
the AVL tree is also known as a height-balanced tree.
⚫ The key advantage of using an AVL tree is that it takes O(log n) time to
perform search, insert, and delete operations in an average case as well as
the worst case because the height of the tree is limited to O(log n) .b-tree) is
equal to the height of the right sub-tree.
⚫ The structure of an AVL tree is the same as that of a binary search tree but
with a little difference.
⚫ In its structure, it stores an additional variable called the BalanceFactor .
Thus, every node has a balance factor associated with it. The balance factor
of a node is calculated by subtracting the height of its right sub-tree from the
height of its left sub-tree.
Balance Factor

A binary search tree in which every node has a balance factor of –1, 0, or 1 is
said to be height balanced. A node with any other balance factor is
considered to be unbalanced and requires rebalancing of the tree.
⚫ Balance factor = Height (left sub-tree) – Height (right sub-tree)
⚫ If the balance factor of a node is 1, then it means that the left sub-tree of
the tree is one level higher than that of the right sub-tree. Such a tree is
therefore called as a left-heavy tree.
⚫ If the balance factor of a node is 0, then it means that the height of the left
sub-tree (longest path in the left sub-tree) is equal to the height of the right
sub-tree.
⚫ If the balance factor of a node is –1, then it means that the left sub-tree of
the tree is one level lower than that of the right sub-tree. Such a tree is
therefore called as a right-heavy tree.
Left Heavy , Right Heavy and Balanced AVL tree
Types of Rotation
There are four types of rebalancing rotations and application of these
rotations depends on the position of the inserted node with reference to the
critical node. The four categories of rotations are:
⚫ LL rotation The new node is inserted in the left sub-tree of the left sub-
tree of the critical node.
⚫ RR rotation The new node is inserted in the right sub-tree of the right
sub-tree of the critical node.
⚫ LR rotation The new node is inserted in the right sub-tree of the left sub-
tree of the critical node.
⚫ RL rotation The new node is inserted in the left sub-tree of the right sub-
tree of the critical node.
AVL tree after inserting 30
AVL tree after inserting 71
LL ROTATION
Example-LL rotation
RR ROTATION
Example-RR rotation
LR ROTATION
Example-LR rotataion
RL rotation
Example 1- Construct an AVL tree by inserting the following elements in the
given order: 63, 9, 19, 27, 18, 108, 99, 81.
Example 2:
• Insert 14, 17, 11, 7, 53, 4, 13 into an empty AVL tree

14

11 17

7 53

4
AVL Tree Example 2:
• Insert 14, 17, 11, 7, 53, 4, 13 into an empty AVL tree

14

7 17

4 11 53

13
AVL Tree Example:
• Now insert 12

14

7 17

4 11 53

13

12
AVL Tree Example:
• Now insert 12

14

7 17

4 11 53

12

13
AVL Tree Example:
• Now the AVL tree is balanced.

14

7 17

4 12 53

11 13
AVL Tree Example:
• Now insert 8

14

7 17

4 12 53

11 13

8
AVL Tree Example:
• Now insert 8

14

7 17

4 11 53

8 12

13
AVL Tree Example:
• Now the AVL tree is balanced.

14

11 17

7 12 53

4 8 13
In Class Exercises

• Build an AVL tree with the following values:


15, 20, 24, 10, 13, 7, 30, 36, 25
15, 20, 24, 10, 13, 7, 30, 36, 25
20

15
15 24
20
10
24

13

20 20

13 24 15 24

10 15 13

10
15, 20, 24, 10, 13, 7, 30, 36, 25

20
13

13 24 10 20

10 15 7 15 24

7 30

13 36

10 20

7 15 30

24 36
15, 20, 24, 10, 13, 7, 30, 36, 25

13 13

10 20 10 20

7 15 30 7 15 24

24 36 30

25 13 25 36

10 24

7 20 30

15 25 36
Remove 24 and 20 from the AVL tree.

13 13

10 24 10 20

7 20 30 7 15 30

15 25 36 25 36

13 13

10 30 10 15

7 15 36 7 30

25 25 36
Probable University Questions
• Construct AVL Tree with the given values.
• Explain the concept of AVL trees
• What is meant by balance factor.
• Explain the different types of AVL tree rotations .
Thank you

You might also like