Advance Data Structure & Algorithms
Module:2
Advanced Balanced Search Trees
Subject Incharge
Ms. Jayshree R. Sanap
Contents
Advanced Balanced Search Trees
➢ In data structures, search trees are used to store data in a sorted and
hierarchical manner so that searching, insertion, and deletion can be done
efficiently.
➢ A balanced search tree is a special type of tree that automatically maintains
its height as small as possible, even after many insertions and deletions.
This balance ensures that operations always take O(log n) time.
➢ When the data size becomes very large and performance becomes critical (like in
databases and file systems), we use Advanced Balanced Search Trees.
Why do we need them?
● A normal Binary Search Tree (BST) can become skewed (like a linked list)
● This increases time complexity from O(log n) to O(n)
● Balanced trees prevent this problem by restructuring themselves
ADSA
Ms.J. Sanap
Advanced Balanced Search Trees
Problem with Normal BST
Insert these keys in a normal BST: 10, 20, 30, 40
● BST structure:
10
20
30
40 Tree becomes skewed
Search time = O(n)
ADSA
[Link]
Advanced Balanced Search Trees
Balanced Search Tree Solution (AVL Tree)
After automatic balancing:
20
/ \
10 30
40
Height is minimized
Search, Insert, Delete = O(log n)
Tree remains efficient regardless of input order
ADSA
[Link]
Advanced Balanced Search Trees
Common Advanced Balanced Search Trees
● AVL Tree
● Red–Black Tree
● B-Tree
● B+ Tree
● Splay Tree
ADSA
Ms.J. Sanap
A Binary Search Tree (BST)
A Binary Search Tree (BST) is a type of binary tree data structure in which
each node contains a unique key and satisfies a specific ordering property:
● All nodes in the left subtree of a node contain values strictly less than the root
node’s value.
● All nodes in the right subtree of a node contain values strictly greater than the
root node’s value.
ADSA
Ms.J. Sanap
A Binary Search Tree (BST)
A Binary Search Tree (BST) is a type of binary tree data structure in which
each node contains a unique key and satisfies a specific ordering property:
● All nodes in the left subtree of a node contain values strictly less than the root
node’s value.
● All nodes in the right subtree of a node contain values strictly greater than the
root node’s value.
ADSA
Ms.J. Sanap
The Problem with BST: Unbalanced Trees
● A standard BST relies on the order of insertions. While the average search,
insertion, and deletion time is O(logn)
● a BST can become severely unbalanced—or "skewed"—if data is inserted in
sorted or near-sorted order.
●
ADSA
Ms.J. Sanap
The Problem with BST: BST Degeneration
When you insert data into a BST in a sorted (ascending or descending) order, the
tree loses its branching structure and turns into a single long chain, essentially
becoming a Linked List.
● Standard BST Performance: In a balanced tree, operations like searching,
insertion, and deletion take O(logn) time.
● However, in a skewed BST (worst-case), these operations slow down to O(n)
because you have to traverse every node to find an element.
ADSA
Ms.J. Sanap
The solution: Why AVL Tree came to be
The AVL Tree (named after inventors Adelson-Velsky and Landis) was created to
solve this by being a self-balancing BST.
1. Guaranteed Balance: An AVL tree maintains a "Balance Factor" for every node,
ensuring the heights of the left and right subtrees differ by no more than 1.
2. Automatic Rotations: Whenever an insertion or deletion makes the tree
unbalanced, the AVL tree performs specific rotations (LL, RR, LR, or RL) to fix
itself immediately.
3. Optimal Performance: By staying balanced, an AVL tree guarantees that the
height of the tree remains logarithmic. This ensures that the worst-case time
complexity for search, insert, and delete is always O(logn).
ADSA
Ms.J. Sanap
AVL Tree
● An AVL tree is a type of binary search tree used in computer science. It is special
because it keeps itself balanced. This means it makes sure that the tree doesn't
get too tall and skinny, which would make finding things slow.
● An AVL tree defined as a self-balancing Binary Search Tree (BST) where the
difference between heights of left and right subtrees for any node cannot be more
than one.
● The importance of AVL trees in data structures is that they ensure operations like
search, insert, and delete are always fast, even if we do them many times.
● This makes AVL trees very useful in applications like databases and memory
management, where we need to quickly find or update information.
ADSA
Ms.J. Sanap
AVL Tree: Properties of AVL Tree
● Balanced Tree: An AVL tree is always balanced. This means that the heights of
the left and right subtrees of any node differ by at most one.
● Height-Balanced Property: The difference in height (also called the balance
factor) between the left and right subtrees of any node is -1, 0, or 1.
● Self-Balancing: An AVL tree automatically performs rotations to maintain
balance after every insertion and deletion.
● Binary Search Tree Properties: An AVL tree follows the properties of a binary
search tree (BST). This means that for any node, the left subtree contains only
nodes with values less than the node's value, and the right subtree contains only
nodes with values greater than the node's value.
● Rotations: To maintain balance, an AVL tree uses four types of rotations: left
rotation, right rotation, left-right rotation, and right-left rotation.
ADSA
Ms.J. Sanap
AVL Tree: Balance Factor of AVL Tree
● The AVL tree balance factor is a key concept used to maintain the tree's
balanced property.
● In AVL trees, the heights of the two child subtrees of any node differ by no more
than one. This balance is maintained through rotations during insertion and
deletion operations.
● The balancing factor (BF) of a node in an AVL tree is defined as the height
difference between its left and right subtrees.
For a node, the balancing factor is calculated as:
ADSA
Ms.J. Sanap
Example of an AVL Tree
● The balance factors for different nodes are: 12 , 8 , 18 , 5 , 11, 17, 4 . Since all
differences are lies between -1 to +1, so the tree is an AVL tree or Balanced BST.
ADSA
Ms.J. Sanap
Operations on an AVL Tree
● Searching : It is same as normal Binary Search Tree (BST) as an AVL
Tree is always a BST. So we can use the same implementation as BST.
The advantage here is time complexity is O(log n)
● Insertion : It does rotations along with normal BST insertion to make
sure that the balance factor of the impacted nodes is less than or equal
to 1 after insertion
● Deletion : It also does rotations along with normal BST deletion to
make sure that the balance factor of the impacted nodes is less than or
equal to 1 after deletion.
ADSA
Ms.J. Sanap
AVL Tree
➔ Rotations: rotations are designed to restore balance in O(1) time while ensuring
the overall time complexity remains O(log n).
➔ AVL Trees use four cases to rebalance themselves after insertions and deletions:
● Left-Left (LL),
● Right-Right (RR),
● Left-Right (LR)
● Right-Left (RL)
ADSA
Ms.J. Sanap
AVL Tree: Rotating the subtrees (Used in Insertion and Deletion)
Case-1: Left-Left (LL)
● 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.
ADSA
Ms.J. Sanap
AVL Tree: Rotating the subtrees (Used in Insertion and Deletion)
Case-1: Left-Left (LL)
ADSA
Ms.J. Sanap
AVL Tree: Rotating the subtrees (Used in Insertion and Deletion)
Case-1: Left-Left (LL)
ADSA
Ms.J. Sanap
AVL Tree: Rotating the subtrees (Used in Insertion and Deletion)
Case-1: Left-Left (LL)
ADSA
Ms.J. Sanap
AVL Tree: Rotating the subtrees (Used in Insertion and Deletion)
Case-2: Right-Right (RR)
● 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 left rotation.
ADSA
Ms.J. Sanap
AVL Tree: Rotating the subtrees (Used in Insertion and Deletion)
Case-2: Right-Right (RR)
ADSA
Ms.J. Sanap
AVL Tree: Rotating the subtrees (Used in Insertion and Deletion)
Case-2: Right-Right (RR)
ADSA
Ms.J. Sanap
AVL Tree: Rotating the subtrees (Used in Insertion and Deletion)
Case-2: Right-Right (RR)
ADSA
Ms.J. Sanap
AVL Tree: Rotating the subtrees (Used in Insertion and Deletion)
Case-3: Left-Right (LR)
● 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.
ADSA
Ms.J. Sanap
AVL Tree: Rotating the subtrees (Used in Insertion and Deletion)
Case-3: Left-Right (LR)
ADSA
Ms.J. Sanap
AVL Tree: Rotating the subtrees (Used in Insertion and Deletion)
Case-3: Left-Right (LR)
ADSA
Ms.J. Sanap
AVL Tree: Rotating the subtrees (Used in Insertion and Deletion)
Case-3: Left-Right (LR)
ADSA
Ms.J. Sanap
AVL Tree: Rotating the subtrees (Used in Insertion and Deletion)
Case-3: Left-Right (LR)
ADSA
Ms.J. Sanap
AVL Tree: Rotating the subtrees (Used in Insertion and Deletion)
Case-4: Right-Left (RL)
● 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.
ADSA
Ms.J. Sanap
AVL Tree: Rotating the subtrees (Used in Insertion and Deletion)
Case-4: Right-Left (RL)
ADSA
Ms.J. Sanap
AVL Tree: Rotating the subtrees (Used in Insertion and Deletion)
Case-4: Right-Left (RL)
ADSA
Ms.J. Sanap
AVL Tree: Rotating the subtrees (Used in Insertion and Deletion)
Case-4: Right-Left (RL)
ADSA
Ms.J. Sanap
AVL Tree: Rotating the subtrees (Used in Insertion and Deletion)
Case-4: Right-Left (RL)
ADSA
Ms.J. Sanap
AVL Tree: Rotating the subtrees (Used in Insertion and Deletion)
Case-4: Right-Left (RL)
ADSA
Ms.J. Sanap
AVL Tree: Example-1
1. Step-by-Step Construction of the AVL Tree for the
given Sequence 32,50,40,25, 30, 60, 78, 20, 28
ADSA
Ms.J. Sanap
AVL Tree: Example-2
2. Step-by-Step Construction of the AVL Tree for the
given Sequence 21, 26, 30, 9, 4, 14, 28, 18,15,10, 2, 3, 7
ADSA
Ms.J. Sanap
AVL Tree: Example
21, 26, 30, 9, 4, 14, 28, 18,15,10, 2, 3, 7
ADSA
Ms.J. Sanap
AVL Tree: Example
21, 26, 30, 9, 4, 14, 28, 18,15,10, 2, 3, 7
ADSA
Ms.J. Sanap
AVL Tree: Example
21, 26, 30, 9, 4, 14, 28, 18,15,10, 2, 3, 7
ADSA
Ms.J. Sanap
AVL Tree: Example
21, 26, 30, 9, 4, 14, 28, 18,15,10, 2, 3, 7
ADSA
Ms.J. Sanap
AVL Tree: Example
21, 26, 30, 9, 4, 14, 28, 18,15,10, 2, 3, 7
ADSA
Ms.J. Sanap
AVL Tree: Example
21, 26, 30, 9, 4, 14, 28, 18,15,10, 2, 3, 7
ADSA
Ms.J. Sanap
AVL Tree: Example
21, 26, 30, 9, 4, 14, 28, 18,15,10, 2, 3, 7
ADSA
Ms.J. Sanap
AVL Tree: Example
21, 26, 30, 9, 4, 14, 28, 18,15,10, 2, 3, 7
ADSA
Ms.J. Sanap
AVL Tree: Example
21, 26, 30, 9, 4, 14, 28, 18,15,10, 2, 3, 7
ADSA
Ms.J. Sanap
AVL Tree: Example
21, 26, 30, 9, 4, 14, 28, 18,15,10, 2, 3, 7
ADSA
Ms.J. Sanap
AVL Tree: Example
Deletion: 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:
● Perform a standard BST deletion.
● Update the height of the current node.
● Calculate the balance factor of the current node.
● Perform rotations if the node becomes unbalanced.
ADSA
Ms.J. Sanap
AVL Tree: Example:Deletion
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:
● Perform a standard BST deletion.
● Update the height of the current node.
● Calculate the balance factor of the current node.
● Perform rotations if the node becomes unbalanced.
ADSA
Ms.J. Sanap
AVL Tree: Example:Deletion
Standard BST Deletion: Locate and remove the target node using BST rules:
Note: There are certain points that must be kept in mind during a deletion
process.
● If the node to be deleted is a leaf node, it is simply removed from the tree.
● If the node to be deleted has one child node, the child node is replaced with
the node to be deleted simply.
● If the node to be deleted has two child nodes then,
○ Either replace the node with it’s inorder predecessor , i.e, the largest
element of the left subtree.
○ Or replace the node with it’s inorder successor , i.e, the smallest
element of the right subtree.
● Update the height of the current node.
After Deletion, Check Balance Factor (BF) for each node,
If |BF|>1 then Perform one of the four Rotations: Left-Left (LL), Right-Right (RR),
Left-Right (LR), Right-Left (RL).
ADSA
Ms.J. Sanap
AVL Tree: Example:Deletion
ADSA
Ms.J. Sanap
AVL Tree: Example:Deletion
44
ADSA
Ms.J. Sanap
AVL Tree: Example:Deletion
ADSA
Ms.J. Sanap
AVL Tree: Example:Deletion
ADSA
Ms.J. Sanap
AVL Tree: Example:Deletion
ADSA
Ms.J. Sanap
AVL Tree: Example:Deletion
ADSA
Ms.J. Sanap
AVL Tree: Example:Deletion
Delete Node 8, 12, 14
ADSA
Ms.J. Sanap
AVL Tree: Example:Deletion
Step 1:
● The node to be deleted from the tree is 8.
● If we observe it is the parent node of the node 5 and 9.
● Since the node 8 has two children it can be replaced by either of its child
nodes.
ADSA
Ms.J. Sanap
AVL Tree: Example:Deletion
Step 2:
● The node 8 is deleted from the tree.
● As the node is deleted we replace it with either of it’s children nodes.
● Here we replaced the node with the inorder successor , i.e, 9.
● Again we check the balance factor for each node.
ADSA
Ms.J. Sanap
AVL Tree: Example:Deletion
Step 3:
● Now The next element to be deleted is 12.
● If we observe, we can see that the node 12 has a left subtree and a right subtree.
● We again can replace the node by either it’s inorder successor or inorder
predecessor.
● In this case we have replaced it by the inorder successor.
ADSA
Ms.J. Sanap
AVL Tree: Example:Deletion
Step 4:
● The node 12 is deleted from the tree.
● Since we have replaced the node with the inorder successor, the tree structure
looks like shown in the image.
● After removal and replacing check for the balance factor of each node of the tree.
ADSA
Ms.J. Sanap
AVL Tree: Example:Deletion
Step 5:
● The next node to be eliminated is 14.
● It can be seen clearly in the image that 14 is a leaf node.
● Thus it can be eliminated easily from the tree.
ADSA
Ms.J. Sanap
AVL Tree: Example:Deletion
Step 6:
● As the node 14 is deleted, we check the balance factor of all the nodes.
● We can see the balance factor of the node 13 is 2.
● This violates the terms of the AVL tree thus we need to balance it using the
rotation mechanism.
ADSA
Ms.J. Sanap
AVL Tree: Example:Deletion
Step 6:
● As the node 14 is deleted, we check the balance factor of all the nodes.
● We can see the balance factor of the node 13 is 2.
● This violates the terms of the AVL tree thus we need to balance it using the
rotation mechanism.
ADSA
Ms.J. Sanap
AVL Tree: Example:Deletion
Step 7:
● In order to balance the tree, we identify the rotation mechanism to be applied.
● Here we need to use LL Rotation.
● The nodes involved in the rotation is shown as follows.
ADSA
Ms.J. Sanap
AVL Tree: Example:Deletion
Step 8:
● The nodes are rotated and the tree satisfies the conditions of an AVL tree.
● The final structure of the tree is shown as follows.
● We can see all the nodes have their balance factor as ‘0’ , ‘1’ and ‘-1’.
ADSA
Ms.J. Sanap