Data Structure Avl Tree
Data Structure Avl Tree
AVL TREE
Submitted by:
Submitted to:
Introduction:
An AVL tree is a balancing binary search tree. It is named after Adelson, Velski and
Landis. It was a first data structure to be invented. In an AVL tree, the heights of
the two child sub-trees of any node differ by at most one; if at any time they differ
by more than one, rebalancing is done to restore this property. Lookup, insertion,
and deletion all take O(log n) time in both the average and worst cases, when n is
the number of nodes in the tree prior to the operation. Insertions and deletions
may require the tree to be rebalanced by one or more tree rotations. The balance
factor of AVL tree could be found by the following formula.
BF=hl-hr
Insertion of AVL tree is similar to Binary Search Tree. After insertion,we have to fix
the AVL tree. A technique called rotation is used to restore the balance of AVL
tree. Following are the rotations.
Left Rotation.
Right Rotation.
Left Right Rotation.
Right Left Rotation.
Left Rotation:
In LL (Left Left) Rotation, the inserted node is in the left sub-tree of the left sub-
tree of node A.
Example:
A
B
A C
C
Right Rotation:
In RR (Right Right) Rotation, the inserted node is in the right sub-tree of right sub-tree of node
A.
Example:
B
C A
Right Rotation
LR Rotation:
In LR (Left Right) Rotation, node is inserted in the right of left sub tree. It makes the tree
unbalanced. It is also known as Double Rotation.
Example: A
A C
B
Left Rotation Right Rotation
B
A B
C C
Left Right Rotation
Example:
A
A
C
C C
Draw an AVL tree from the following nodes:
3,2,1,4,5,6,7.