AVL-Trees: Advance Analysis of Algorithm
AVL-Trees: Advance Analysis of Algorithm
AVL-Trees: Advance Analysis of Algorithm
AVL-Trees
AVL tree
AVL (Adel'son-Vel'skii and Landis 1962) Transfer and Conquer Height of a node The height of a leaf is 1. The height of a null pointer is zero. The height of an internal node is the maximum height of its children plus 1
Node will contain left child as less than and right child as greater than to accomplish balance factor.
AVL tree
An
for every node in the tree, the height of the left and right subtrees differ by at most 1.
AVL property violated here
Rotations
When the tree structure changes (e.g., insertion or deletion), we need to transform the tree to restore the AVL tree property. This is done using single rotations or double rotations.
x y B A
Before Rotation
y x
After Rotation
Rotations
Since
an insertion/deletion involves adding/deleting a single node, this can only increase/decrease the height of some subtree by 1 Thus, if the AVL tree property is violated at a node x, it means that the heights of left(x) ad right(x) differ by exactly 2. Rotations will be applied to x to restore the AVL tree property.
Insertion
First, insert the new key as a new leaf just as in ordinary binary search tree Then trace the path from the new leaf towards the root. For each node x encountered, check if heights of left(x) and right(x) differ by at most 1. If yes, proceed to parent(x). If not, restructure by doing either a single rotation or a double rotation [next slide]. For insertion, once we perform a rotation at a node x, we wont need to perform any rotation at any ancestor of x.
Insertion
Let
x be the node at which left(x) and right(x) differ by more than 1 Assume that the height of x is h+3 There are 4 cases
Insertion
Basically
Restore
needed
7 6 6 8
Rotations
Rebalance
of AVL tree are done with simple modification to tree, known as rotation Insertion occurs on the outside (i.e., left-left or right-right) is fixed by single rotation of the tree Insertion occurs on the inside (i.e., left-right or right-left) is fixed by double rotation of the tree
3
2 Insert 3, 2
2 1
Insert 4
5 6
Single rotation
Single rotation 4
4
2 1 3 5 6 7 16 15 1 2 3
6 5
What is the result?
x
C
8
AVL Tree
3
y 8
4
3.5
Insert 3.5 4 5
3.5
8
After Rotation
Deletion
If the node is a leaf or has only one child, remove it. Otherwise, replace it with either the largest in its left subtree (inorder predecessor) or the smallest in its right subtree (inorder successor), and remove that node. The node that was found as a replacement has at most one subtree. After deletion, retrace the path back up the tree (parent of the replacement) to the root, adjusting the balance factors as needed. For deletion, after we perform a rotation at x, we may have to perform a rotation at some ancestor of x. Thus, we must continue to trace the path until we reach the root.
Deletion
Deletion
Delete
Imbalance
Imbalance
Example of AVL Tree by using following given Array considering Balance Factor
5,6,8,3,2,4,7
Thanks