Vous êtes sur la page 1sur 27

# AVL Trees

## Invented by Adelson-Velskii and Landis.

A binary search tree with a balance condition.
Easy to maintain.
Ensures the depth is O(log N)

## Balance condition for every node in the tree, the

height of the left and right subtrees can differ by at
most 1.

Requiring balancing at
the root is not enough.

5

7
8

2
1

2
1

## Both are binary search trees,

which is an AVL tree?

## Inserting in an AVL Tree

When we do an Insertion
Update all the balancing information for the nodes on the
path back to the root.
As we are updating the balance information on the path up to the root,
we may find a node whose new balance violates the AVL condition.

## What if the insertion violated the AVL tree property?

We do a simple modification to the tree known as a rotation.

## Insertion in an AVL Tree

We will show how to rebalance the tree at the first
violated node
Then prove that this guarantees that the entire tree then
satisfies the AVL property.

## Insertion in an AVL tree

Let us call the node that must be
rebalanced
Since any node has at most 2 children, and a
height imbalance requires that s 2 subtrees
height differ by 2, there are 4 violation cases:
1) An insertion into the left subtree of the left
child of .

2)

child of .

3)

child of .

4)

right child of .

## Outside cases (left-left or right-right), fixed by a

single rotation:
1) An insertion into the left subtree of the left child of .
4) An insertion into the right subtree of the right child of
.
6

6
8

4
3

8
9

## Inside cases (right-left or left-right), fixed by a

double rotation:
2) An insertion into the right subtree of the left child of
.
3) An insertion into the left subtree of the right child of
.
7

6
9

9
8

4
5

## AVL Tree Single Rotation

We want X up a level and Z down a level.
We want k2 as the new root.

Pretend the tree is flexible, grab hold of k 2, letting gravity take hold.
k3 > k2 (by BST property), so k3 becomes k2s right subtree.
X and Z can remain attached to the same point,
and Y becomes k3s subtree to maintain BST property.
k

Symmetric case

k
X

## AVL Tree Rotations

Single rotations: insert 14, 15, 16, 13, 12,
11, 10
First insert 14 and 15:
1
4
1
5

## AVL Tree Rotations

Single rotations:
Inserting 16 causes AVL violation:
1
4
1
5
1
6

Need to rotate.

## AVL Tree Rotations

Single rotations:
Inserting 16 causes AVL violation:
1
4
1
5
1
6

Need to rotate.

## AVL Tree Rotations

Single rotations:
Rotation type:
1
4
1
5
1
6

## AVL Tree Rotations

Single rotations:
Rotation restores AVL balance:
1
5

1
4

1
6

## AVL Tree Rotations

Single rotations:
Now insert 13 and 12:
1
5
1
4
1
3
1
2

1
6

## AVL Tree Rotations

Single rotations:
Rotation type:
1
5
1
4
1
3
1
2

1
6

## AVL Tree Rotations

Single rotations:
1
5
1
3
1
2

1
6
1
4

## AVL Tree Rotations

Single rotations:
1
5
1
3
1
2

1
6
1
4

1
1

## AVL Tree Rotations

Single rotations:
1
5

Rotation type:
1
3
1
2
1
1

1
6
1
4

## AVL Tree Rotations

Single rotations:
1
3
1
5

1
2
1
1

1
4

1
6

## AVL Tree Rotations

Single rotations:
1
3
1
5

1
2
1
1

1
4

1
0

1
6

## AVL Tree Rotations

Single rotations:
Rotation type:

1
3
1
5

1
2
1
1
1
0

1
4

1
6

## AVL Tree Rotations

Single rotations:
1
3
1
5

1
1
1
0

## AVL balance restored.

1
2

1
4

1
6

Double Rotation
Place k2 as the new root.
This forces k1 to be k2s left child and k3 to be its right
child.
Then the 4 subtrees A,B,C,D fall according to BST rules.
k

## Double Rotation in more

detail
A Double Rotation is like 2 single rotations.
Example: Left-right double rotation.
k
3

A
B

Single Left
Rotation at
K1

k
3

t
gh
Ri at
le on
ng ti
Si ta
Ro
K3

k
2