# AVL Tree Time Complexity

## What Is an AVL Tree?

Adelson-Velskii and Landis are the people who discovered it, so the name came from their names i.e., AVL. It is commonly referred to as a height binary tree. An AVL tree is one that has one of the following characteristics at each of its nodes.

If a node's longest path in its left subtree is longer than its longest path in its right subtree, the node is said to be "left heavy."

If the longest path in a node's right subtree is one more long than the longest path in its left subtree, the node is said to be "right heavy."

If the longest paths in the right and left subtrees are equal, a node is said to be balanced.

The AVL tree is a height-balanced tree where each node's right and left subtree height differences are either -1, 0 or 1. A factor known as the balance factor keeps the subtrees' height differences apart. As a result, we can define AVL as a balanced binary search tree in which each node's balance factor is either -1, 0 or +1. In this case, the formula follows to determine the balancing factor: ### The AVL Tree's rotations:

If any node of the tree falls out of balance, the necessary rotations are carried out to correct the imbalance. Four different rotational kinds are possible. As follows:

1. LL Rotation: When a new node is added as the left child of the unbalanced node's left subtree.
2. RR Rotation: When a new node is added as the right child of the right subtree of an unbalanced node, this process is known as RR Rotation.
3. LR Rotation: When a new node is added as the left child of an unbalanced node's right subtree.
4. RL Rotation: When a new node is added as the right child of an unbalanced node's left subtree.

## Insertion:

To insert an element into an AVL tree, rotations, calculating the balancing factor, and updating the height following insertion are required. Rotations are constant time processes, as was previously mentioned. Constant time is required for both updating the height and calculating the balance factor. Therefore, traversing the length of the tree, whose height is O (log n), takes a long time.

The best solution is when the tree doesn't need to be structurally changed and the new node can be inserted in its position with no need for rebalancing. As we traverse the height of the AVL Tree in this case, the time complexity will be O(log n).

The worst-case situation is when the tree becomes out of balance after the addition of the new node and rotations are required. The time complexity in this case is also O (log n).

Due to the fact that the average case equals the mean of all possible scenarios, the time complexity of insertion in this scenario is similarly O (log n).

## Deletion:

Similar to insertion, deletion entails traversing the tree, calculating the balancing factor, and updating the height of the tree whenever a particular node is deleted.

The best solution is when there is no need for rebalancing and when the balance of the tree is unaffected by removing a node. The time complexity in this situation will be O(log n) as we traverse the height of the AVL Tree to reach the node that has to be deleted. Due to traversal, the time complexity in this situation is also O (log n).

The worst-case situation is when the tree falls out of balance after a particular node is deleted and rotations are required. The time complexity is O (log n) in this scenario as well because of traversal.

The average time complexity of deletion is also O (log n) because it is the mean of all possible cases or scenarios.

## Traversing and Searching:

In the AVL Tree, traversal is a basic operation that is applied to both insertion and deletion. The traversal operation has an O(log n) time complexity in all three scenarios (the best case, the worst case, and the average case) .

The time complexity in different scenarios for searching an element in the AVL Tree is as follows:

The best solution is when the element that has to be located is the root element. As a result, the element is discovered during the first pass, eliminating the need to traverse the entire tree. As a result, the time complexity becomes O (1).

The worst-case scenario is when we have to traverse the tree's leaf nodes since the element, we're looking for is present there.

## Overview:

OPERATION BEST CASE AVERAGE CASE WORST CASE
Insert O (log n) O (log n) O (log n)
Delete O (log n) O (log n) O (log n)
Search O (1) O (log n) O (log n)
Traversal O (log n) O (log n) O (log n)

### Feedback   