Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 17

Advance Analysis Of Algorithm

AVL-Trees

AVL Trees / Slide 2

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 Trees / Slide 3

AVL tree
An

AVL tree is a binary search tree in which

for every node in the tree, the height of the left and right subtrees differ by at most 1.
AVL property violated here

AVL Trees / Slide 4

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.

e.g. Single Rotation

x y B A
Before Rotation

y x

After Rotation

AVL Trees / Slide 5

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.

AVL Trees / Slide 6

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.

AVL Trees / Slide 7

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

Height of left(x) is h+2 (i.e. height of right(x) is h)


of left(left(x)) is h+1 single rotate with left child Height of right(left(x)) is h+1 double rotate with left child
Height

Height of right(x) is h+2 (i.e. height of left(x) is h)


of right(right(x)) is h+1 single rotate with right child Height of left(right(x)) is h+1 double rotate with right child
Height

AVL Trees / Slide 8

Insertion
Basically

follows insertion strategy of binary search tree


But may cause violation of AVL tree property

Restore

the destroyed balance condition if

needed

7 6 6 8

Original AVL tree

Insert 6 Property violated

Restore AVL property

AVL Trees / Slide 9

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

AVL Trees / Slide 10

Single Rotation Example

Sequentially insert 3, 2, 1, 4, 5, 6 to an AVL Tree


3 2 2 1 3 1 2 3 4 1 2 3

3
2 Insert 3, 2

2 1

1 Single rotation Insert 1 violation at node 3 2 4 3 5 1 3 4

Insert 4

Insert 5, 4 violation at node 3 4 2 5 3 Single rotation 6 5

5 6

Single rotation

Insert 6, violation at node 2

AVL Trees / Slide 11

If we continue to insert 7, 16, 15, 14, 13, 12, 11, 10, 8, 9


4 2 1 3 5 6 7 1 2 3 4 6

Insert 7, violation at node 5

Single rotation 4

4
2 1 3 5 6 7 16 15 1 2 3

6 5
What is the result?

Insert 16, fine Insert 15 violation at node 7

Single rotation But. Violation remains

AVL Trees / Slide 12

Double Rotation Example


5
5

x
C
8

AVL Tree
3

y 8
4

3.5

Insert 3.5 4 5
3.5
8

After Rotation

AVL Trees / Slide 13

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.

AVL Trees / Slide 14

Deletion

AVL Trees / Slide 15

Deletion

Delete

Imbalance

at 3 Perform rotation with 2

Imbalance

at 5 Perform rotation with 8

AVL Trees / Slide 16

Example of AVL Tree by using following given Array considering Balance Factor

5,6,8,3,2,4,7

AVL Trees / Slide 17

Thanks

You might also like