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

AVL Tree

- An AVL tree is a self-balancing binary search tree where the height of the left and right subtrees of every node differs by at most one. - The balance factor of each node, defined as the height of its left subtree minus the height of its right subtree, is used to determine if the tree is balanced. - When an insertion or deletion causes imbalance, rotations are performed to balance the tree. There are left, right, left-right, and right-left rotations to rebalance the tree.

Uploaded by

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

AVL Tree

- An AVL tree is a self-balancing binary search tree where the height of the left and right subtrees of every node differs by at most one. - The balance factor of each node, defined as the height of its left subtree minus the height of its right subtree, is used to determine if the tree is balanced. - When an insertion or deletion causes imbalance, rotations are performed to balance the tree. There are left, right, left-right, and right-left rotations to rebalance the tree.

Uploaded by

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

AVL Tree-

• AVL trees are special kind of binary search trees.


• In AVL trees, height of left subtree and right subtree of every node differs by at most one.
• AVL trees are also called as self-balancing binary search trees.

• It is a binary search tree.


• The difference between height of left subtree and right subtree of every node is at most one.
This tree is not an AVL tree because-
• The difference between height of left subtree and right subtree of root node = 4 – 2 = 2.
• This difference is greater than one.

Balance Factor-

In AVL tree,
• Balance factor is defined for every node.
• Balance factor of a node = Height of its left subtree – Height of its right subtree
AVL Tree Operations-

Like BST Operations, commonly performed operations on AVL tree are-


1. Search Operation
2. Insertion Operation
3. Deletion Operation
After performing any operation on AVL tree, the balance factor of each node is checked.
There are following two cases possible-

Case-01:

• After the operation, the balance factor of each node is either 0 or 1 or -1.
• In this case, the AVL tree is considered to be balanced.
• The operation is concluded.

Case-02:

• After the operation, the balance factor of at least one node is not 0 or 1 or -1.
• In this case, the AVL tree is considered to be imbalanced.
• Rotations are then performed to balance the tree.
AVL Tree Rotations-

Rotation is the process of moving the nodes to make tree balanced.

Kinds of Rotations-
LR Rotation with example 2

State Action

A node B has been inserted into the right subtree of A the left subtree of C, because of which C has
become an unbalanced node having balance factor 2. This case is L R rotation where: Inserted node
is in the right subtree of left subtree of C

As LR rotation = RR + LL rotation, hence RR (anticlockwise) on subtree rooted at A is performed


first. By doing RR rotation, node A, has become the left subtree of B.

After performing RR rotation, node C is still unbalanced, i.e., having balance factor 2, as inserted
node A is in the left of left of C

Now we perform LL clockwise rotation on full tree, i.e. on node C. node C has now become the right
subtree of node B, A is left subtree of B

Balance factor of each node is now either -1, 0, or 1, i.e. BST is balanced now.
RL Rotation with example 2

State Action

A node B has been inserted into the left subtree of C the right subtree of A, because of which A has
become an unbalanced node having balance factor - 2. This case is RL rotation where: Inserted
node is in the left subtree of right subtree of A

As RL rotation = LL rotation + RR rotation, hence, LL (clockwise) on subtree rooted at C is performed


first. By doing RR rotation, node C has become the right subtree of B.

After performing LL rotation, node A is still unbalanced, i.e. having balance factor -2, which is
because of the right-subtree of the right-subtree node A.

Now we perform RR rotation (anticlockwise rotation) on full tree, i.e. on node A. node C has now
become the right subtree of node B, and node A has become the left subtree of B.

Balance factor of each node is now either -1, 0, or 1, i.e., BST is balanced now.
Construct an AVL tree by inserting the following elements in the given
order.
63, 9, 19, 27, 18, 108, 99, 81
Example: Construct an AVL Tree by inserting numbers from 1 to 8.
Construct an AVL tree having the following elements
20 30 80 40 10 60 50 70
Construct an AVL tree having the following elements
H, I, J, B, A, E, C, F, D, G, K, L
1. Insert H, I, J

On inserting the above elements, especially in the case of H, the BST becomes unbalanced as the Balance
Factor of H is -2. Since the BST is right-skewed, we will perform RR Rotation on node H.

The resultant balance tree is:

2. Insert B, A
On inserting the above elements, especially in case of A, the BST becomes unbalanced as the Balance Factor
of H and I is 2, we consider the first node from the last inserted node i.e. H. Since the BST from H is left-
skewed, we will perform LL Rotation on node H.

The resultant balance tree is:

3. Insert E
On inserting E, BST becomes unbalanced as the Balance Factor of I is 2, since if we travel from E to I we find
that it is inserted in the left subtree of right subtree of I, we will perform LR Rotation on node I. LR = RR + LL
rotation

3 a) We first perform RR rotation on node B

The resultant tree after RR rotation is:


3b) We first perform LL rotation on the node I

The resultant balanced tree after LL rotation is:

4. Insert C, F, D
On inserting C, F, D, BST becomes unbalanced as the Balance Factor of B and H is -2, since if we travel from
D to B we find that it is inserted in the right subtree of left subtree of B, we will perform RL Rotation on node
I. RL = LL + RR rotation.

4a) We first perform LL rotation on node E

The resultant tree after LL rotation is:

4b) We then perform RR rotation on node B

The resultant balanced tree after RR rotation is:

5. Insert G
On inserting G, BST become unbalanced as the Balance Factor of H is 2, since if we travel from G to H, we
find that it is inserted in the left subtree of right subtree of H, we will perform LR Rotation on node I. LR = RR
+ LL rotation.

5 a) We first perform RR rotation on node C

The resultant tree after RR rotation is:

5 b) We then perform LL rotation on node H

The resultant balanced tree after LL rotation is:


6. Insert K

On inserting K, BST becomes unbalanced as the Balance Factor of I is -2. Since the BST is right-skewed from
I to K, hence we will perform RR Rotation on the node I.

The resultant balanced tree after RR rotation is:


7. Insert L

On inserting the L tree is still balanced as the Balance Factor of each node is now either, -1, 0, +1. Hence the
tree is a Balanced AVL tree

AVL Tree Properties-

Important properties of AVL tree are-

Property-01:
Maximum possible number of nodes in AVL tree of height H

= 2H+1 – 1

Example-

Maximum possible number of nodes in AVL tree of height-3


= 23+1 – 1
= 16 – 1
= 15
Thus, in AVL tree of height-3, maximum number of nodes that can be inserted = 15.

We can not insert more number of nodes in this AVL tree.

You might also like