You are on page 1of 57

AVL Tree

Motivation
• When building a binary search tree, what type of
trees would we like? Example: 3, 5, 8, 20, 18, 13, 22

5
13
8
5 20
13

18 3 8 18 22

20

22

2
Motivation
• Complete binary tree is hard to build when we
allow dynamic insert and remove.
– We want a tree that has the following properties
• Tree height = O(log(N))
• allows dynamic insert and remove with O(log(N)) time complexity.
– The AVL tree is one of this kind of trees.
8
13
5 18
5 20

3 13 20
3 8 18 22 22
3
Introduction
• AVL tree is the first balanced binary search
tree (name after its discovers, Adelson-
Velskii and Landis).
• The AVL tree is a balanced search tree that
has an additional balance condition.
• The balance condition must be easy to
maintain and ensures that the depth of the
tree is O(logN)
§4 AVL Trees

Adelson-Velskii-Landis (AVL) Trees (1962)


【 Definition 】 An empty binary tree is height balanced. If T is a
nonempty binary tree with TL and TR as its left and right subtrees, then
T is height balanced iff

(1) TL and TR are height balanced,The


andheight of an empty tree
(2) | hL  hR |  1 where hL and hR are theisheights
defined of L and
to Tbe –1. TR ,
respectively.
【 Definition 】 The balance factor BF( node ) = hL  hR . In an AVL tree,
BF( node ) = 1, 0, or 1.

4 5 7

3 5 2 8 2 8

2 6 1 4 7 1 4

1 7 3 3 5

3/19
AVL Trees
 A tree is said to be balanced if for each
node, the number of nodes in the left subtree
and the number of nodes in the right subtree
differ by at most one.

 A tree is said to be height-balanced or AVL


if for each node, the height of the left
subtree and height of the right subtree differ
by at most one.
The Height of an AVL Tree - I
• For an AVL Tree with N nodes, the height is at most
1.44log(N+2)-0.328.
• In practice it is slightly more than log N.
• Example: An AVL tree of height 9 with fewest nodes
(143).

Izmir University of Economics 7


4.5 AVL Trees
 The height of the left subtree minus the
height of the right subtree of a node is called
the balance of the node. For an AVL tree,
the balances of the nodes are always -1, 0
or 1.

 Given an AVL tree, if insertions or deletions


are performed, the AVL tree may not remain
height balanced.
Balanced Binary Search Trees
• If the height of a binary tree is always O(log
n), we can guarantee O(log n) performance
for each search tree operation
• Trees with a worst-case height of O(log n)
are called balanced trees
• An example of a balanced tree is AVL
(Adelson-Velsky and Landis) tree
AVL Tree
Definition
• Binary tree.
• If T is a nonempty binary tree with TL and TR
as its left and right subtrees, then T is an
AVL tree iff
1. TL and TR are AVL trees, and
2. |hL – hR|  1 where hL and hR are the heights of
TL and TR, respectively
Balance Factor
• AVL trees are normally represented using the linked
representation
• To facilitate insertion and deletion, a balance factor (bf)
is associated with each node
• The balance factor bf(x) of a node x is defined as
height(xleftChild) – height(xrightChild)
• Balance factor of each node in an AVL tree must be –1,
0, or 1
AVL Tree with Balance Factors
-1 10

1 7 1 40

0 3 0 8 1 30 45 -1

0 0 5 0
1 -1 20 0 35 60
0
• Is this an AVL tree? 25

• What is the balance factor for each node in this AVL tree?
• Is this an AVL search tree?
Rotation
Definition
• To switch children and parents among two
or three adjacent nodes to restore balance of
a tree.
• A rotation may change the depth of some
nodes, but does not change their relative
ordering.
Imbalance Types
• After an insertion, when the balance factor of node A is –2 or 2,
the node A is one of the following four imbalance types
1. LL: new node is in the left subtree of the left subtree of A
2. LR: new node is in the right subtree of the left subtree of A
3. RR: new node is in the right subtree of the right subtree of A
4. RL: new node is in the left subtree of the right subtree of A
4.5.1 AVL Trees: Single Rotation
 Construct AVL tree (height balanced)
4.5.1 AVL Trees: Single Rotation
Insert
Insert 4,
4, 55

Insert
Insert 66
4.5.1 AVL Trees: Single Rotation

Insert
Insert 77
AVL Trees
(Adelson – Velskii – Landis)
AVL tree:
20

16 25

7 18 30

5 17 19
AVL Trees
Not AVL tree:
20

16 25

7 18 30

5 17 19 27
AVL Tree Rotations
Single rotations: insert 14, 15, 16, 13, 12, 11, 10
• First insert 14 and 15:

14

15

• Now insert 16.


AVL Tree Rotations
Single rotations:
• Inserting 16 causes AVL violation:

14

15

16

• Need to rotate.
AVL Tree Rotations
Single rotations:
• Inserting 16 causes AVL violation:

14

15

16

• Need to rotate.
AVL Tree Rotations
Single rotations:
• Rotation type:

14

15

16
AVL Tree Rotations
Single rotations:
• Rotation restores AVL balance:

15

14 16
AVL Tree Rotations
Single rotations:
• Now insert 13 and 12:
15

14 16

13

12

• AVL violation - need to rotate.


AVL Tree Rotations
Single rotations:
• Rotation type:
15

14 16

13

12
AVL Tree Rotations
Single rotations:

15

13 16

12 14

• Now insert 11.


AVL Tree Rotations
Single rotations:
15

13 16

12 14

11

• AVL violation – need to rotate


AVL Tree Rotations
Single rotations:
• Rotation type: 15

13 16

12 14

11
AVL Tree Rotations
Single rotations:

13

12 15

16
11 14

• Now insert 10.


AVL Tree Rotations
Single rotations:

13

12 15

16
11 14

10

• AVL violation – need to rotate


AVL Tree Rotations
Single rotations:
• Rotation type:
13

12 15

16
11 14

10
AVL Tree Rotations
Single rotations:

13

11 15

10 12 14 16

• AVL balance restored.


AVL Trees
3. Inserted into the right subtree of A’s left child (lets call this B).

This problem is fixed by applying a left-right rotation.


- Make A’s left link equal to B’s right link (call this C)
- Make B’s right link equal to C’s left link
- Make C’s left link equal to B
- Change the parent of A to point to C
- Make A’s left link equal to C’s right link
- Make C’s right link point to A

A 15 A 15 C 12
B 7 C 12 B 7 15 A

C 12 B 7
AVL Trees
4. Inserted into the left subtree of A’s right child (lets call this B).

This problem is fixed by applying a right-left rotation.


- Make A’s right link equal to B’s left link (call this C)
- Make B’s left link equal to C’s right link
- Make C’s right link equal to B
- Change the parent of A to point to C
- Make A’s right link equal to C’s left link
- Make C’s left link point to A

A 15 A 15 C 21

B 32 C 21 A 15 32 B

C 21 B 32
AVL Tree Rotations
Double rotations: insert 1, 2, 3, 4, 5, 7, 6, 9, 8
• First insert 1 and 2:
13

11 15

10 12 14 16
AVL Tree Rotations
Double rotations:
• AVL violation - rotate
13

11 15

16
10 12 14

2
AVL Tree Rotations
Double rotations:
• Rotation type:
13

11 15

16
10 12 14

2
AVL Tree Rotations
Double rotations:
• AVL balance restored:
13

11 15

2 12 14 16

1 10

• Now insert 3.
AVL Tree Rotations
Double rotations:
• AVL violation – rotate:
13

11 15

2 12 14 16

1 10

3
AVL Tree Rotations
Double rotations:
• Rotation type:
13

11 15

2 12 14 16

1 10

3
AVL Tree Rotations
Double rotations:
• AVL balance restored:
13

10 15

2 11 14 16

1 3 12

• Now insert 4.
AVL Tree Rotations
Double rotations:
• AVL violation - rotate
13

10 15

2 11 14 16

1 3 12

4
AVL Tree Rotations
Double rotations:
• Rotation type:
13

10 15

2 11 14 16

1 3 12

4
AVL Tree Rotations
Double rotations:
10

2 13

1 11 15
3

4 12 14 16

• Now insert 5.
AVL Tree Rotations
Double rotations:
10

2 13

1 11 15
3

4 12 14 16

• AVL violation – rotate.


AVL Tree Rotations
Single rotations:
• Rotation type:
10

2 13

1 11 15
3

4 12 14 16

5
AVL Tree Rotations
Single rotations:
• AVL balance restored:
10

2 13

1 11 15
4

5 12 14 16
3

• Now insert 7.
AVL Tree Rotations
Single rotations:
• AVL violation – rotate.
10

2 13

1 11 15
4

5 12 14 16
3

7
AVL Tree Rotations
Single rotations:
• Rotation type:
10

2 13

1 11 15
4

5 12 14 16
3

7
AVL Tree Rotations
Double rotations:
• AVL balance restored.
10

4 13

2 11 15
5

7 12 14 16
1 3

• Now insert 6.
AVL Tree Rotations
Double rotations:
• AVL violation - rotate.
10

4 13

2 11 15
5

7 12 14 16
1 3

6
AVL Tree Rotations
Double rotations:
• Rotation type:
10

4 13

2 11 15
5

7 12 14 16
1 3

6
AVL Tree Rotations
Double rotations:
• AVL balance restored.
10

4 13

2 11 15
6

12 14 16
1 3 5 7

• Now insert 9 and 8.


AVL Tree Rotations
Double rotations:
• AVL violation - rotate.
10

4 13

2 11 15
6

12 14 16
1 3 5 7

8
AVL Tree Rotations
Double rotations:
• Rotation type:
10

4 13

2 11 15
6

12 14 16
1 3 5 7

8
AVL Tree Rotations
Final tree:
• Tree is almost perfectly balanced
10

4 13

2 11 15
6

12 14 16
1 3 5 8

7 9

You might also like