AVL Tree

Made By : Pulkit Aggarwal 755/IT/12

1

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.

2

3 .

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

It turns out that this can always be done with a simple modification to the tree known as rotation. After an insertion. 5 . 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. only the nodes that are on the path from the insertion point to the root might have their balance altered.

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

Single Rotation(Left-Left) Let k2 be the first node on the path up violating AVL balance property. 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) 7 .Subtree X has grown an extra level (2 levels deeper than Z now).

8 .

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).Single Rotation(Right-Right) Let k1 be the first node on the path up violating AVL balance property. 9 .Subtree Z has grown an extra level (2 levels deeper than X now).

10 .

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

12 .

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

14 .

Height of the Tree 15 .

Basic Operations Searching Insertion Deletion 16 .

Searching Time Complexity : O(log n) Space Complexity : O(n) for Stack 17 .

Insertion 18 .

19 .

Deletion 20 .

21 .

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

Sign up to vote on this title
UsefulNot useful