Professional Documents
Culture Documents
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.
I 2 7
E L
1 3 11
C F K
5 9
A
4 8 10
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.
B A
A B
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)
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.
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.
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
T2 : T3 : T4 :