# AVL Tree in Data Structure

Mentor: Shailendra Chauhan
Duration : 00:05:00

## What is AVL Tree in Data Structure?

The AVL Tree is a special type of Binary Search Tree with a balance factor that indicates how the heights of the left and right subtrees differ. During the execution of any operation, AVL Trees immediately acquire the tree's minimum possible height.

## Balance Factor in an AVL Tree in Data Structures

• If any node's balancing factor is 1, the left subtree is one level higher than the right subtree, indicating that the node is left-heavy.
• If any node's balancing factor is 0, the left and right subtrees have the same height. Leaf nodes have no subtrees, hence their balance factor is zero.
• If any node's balancing factor equals -1, the left sub-tree is one level lower than the right sub-tree, indicating that the node is right-heavy.

## AVL Tree Usages

• To index big records in databases.
• For searching big databases.

## Rotations of AVL Tree

AVL tree rotation is a basic process in self-balancing binary search trees, specifically AVL trees. If any node in an AVL tree becomes unbalanced due to operations such as insertion or deletion, certain tree rotations are done to restore balance.

## Types of AVL Rotations

There are four different types of AVL rotations:

1. Left Rotation (LL Rotation)
2. Right Rotation (RR Rotation)
3. Left-Right Rotation (LR Rotation)
4. Right-Left Rotation (RL Rotation)

### Left Rotation (LL Rotation)

In a left rotation, the right child of a node becomes the new root, and the original node becomes the left child of the new root. The left child of the new root becomes the right child of the original node.

### Right Rotation (RR Rotation)

In a right rotation, the left child of a node becomes the new root, while the original node becomes the new root's right child. The right child of the new root becomes the left child of the original node.

### Left-Right Rotation (LR Rotation)

An LR rotation consists of a left rotation followed by a right rotation. It is performed when a node's left subtree has an imbalance to the right and the right subtree of its left child is unbalanced to the left.

### Right-Left Rotation (RL Rotation)

An RL rotation consists of a right rotation followed by a left rotation. It is used when a node's right subtree is imbalanced to the left and the left subtree of its right child is also unbalanced to the right.

## Operations on AVL Trees in Data Structures

Following are the basic operations on AVL Trees in Data Structures:

1. Insertion
2. Deletion
3. Searching

### Insertion

Balances the AVL tree after adding a new node to maintain the AVL property, which assures that the height difference between the left and right subtrees is no more than 1.

### Deletion

Rebalances the AVL tree after removing a node to preserve the AVL attribute, changing node heights as needed.

### Searching

Uses the binary search feature to identify a given key within the AVL tree, moving left or right based on key comparisons until the key is discovered or the search reaches a leaf node.

## Complexity Analysis of AVL Tree Operations

### Space Complexity

AVL Trees have the same space complexity as Binary Search Trees, i.e., O(n), because they do not alter the data.

## Applications of AVL Tree in Data Structure

• Large databases can be efficiently searched through and indexed using this method.
• AVL Trees are used to represent various forms of in-memory collections, including sets and dictionaries.
• Database applications require frequent data lookups yet have fewer insertions and removals.
• Software that requires optimized search.
• It is used in business environments and storytelling games.

## Advantages of AVL Tree in Data Structure

• AVL trees are self-balancing.
• It can't be skewed.
• It delivers faster lookups than Red-Black Trees.
• Better searching efficiency than other trees, such as the binary tree.
• The maximum height is log(n), where n is the total number of nodes in the tree.

## Disadvantages of AVL Tree in Data Structure

• Implementing it is difficult.
• Certain methods have a large constant factor.
• Compared to Red-Black trees, AVL trees are uncommon.
• Because of its rather strict balance, AVL trees require sophisticated insertion and removal methods as more rotations are performed.
• Balancing requires additional processing.
Still have some questions? Let's discuss.