0% found this document useful (0 votes)
56 views

AVL Tree

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views

AVL Tree

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

Basic Tree concept –

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.

To maintain this, an implementation of an AVL will


include an algorithm to rebalance the tree when
adding an additional element.
✓ Worst-case time
complexity of insert and
delete is O(log n).

✓ (where n is the number of


nodes in the AVL tree).

✓ The worst-case space


complexity is O(n).
AVL Insertion Process
Step 1: Insert a new element into the tree using BST's (Binary Search
Tree) insertion logic.
Step 2: After inserting the elements, check the Balance Factor of
each node.
Step 3: When every node's balance factor is 0 or 1, or -1, the
algorithm will proceed to the next operation.
Step 4: If the balance factor of any node is not in the above three
values, the tree is imbalanced.
Then perform the suitable Rotation to make it balanced, and the
algorithm will proceed to the next operation.
Left or Right rotations
✓ an imbalance in the left child's right sub-tree,
do left-right rotation
✓ an imbalance in the left child's left sub-tree,
do right rotation
✓ an imbalance in the right child's right sub-tree,
do left rotation
✓ an imbalance in the right child's left sub-tree,
do right-left rotation
AVL Tree Rotations: Right Rotation
This rotation is performed when a new node is inserted at the
left child of the left subtree.

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.

If BF(node) = -2 and BF(node -> right-child) = +1,


perform RL rotation.

If BF(node) = +2 and BF(node -> left-child) = -1,


perform LR rotation.
Insertion in AVL Trees
✓ Insert operation is almost the same as in simple binary search trees.
Insertion in AVL Trees
✓ Insert the node in the AVL tree, insert 160.
✓ Once the node is added, the balance factor of each node is
updated. After 160 is inserted, the balance factor of every
node is updated.
✓ Now check if any node violates the range of the balance
factor if the balance factor is violated, then perform
rotations.
In the above example, the balance factor of 350 is violated and
case 1 becomes applicable there, perform LL rotation and the
tree is balanced again.
Deletion in AVL Trees
Delete using the same logic as in simple binary search trees.
After deletion, restructure the tree, if needed, to maintain balanced
factor.

Step 1: Find the element in the tree.


Step 2: Delete the node, as per the BST Deletion.
Step 3: Two cases are possible:-
Deletion in AVL Trees
Step 3: Two cases are possible:-
Case 1: Deleting from the right subtree.

1A. If BF(node) = +2 and BF(node -> left-child) = +1, perform LL rotation.

1B. If BF(node) = +2 and BF(node -> left-child) = -1, perform LR rotation.

1C. If BF(node) = +2 and BF(node -> left-child) = 0, perform LL rotation.


Deletion in AVL Trees: Deleting from the right subtree
Deletion in AVL Trees
Deletion in AVL Trees
Deletion in AVL Trees Case 2: Deleting from left subtree.
2A. If BF(node) = -2 and BF(node -> right-child) = -1,
perform RR rotation.

2B. If BF(node) = -2 and BF(node -> right-child) = +1,


perform RL rotation.

2C. If BF(node) = -2 and BF(node -> right-child) = 0,


perform RR rotation.
Deletion in AVL Trees Case 2: Deleting from left subtree.
2A. If BF(node) = -2 and BF(node -> right-child) = -1,
perform RR rotation.
Deletion in AVL Trees Case 2: Deleting from left subtree.
2B. If BF(node) = -2 and BF(node -> right-child) = +1,
perform RL rotation.
Deletion in AVL Trees Case 2: Deleting from left subtree.
2C. If BF(node) = -2 and BF(node -> right-child) = 0,
perform RR rotation.
Build an AVL tree of these elements.
Let, nums = [12 , 3 , 9 , 4 , 6 , 2]
Build an AVL tree of these elements.
Let, nums = [12 , 3 , 9 , 4 , 6 , 2]
Build an AVL tree of these elements.
Let, nums = [12 , 3 , 9 , 4 , 6 , 2]
THANK YOU

You might also like