AVL Trees
( Self Balanced Binary Search Trees )
BST Limitations:
• BST is good for shallow Trees.
– i.e if Depth is small , then O(logn),
– Otherwise it is as bad as a linked list O(n) – Worst Case
• If the depth of BST grows, then the following possible solutions can be made,
to make tree work with efficient time complexity.
– Keep BSTs shallow by maintaining ‘Balanced” AVL Trees
– Exploit Most-Recently Used ( MRU ) information Splay Trees
– Reduce Disk Access by increasing branching Factor B-Trees
By M. Varalakshmi, Department of IT, MGIT
Time Complexities of BST:
By M. Varalakshmi, Department of IT, MGIT
Goal:
Develop a data structure that has
Guaranteed O(log n)
worst-case complexity for lookup / Search
AVL Tree
1. AVL Tree is invented by Adelson, Velsky and Landis in 1962.
• Case 1: If BF =0, then the left and right subtrees of are of
equal heights.
• Case 2: If BF = 1, then the left subtree is 1 level higher than
its right subtree in AVL Tree
• Case 3: If BF = -1, then the right subtree is 1 level higher than
its left subtree in AVL Tree
Balance Factors of Each Node:
Heights of Nodes
Balance Factors of Nodes
Operations on AVL tree:
• AVL tree is also a BST, therefore, all the operations are performed in the
same way as they are performed in a binary search tree.
1. Creation
2. Insertion
3. Deletion
4. Traversal
• Searching and traversing do not lead to the violation in property of AVL
tree.
• However, insertion and deletion are the operations which can violate
this property. But can be fixed by performing Rotations on AVL Trees
Why AVL Tree?
1. AVL tree controls height of the BST by not letting it skewed.
2. Time taken for all operations in a BST of height h is O(h).
3. However, it can be extended to O(n) if the BST becomes skewed (i.e.
worst case).
4. By limiting this height to ( log n ), AVL tree imposes an upper bound
on each operation to be O(log n) ,where n is the number of nodes.
AVL Tree Rotations:
• During Insertions / deletion of nodes in AVL tree may cause imbalance, To maintain
balance factor of -1,0,1 after insertions or deletions, AVL tree can apply any of these
rotations.
1. LL – Rotation(Left Rotation)
• This rotation is used when the new node has been inserted
into right subtree and makes the tree unbalanced.
2. RR – Rotation(Right Rotation)
• This rotation is used when the new node is inserted to the
left of left subtree and makes the tree unbalanced.
3. LR – Rotation(Left-Right Rotation)
1. Perform a left rotation on the left child.
2. Perform a right rotation on the original unbalanced node.
4. RL – Rotation(Right-Left Rotation)
1. Perform a right rotation on the right child.
2. Perform a left rotation on the original unbalanced node.
Construct AVL Tree for the following data
21, 26, 30, 9, 4, 14, 28, 18,15,10, 2, 3, 7
Example:
Construct an AVL Tree by inserting numbers from 1 to 8.
Example:
Construct an AVL tree by inserting the following elements in
the given order.
63, 9, 19, 27, 18, 108, 99, 81
Construct an AVL tree having the following elements
H, I, J, B, A, E, C, F, D, G, K, L
2. Insertion in AVL Tree
• Insertion in an AVL tree involves adding a new key while ensuring the tree
remains balanced. After inserting a new node, the balance factor of each node
is checked, and rotations are performed if necessary to maintain the AVL
property.
• Algorithm:
– Perform a standard BST insertion.
– Update the height of the current node.
– Calculate the balance factor of the current node.
– Perform rotations if the node becomes unbalanced.
• Insert key 30 into an AVL tree.
3. Deleting a node in AVL Tree:
• Deletion in an AVL tree involves removing a node and then ensuring the
tree remains balanced. After deleting a node, the balance factor of each
node is checked, and rotations are performed if necessary to maintain the
AVL property.
• Algorithm:
1. Perform a standard BST deletion.
2. Update the height of the current node.
3. Calculate the balance factor of the current node.
4. Perform rotations if the node becomes unbalanced.
Example-2:
Delete Node 55 from the AVL tree shown in the following image.
4. Searching for a node in AVL:
Searching in an AVL tree is similar to searching in a binary search tree (BST). Since
AVL trees are balanced, searching is efficient with a time complexity of O(log n).
Algorithm:
1. Start from the root node.
2. Compare the target key with the key of the current node.
3. If the target key is equal to the current node's key, return the node.
4. If the target key is less than the current node's key, move to the left child.
5. If the target key is greater than the current node's key, move to the right child.
6. Repeat steps 2-5 until the target key is found or the leaf node is reached.
• Example:
Search for key 20 in the AVL tree
Result: Node with key 20 found.
Traversals in AVL Trees
AVL Tree Traversals:
Tree traversal is the process of visiting every node in the tree exactly
once in a systematic order.
The main traversal methods are
1. In-Order Traversal
2. Pre-Order Traversal
3. Post-Order Traversal
1. In-order Traversal
Algorithm:
1. Traverse the left subtree in in-order.
2. Visit the root node.
3. Traverse the right subtree in in-order.
Example:
Consider the following AVL tree
In-order Traversal Result:
10, 20, 25, 30, 40
2. Pre-order Traversal
Algorithm:
1. Visit the root node.
2. Traverse the left subtree in pre-order.
3. Traverse the right subtree in pre-order.
Example:
• Consider the following AVL tree:
Pre-order Traversal Result:
20, 10, 30, 25, 40
3. Post-order Traversal
Algorithm:
1. Traverse the left subtree in post-order.
2. Traverse the right subtree in post-order.
3. Visit the root node.
Example:
Consider the following AVL tree
Post-order Traversal Result:
10, 25, 40, 30, 20
Time Complexities of AVL Trees:
Applications of AVL Tree Data Structure
• AVL tree in DSA are highly efficient and versatile due to their self-
balancing properties, which make them suitable for various
applications where quick data retrieval, insertion, and deletion are
crucial:
1. Database Indexing
2. Memory Management
3. File Systems
4. Network Routers
5. Compiler Design
6. Gaming