AVL Tree
AVL Tree
Operations on Binary
trees – Tree traversals –
Binary search tree,
Implementation – AVL
tree – Application
What is an AVL Tree?
✓ An AVL tree is a type of binary
search tree.
✓ Named after it's inventors
Adelson, Velskii, and Landis.
✓ AVL trees have the property of
dynamic self-balancing in
addition to all the other
properties of binary search
trees.
Why do we need an
AVL tree in DS?
✓ All the elements are arranged on
one side of the root, which leads
to an increase in the time
complexity of searching an
element and complexity
becomes- O(n) (Worst Case)
✓ To resolve the issues and
decrease the searching time,
AVL trees were invented.
A BST is a data structure composed of
nodes.
It has the following Properties:
1.Each tree has a root node (at the top)
2.The root node has zero, one, or two child nodes
3.Each child node has zero, one, or two child nodes
4.Each node has up to two children
5.For each node, its left descendants are less than the
current node, which is less than the right descendants
AVL trees have an additional properties:
✓ The difference between the depth of right and
left sub-trees cannot be more than one.
✓ This difference is called the balance factor.
Note :
✓ A single right rotation is performed.
✓ This type of rotation is identified when a node has a balanced factor as +2, and its
left-child has a balance factor as +1.
AVL Tree Rotations: Left Rotation
This rotation is performed when a new node is inserted at the right
child of the right subtree.
Note :
✓ A single left rotation is performed.
✓ This type of rotation is identified when a node has a balanced factor as -2, and its
right-child has a balance factor as -1.
AVL Tree Rotations: Right – Left Rotation
This rotation is performed when a new node is inserted at the
right child of the left subtree.
Note :
This rotation is performed when a node has a balance factor as –2, and
its right-child has a balance factor as +1.
AVL Tree Rotations: Left – Right Rotation
This rotation is performed when a new node is inserted at the left
child of the right subtree.
Note:
This rotation is performed when a node has a balance factor as +2, and its left-child
has a balance factor as -1.
Summary of Rotations
If BF(node) = +2 and BF(node -> left-child) = +1,
perform LL rotation.
If BF(node) = -2 and BF(node -> right-child) = 1,
perform RR rotation.