0% found this document useful (0 votes)
17 views13 pages

AVL Tree

An AVL tree is a self-balancing binary search tree where the height of the two child subtrees of every node differ by at most one. AVL trees support efficient insertion, deletion, and search operations by ensuring the tree remains balanced after each operation. To balance the tree after insertion or deletion, the balance factor of nodes on the path from the inserted/deleted node to the root is checked, and rotations are performed on the first imbalanced node encountered to balance the tree.

Uploaded by

SK Bhatthi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views13 pages

AVL Tree

An AVL tree is a self-balancing binary search tree where the height of the two child subtrees of every node differ by at most one. AVL trees support efficient insertion, deletion, and search operations by ensuring the tree remains balanced after each operation. To balance the tree after insertion or deletion, the balance factor of nodes on the path from the inserted/deleted node to the root is checked, and rotations are performed on the first imbalanced node encountered to balance the tree.

Uploaded by

SK Bhatthi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

AVL Tree-

AVL tree is 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.

Example-

Following tree is an example of AVL tree-

This tree is an AVL tree because-

• It is a binary search tree.


• The difference between height of left subtree and right subtree of every node is at most
one.

Following tree is not an example of AVL Tree-


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

In AVL tree,

Balance factor of every node is either 0 or 1 or -1.


AVL Tree Operations-

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

1. Search Operation
2. Insertion Operation
3. Deletion Operation

Insertion in AVL Tree-

Insertion Operation is performed to insert an element in the AVL Tree.

To insert an element in the AVL tree, follow the following steps-

• Insert the element in the AVL tree in the same way the insertion is performed in BST.
• After insertion, check the balance factor of each node of the resulting tree.

Now, following two cases are possible-

Case-01:

• After the insertion, the balance factor of each node is either 0 or 1 or -1.
• In this case, the tree is considered to be balanced.
• Conclude the operation.
• Insert the next element if any.

Case-02:

• After the insertion, the balance factor of at least one node is not 0 or 1 or -1.
• In this case, the tree is considered to be imbalanced.
• Perform the suitable rotation to balance the tree.
• After the tree is balanced, insert the next element if any.

Rules To Remember-
Rule-01:

After inserting an element in the existing AVL tree,

• Balance factor of only those nodes will be affected that lies on the path from the newly inserted
node to the root node.

Rule-02:

To check whether the AVL tree is still balanced or not after the insertion,

• There is no need to check the balance factor of every node.


• Check the balance factor of only those nodes that lies on the path from the newly inserted node
to the root node.

Rule-03:

After inserting an element in the AVL tree,

• If tree becomes imbalanced, then there exists one particular node in the tree by balancing which
the entire tree becomes balanced automatically.
• To re balance the tree, balance that particular node.

To find that particular node,

• Traverse the path from the newly inserted node to the root node.
• Check the balance factor of each node that is encountered while traversing the path.
• The first encountered imbalanced node will be the node that needs to be balanced.
To balance that node,

• Count three nodes in the direction of leaf node.


• Then, use the concept of AVL tree rotations to re balance the tree.

PRACTICE PROBLEM BASED ON AVL TREE


INSERTION-
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

Step-02: Insert 20

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


Step-03: Insert 60

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

Step-04: Insert 10

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


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

Step-05: Insert 8

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


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

To balance the tree,


• 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-


Step-06: Insert 15

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


• As 15 > 10, so insert 15 in 10’s right sub tree.
• As 15 < 20, so insert 15 in 20’s left sub tree.
To balance the tree,
• 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-

Step-07: Insert 32

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


• As 32 < 50, so insert 32 in 50’s left sub tree.
Step-08: Insert 46

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


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

Step-09: Insert 11
• As 11 < 20, so insert 11 in 20’s left sub tree.
• As 11 > 10, so insert 11 in 10’s right sub tree.
• As 11 < 15, so insert 11 in 15’s left sub tree.

Step-10: Insert 48

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


• As 48 < 50, so insert 48 in 50’s left sub tree.
• As 48 > 32, so insert 48 in 32’s right sub tree.
• As 48 > 46, so insert 48 in 46’s right sub tree.
To balance the tree,
• Find the first imbalanced node on the path from the newly inserted node (node 48) to the
root node.
• The first imbalanced node is node 32.
• Now, count three nodes from node 32 in the direction of leaf node.
• Then, use AVL tree rotation to balance the tree.

Following this, we have-


This is the final balanced AVL tree after inserting all the given elements.

You might also like