You are on page 1of 22

# AVL Tree

## Made By : Pulkit Aggarwal 755/IT/12

An AVL Tree (Adelson-Velskii and Landis Tree) is a binary search tree with a balance condition. It must be easy to maintain, and it ensures that the depth of the tree is O (log N).

## Balance Condition for AVL Trees

BST with every node satisfying the property that the heights of left and right subtrees can differ only by one.

Concept
Lets try to insert 6 into the AVL tree below. This would destroy the AVL property of the tree. Then this property has to be restored before the insertion step is considered over.

It turns out that this can always be done with a simple modification to the tree known as rotation. After an insertion, only the nodes that are on the path from the insertion point to the root might have their balance altered. As we follow the path up to the root and update the balancing information there may exist nodes whose new balance violates the AVL condition.

## Rotation AVL Tree Rotations

Lets call the node to be balanced . There are four cases to be considered for a violation: 1) An insertion into left subtree of the left child of . (LL) 2) An insertion into right subtree of the left child of . (LR) 3) An insertion into left subtree of the right child of . (RL) 4) An insertion into right subtree of the right child of . (RR) Case I (LL, RR) (insertion occurs on the outside) is fixed by a single rotation. Case II (RL, LR) (insertion occurs on the inside) is fixed by double rotation.
6

Single Rotation(Left-Left)
Let k2 be the first node on the path up violating AVL balance property.Subtree X has grown an extra level (2 levels deeper than Z now). Y cannot be at the same level as X (k2 then out of balance before insertion) and Y cannot be at the same level as Z (then k1 would be the first to violate).

## Single Rotation (LL)

Single Rotation(Right-Right)
Let k1 be the first node on the path up violating AVL balance property.Subtree Z has grown an extra level (2 levels deeper than X now). Y cannot be at the same level as X (k1 then out of balance before insertion) and Y cannot be at the same level as Z (then k2 would be the first to violate).

10

Double Rotation(Left-Right)
Exactly one of tree B or C is 2 levels deeper than D (unless all empty). To rebalance, k3 cannot be root and a rotation between k1 and k3 was shown not to work. So the only alternative is to place k2 as the new root. This forces k1 to be k2s left child and k3 to be its right child. It also completely determines the locations of all 4 subtrees. AVL balance property is now satisfied. Old height of the tree is restored.

11

12

Double Rotation(Right-Left)
Exactly one of tree B or C is 2 levels deeper than A (unless all empty). To rebalance, k1 cannot be root and a rotation between k1 and k3 was shown not to work. So the only alternative is to place k2 as the new root. This forces k1 to be k3s right child and k2 to be its left child. It also completely determines the locations of all 4 subtrees. AVL balance property is now satisfied. Old height of the tree is restored.

13

14

## Height of the Tree

15

Basic Operations
Searching Insertion Deletion

16

Searching

## Space Complexity : O(n) for Stack

17

Insertion

18

19

Deletion

20

21

Applications
Searching in O(log n) time. Compression of Numerical data.

22