You are on page 1of 11

Non-Linear Data Structures

AVL Balanced Trees

AVL Tree
Definition
z An AVL Tree is a special case of a height balanced binary tree,
where for each node in the tree, the difference in height of its two
subtrees is at most one.

z A perfectly balanced binary tree is an AVL tree but not the reverse.

z The operations defined for binary search trees work also for AVL
trees, except for the insert and delete operations, where a rebalance
may be needed after the operation is done.

z AVL trees have an average search length that is almost identical to


the perfectly balanced (minimum height) trees.

z The height of an AVL tree never exceeds 1.45 (log2 n).


AVL Tree
Examples
6

I 2 7

E L
1 3 11

C F K
5 9

A
4 8 10

AVL tree Not an AVL tree

AVL Tree Implementation


Using a Linked Structure
z Use the binary tree node generic class: BTNode<E>, as
defined before. Add an integer balance factor field. BF element
left right
z The balance factor of a node is defined as the height of
its right subtree minus the height of its left subtree. Left child Right child

z Use two BTNode<E> pointer variables:


• root -- points to the root node of the tree.
• current -- points to the current element node in the tree.

z Use one integer variable: root current


• count -- stores the number of nodes in the tree.
count
z An empty AVL tree is initialized by setting: 0
root = current = null, and count = 0.
AVL Tree Implementation
Using a Linked Structure (cont.)
z Operations implemented for the BST are still
applicable to the AVL tree.

z The insert, and remove operations need slight


changes.

z After the insertion or removal of a node, check for


the AVL balance conditions and adjust the tree
accordingly.

AVL Tree
The Insert Operation
z Given an AVL tree with a root node, n.
Let L and R be the left and right subtrees of n, respectively.
Let hL and hR be the heights of L and R, respectively.

z Suppose that a new node is to be inserted in the left subtree,


L, then three cases are identified:
1. hL = hR : L & R become of unequal height, the tree is still AVL.
2. hL < hR : L & R become of equal height, the tree is still AVL.
3. hL > hR : The balance is violated, the tree must be rebalanced.

z The rebalancing action is a transformation to restore the


balance, where the only movements allowed are in the
vertical direction, called rotation. Two cases are possible:
AVL Tree
Case1: The Single Rotation
z The new node is inserted at the leftmost branch of the tree, as shown.
z A similar case exists when the new node is inserted at the rightmost
branch of the tree ( the mirror image).
z Rebalancing for both cases is done through a single rotation operation.

B A

A B

Case1: Single Rotation


h h
h h h h

T1 T2 T3 T1 T2 T3

AVL Tree
Case2: The Double Rotation
z A new node inserted in the middle of left branch of the tree, as shown.
z A similar case exists when a new node is inserted in the middle of right
branch of the tree (the mirror image).
z Rebalancing for both cases is done through a double rotation operation.
C
B
A
B A C

h
Case2: Double Rotation
h
h-1 h-1 h-1 h-1
h h

T1 T2 T3 T4 T1 T2 T3 T4
AVL Tree
The Insert Algorithm
1. Follow the search path until it is verified that the key to
be inserted is not in the tree.
2. Insert the new node, and determine the resulting
balance factor.
3. Retreat along the search path and check the balance
factor at each node:
• Suppose that a node, p, is reached from the left branch with
indication that it increased its height ( BF = -2).
• Check the balance factor of the left child of p:
1. If it is -1, this is case1 Î rebalance using a single rotation.
2. Otherwise, this is case2 Î rebalance using double rotation.
• Similarly, for the right branch, just reverse all the red color text.

AVL Tree
Examples of the Insert Operation
4 + 4 +2 5 0

5 0 Add 7 5 + RR 0 4 7 0

7 0

5 - 5 ? 5 -

- 4 7 0 Add 1 -2 4 7 0 LL 0 2 7 0

0 2 - 2 0 1 4 0

0 1
AVL Tree
Examples of the Insert Operation (cont.)

5 - 5 -2 4 0

0 2 7 0 Add 3 + 2 7 0 LR 0 2 5 +

0 1 4 0 0 1 4 - 0 1 3 0 7 0

0 3

AVL Tree
Examples of the Insert Operation (cont.)

4 0 4 ? 4 0

0 2 5 + Add 6 0 2 5 +2 RL 0 2 6 0

0 1 3 0 7 0 0 1 3 0 7 - 0 1 0 3 5 0 7 0

0 6
AVL Tree
The Insert Operation -- Performance
z The expected height of a constructed AVL tree when all n!
permutations of n keys occur with equal probability is difficult to get
mathematically, but test results show it to have an average:

h = log2(n) + C, (C ≅ 0.25)

z The rebalancing operation is O(1)

z Rebalancing is needed once for approximately every two insertions.

z Use AVL trees only if searching is more frequent than insertion.

AVL Tree
The Remove Operation
z Given an AVL tree with a root node, n.
Let L and R be the left and right subtrees of n, respectively.
Let hL and hR be the heights of L and R, respectively.

z Suppose that a node is to be removed from the left subtree,


L, then three cases are identified:
1. hL = hR : L & R become of unequal height, the tree is still AVL.
2. hL > hR : L & R become of equal height, the tree is still AVL.
3. hL < hR : The balance is violated, the tree must be rebalanced.

z Rebalancing is done through the same rotation operations


used for insertion, namely, single or double rotations.
AVL Tree
The Remove Algorithm
1. Follow the search path until it is verified that the key to
be removed is in the tree.
2. Remove the node.
3. Retreat along the search path and check the balance
factor at each node:
• Suppose that a node, p, is reached from the left branch with
indication that it changed its height ( BF = ±2).
• Check the balance factor of the right child of p:
1. If it is >=0, this is case1 Î rebalance using a single rotation.
2. Otherwise, this is case2 Î rebalance using double rotation.
• Similarly, for the right branch, just reverse all the red color text.

AVL Tree
Examples of the Remove Operation

5 + 5 ? 5 +

0 2 7 + Delete 6 0 2 7 +2 RR 0 2 10 -

0 1 0 3 6 0 10 0 0 1 0 3 10 0 0 1 0 3 7 + 11 0

0 9 11 0 0 9 11 0 9 0
AVL Tree
Examples of the Remove Operation

5 + 3 +

0 2 10 - Delete 5 - 2 10 - No Rotation

0 1 0 3 7 + 11 0 0 1 7 + 11 0

9 0 9 0

AVL Tree
Examples of the Remove Operation

3 + 3 +2 7 0

- 2 10 - Delete 2 0 1 10 - RL - 3 10 0

0 1 7 + 11 0 7 + 11 0 0 1 9 0 11 0

9 0 9 0
AVL Tree
The Remove Operation -- Performance
z While insertion of a single key may require at most one rotation,
removal of a single key may require a rotation at every node along
the search path.

z The worst case is to remove the rightmost node of a Fibonacci tree.

z The rebalancing operation is O(1)

z Tests show that a rotation is required for every five deletions.

z Use AVL trees only if searching is more frequent than insertion or


deletion.

AVL Tree
The Fibonacci Tree
z Fibonacci trees are the worst case of AVL trees.

z Definition:
1. The empty tree is the Fibonacci tree of height 0.
2. A single node is the Fibonacci tree of height 1.
3. If Th-1 and Th-2 are Fibonacci trees of heights h-1 and h-2,
respectively, then Th = < > is a Fibonacci tree of height h.
Th-1 Th-2

4. No other trees are Fibonacci trees.


AVL Tree
Example Fibonacci Trees

T2 : T3 : T4 :

You might also like