0% found this document useful (0 votes)
12 views6 pages

AVL Trees

AVL Trees are a type of balanced Binary Search Tree that maintain a height difference of no more than 1 between left and right subtrees to ensure efficient search operations. The balance factor of each node indicates whether the tree is left-heavy, right-heavy, or balanced, which helps in detecting imbalances. When an AVL Tree becomes unbalanced due to insertions or deletions, specific rotations (LL, RR, RL, LR) are used to restore balance.

Uploaded by

Irfan Ul Haq
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)
12 views6 pages

AVL Trees

AVL Trees are a type of balanced Binary Search Tree that maintain a height difference of no more than 1 between left and right subtrees to ensure efficient search operations. The balance factor of each node indicates whether the tree is left-heavy, right-heavy, or balanced, which helps in detecting imbalances. When an AVL Tree becomes unbalanced due to insertions or deletions, specific rotations (LL, RR, RL, LR) are used to restore balance.

Uploaded by

Irfan Ul Haq
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/ 6

AVL Trees: Lecture

Introduction to AVL Trees

A Binary Search Tree (BST) is a simple and efficient way to organize data, but it has a flaw—it
can become unbalanced. An unbalanced BST can degrade performance, leading to slow
searches. To solve this problem, we use AVL Trees, named after their inventors, G. M. Adelson-
Velskii and E. M. Landis.

An AVL Tree ensures balance by maintaining the height difference between left and right
subtrees of any node to no more than 1. This makes search operations much faster compared to
an unbalanced BST.

Why Balancing Matters

Consider two trees storing the same data:

1. Unbalanced Tree: The structure becomes like a linked list. Searching can take many
steps, especially for deeper nodes.
Example: Searching for a node in an unbalanced tree with 1000 elements might take up
to 1000 steps.
2. Balanced AVL Tree: Searches are efficient. In a balanced AVL tree with 1000 elements,
the maximum search steps would be about 10.
Illustration: Refer to Figure 1, where an unbalanced and balanced tree are compared.
The balanced tree requires fewer steps for search operations.

AVL Tree Definition and Balance Factor

An AVL Tree is defined as:

• Either empty, or
• Has two subtrees (left and right) where the height difference ∣HL−HR∣ is at most 1 or
<=1.

Here:
• HL = Height of the left subtree
• HR= Height of the right subtree

Each node in an AVL tree has a balance factor:

• +1 (LH): Left subtree is taller.


• 0 (EH): Both subtrees are equal in height.
• -1 (RH): Right subtree is taller.

These balance factors help detects imbalances. See Figure 2 for examples of balance factors.

Rebalancing AVL Trees

When a tree becomes unbalanced after inserting or deleting a node, we use rotations to restore
balance. Four scenarios can occur:

1. Left of Left (LL)


When both the node and its left subtree are left-heavy, we perform a Right Rotation.
o Example: Refer to Figure 3 for a simple LL case.
2. Right of Right (RR)
Both the node and its right subtree are right-heavy, requiring a Left Rotation.
o Example: See Figure 4 for RR case.
3. Right of Left (RL)
The node is left-heavy, but its left subtree is right-heavy. We use a Left Rotation on the
left subtree, followed by a Right Rotation on the root.
o Example: Refer to Figure 5 for RL case.
4. Left of Right (LR)
The node is right-heavy, but its right subtree is left-heavy. We apply a Right Rotation on
the right subtree, followed by a Left Rotation on the root.
o Example: See Figure 6 for LR case.

You might also like