Advance Data
Structures &
Unit I
Algorithm
Analysis
II Year I Semester
J. SIVA NAGESWARARAO
STMW
AVL Tree – Creation, Insertion and Deletion Operations & Applications
Height of The Tree
The maximum depth or height of the tree is the number of edges in the tree from the root to the
deepest node.
Detailed Explanation :
Root Node: The root node is the topmost node of a tree. It is the starting point of the tree, and all
other nodes are connected directly or indirectly to it.
Leaf Node: A leaf node is a terminal node — it doesn’t branch out further. It has no left or right
child (in binary trees), or no children at all (in general trees).
Height of a tree: The height of a tree is the length of the longest path from the root node to a leaf
node. It is measured in terms of the number of edges (not nodes) on the path from the root to the
deepest leaf.
AVL Tree
An AVL Tree is a self-balancing Binary Search Tree (BST) / Height Balanced Binary Search Tree
where the difference in height between the left and right subtrees of any node (called the balance
factor) is at most 1.
It was invented by Adelson-Velsky and Landis — hence the name AVL.
Balance Factor Formula:
Balance Factor = Height of Left Subtree – Height of Right Subtree
For every node: Balance Factor should be -1, 0, or +1
If it becomes < -1 or > +1, the tree is unbalanced → requires rotation to rebalance.
J. SIVA NAGESWARARAO | ADVANCE DATA STRUCTURE
🔸 Types of Rotations to Balance AVL Tree:
1. Left Rotation (LL)
2. Right Rotation (RR)
3. Left-Right Rotation (LR)
4. Right-Left Rotation (RL)
1. Left-Left Rotation:
Occurs when a node is inserted into the left subtree of the left child, causing the balance
factor to become more than +1.
Fix: Perform a single right rotation.
J. SIVA NAGESWARARAO | ADVANCE DATA STRUCTURE
2. Right-Right Rotation:
Occurs when a node is inserted into the right subtree of the right child, making the balance
factor less than -1.
Fix: Perform a single right rotation.
3. Left-Right Rotation:
Occurs when a node is inserted into the right subtree of the left child, which disturbs the
balance factor of an ancestor node, making it left-heavy.
Fix: Perform a left rotation on the left child, followed by a right rotation on the node.
J. SIVA NAGESWARARAO | ADVANCE DATA STRUCTURE
4. Right-Left Rotation:
Occurs when a node is inserted into the left subtree of the right child, which disturbs the
balance factor of an ancestor node, making it right-heavy.
Fix: Perform a right rotation on the right child, followed by a left rotation on the node.
J. SIVA NAGESWARARAO | ADVANCE DATA STRUCTURE
J. SIVA NAGESWARARAO | ADVANCE DATA STRUCTURE
✅ Applications of AVL Tree in Data Structures:
1. Databases:
o AVL trees are used in databases to maintain index files, ensuring that searching,
inserting, and deleting records is fast and efficient.
2. Memory Management:
o Used by operating systems and compilers for dynamic memory allocation to
maintain free lists efficiently.
3. Search Operations:
o Useful in situations where frequent search operations are required and performance
is critical (due to its balanced nature).
4. Routing Tables in Networks:
o Helps in maintaining efficient lookup tables for IP routing due to fast access and
update times.
5. File Systems:
o AVL trees are used in some file systems for indexing, where quick search, insert, and
delete operations are needed.
6. Cache Management:
o Used in caching algorithms to keep frequently accessed data easily available with
fast lookup.
7. Multimaps and Multisets in STL:
o Internally, AVL trees or other balanced trees like Red-Black Trees are used to
implement associative containers.
8. Event Simulation Systems:
o AVL trees help maintain the event list in time order, enabling quick updates and
deletions.
9. Data Compression Algorithms:
o AVL trees are used in Huffman coding algorithms to maintain tree structures
efficiently.
Insertion operation in AVL Tree
Algorithm
Step 1: Insert the new element into the tree using Binary search Tree Insertion logic.
Step 2: After insertion, check the Balance Factor of Every Node.
Step 3: If the balance factor of every node is 0 or 1 or -1 then go for next operation.
J. SIVA NAGESWARARAO | ADVANCE DATA STRUCTURE
Step 4: If the Balance Factor of any node is other than 0 or 1 or -1 then that tree is said to be
imbalanced. In this case, perform suitable Rotation to make it balanced and go for next operation.
Example – 1
J. SIVA NAGESWARARAO | ADVANCE DATA STRUCTURE
Deletion operation in AVL Tree
The deletion operation in AVL Tree is similar to deletion operation in BST. But after every deletion
operation, we need to check with the Balance Factor condition. If the tree is balanced after deletion
go for next operation otherwise perform suitable rotation to make the tree Balanced.
1.Standard BST Deletion: First, perform the standard BST deletion operation. This involves finding
the node to be deleted and handling the following cases:
J. SIVA NAGESWARARAO | ADVANCE DATA STRUCTURE
* **Case 1: Node is a leaf node:** Simply remove the node.
* **Case 2: Node has one child:** Replace the node with its child.
* **Case 3: Node has two children:** Find the inorder successor (smallest node in the right
subtree) or inorder predecessor (largest node in the left subtree) of the node to be deleted. Replace
the node with its inorder successor/predecessor and then delete the inorder successor/predecessor
(which will fall into either Case 1 or Case 2).
2. Update Heights: After deleting the node, traverse back up the tree from the deleted node's
parent towards the root, updating the height of each node along the path.
3. Rebalancing: For each node along the path, check its balance factor. If the balance factor is
outside the range of -1 to 1, a rebalancing operation (rotation) is required to restore the AVL
property.
J. SIVA NAGESWARARAO | ADVANCE DATA STRUCTURE
J. SIVA NAGESWARARAO | ADVANCE DATA STRUCTURE
J. SIVA NAGESWARARAO | ADVANCE DATA STRUCTURE