You are on page 1of 9

# The AVL Balance Condition

## AVL balance property:

CSE 326: Data Structures
Left and right subtrees of every node
have heights differing by at most 1
AVL Trees
• Ensures small depth
› Will prove this by showing that an AVL tree of height
Neva Cherniavsky h must have a lot of (i.e. O(2h)) nodes
Summer 2006 • Easy to maintain
› Using single and double rotations

## The AVL Tree Data Structure Is this an AVL Tree?

Structural properties
1. Binary tree property 10
(0,1, or 2 children) 8 •How do we track the
2. Heights of left and right balance?
5 15
subtrees of every node 5 11 •How do we detect
differ by at most 1 imbalance?
Result: 2 9 20
2 6 10 12
•How do we restore
Worst case depth of any
node is: O(log n) 7 17 30 balance?
4 7 9 13 14

Ordering property
15
› Same as for BST

Circle One:
6

## 4 8 AVL Height of an AVL Tree

1 7 11
Not AVL • M(h) = minimum number of nodes in an
10 12 AVL tree of height h.
6 • Basis h

4 8 AVL
› M(0) = 1, M(1) = 2
• Induction
1 5 7 11
› M(h) = M(h-1) + M(h-2) + 1 h-2
Not AVL h-1
3 • Solution
2 › M(h) > φh - 1 (φ = (1+√5)/2 ≈ 1.62)

Student Activity If not AVL, put a box around nodes where AVL property is violated.

1
Proof that M(h) > φh Height of an AVL Tree
• Basis: M(0) = 1 > φ0 -1, M(1) = 2 > φ1-1 • M(h) > φh (φ ≈ 1.62)
• Induction step. • Suppose we have N nodes in an AVL
M(h) = M(h-1) + M(h-2) + 1 tree of height h.
> (φh-1 - 1) + (φh-2 - 1) + 1 › N > M(h)
= φh-2 (φ +1) - 1 › N > φh - 1
= φh - 1 (φ2 = φ +1) › logφ(N+1) > h (relatively well balanced
tree!!)

## Node Heights Node Heights after Insert 7

2 2 balance factor
2 3 1-(-1) = 2
6 6 6 6
1 0 1 1 1 1 1 2
4 9 4 9 4 9 4 9
0 0 0 0 0 0 0 0 0 0 1 -1
1 5 1 5 8 1 5 7 1 5 8
0
7
height of node = h height of node = h
balance factor = hleft-hright balance factor = hleft-hright
empty height = -1 empty height = -1

## Insert and Rotation in AVL Single Rotation in an AVL

Trees Tree
• Insert operation may cause balance factor 2 2
to become 2 or –2 for some node 6 6
1 2 1 1
› only nodes on the path from insertion point to 4 9 4 8
root node have possibly changed in height 0 0 1 0 0 0 0

## › So after the Insert, go back up to the root 1 5 8 1 5 7 9

node by node, updating heights 0
› If a new balance factor (the difference hleft- 7
hright) is 2 or –2, adjust tree by rotation around
the node

2
Insertions in AVL Trees Bad Case #1
Let the node that needs rebalancing be α.
Insert(6)
There are 4 cases:
Outside Cases (require single rotation) : Insert(3)
1. Insertion into left subtree of left child of α. Insert(1)
2. Insertion into right subtree of right child of α.
Inside Cases (require double rotation) :
3. Insertion into right subtree of left child of α.
4. Insertion into left subtree of right child of α.
The rebalancing is performed through four
separate rotation algorithms.

## Fix: Apply Single Rotation AVL Insertion: Outside Case

AVL Property violated at this node (x)
2 1
Consider a valid
AVL subtree
j
6 3

3
1
0 0 k h

1 6
1
0 h
h
Z
X Y
Single Rotation:
1. Rotate between x and child

## AVL Insertion: Outside Case AVL Insertion: Outside Case

j Inserting into X j Do a “rotation from left”
destroys the AVL
property at node j
k h k h

h+1 h Z h+1 h Z
Y Y
X X

3
Single rotation from left Outside Case Completed
j k
“rotation from left” done!
(“rotation from right” is
mirror symmetric)

k h
h+1
j
h+1 h Z h h

Y X Y Z
X AVL property has been restored!

## Single rotation example

15
5 20

3 10 17 21 Insert(1)
2 4 Insert(6)
1 15 Insert(3)
3 20

2 5 17 21
1 4 10

## Fix: Apply Double Rotation

AVL Property violated at this node (x) AVL Insertion: Inside Case
2 2
1 1 1 Consider a valid
AVL subtree
j
3
1 1
6 3
0
0 0 k h
0 1 6
3 6 h h Z
Double Rotation X Y
1. Rotate between x’s child and grandchild
2. Rotate between x and x’s new child

4
AVL Insertion: Inside Case AVL Insertion: Inside Case
Inserting into Y
destroys the j Does “rotation from left”
restore balance? k “Rotation from left”
does not restore
AVL property balance… now k is
at node j
k h
h j out of balance

h h+1 Z X h+1
h

X Z
Y Y

## AVL Insertion: Inside Case AVL Insertion: Inside Case

Consider the structure
of subtree Y… j Y = node i and
subtrees V and W
j
k h
k h

h h+1 Z h
i h+1 Z
X X h or h-1
Y
V W

## AVL Insertion: Inside Case Double rotation : first rotation

2
j We will do a
“double rotation” . . .
j
1
k i
i Z k Z
X W
V W X V

5
Double rotation : second Double rotation : second
rotation rotation
j right rotation complete

## Balance has been

i i restored to the universe

k Z k j
W h h or h-1
h

X V X V W Z

15 15
8 17 8 17

4 10 16 6 10 16
4
3 6
3 5
5
15 15
8 17 6 17
6 10 16 4 8 16
4 3 5 10
3 5

## Imbalance at node X Insert into an AVL tree: a b e c d

Single Rotation
1. Rotate between x and child

Double Rotation
1. Rotate between x’s child and grandchild
2. Rotate between x and x’s new child

Student Activity

6
Single and Double Rotations:
Inserting what integer values Insertion into AVL tree
would cause the tree to need a:

## 1. single rotation? 9 1. Find spot for new key

5 11
2. Hang new node there with this key
2 7 13
3. Search back up the path for imbalance
2. double rotation? 0 3
4. If there is an imbalance:
case #1: Perform single rotation and exit

## 3. no rotation? case #2: Perform double rotation and exit

Both rotations keep the subtree height unchanged.
Hence only one rotation is sufficient!
Student Activity

## Easy Insert Hard Insert (Bad Case #1)

3
3 Insert(33) 10
Insert(3) 10
2 2
1 2 5 15
5 15
1 0 0 1
0 0 0 1 2 9 12 20
2 9 12 20
0 0 0
0 0 3 17 30
17 30 Unbalanced?

## Unbalanced? How to fix?

3
Single Rotation3 Hard Insert (Bad Case #2)
10 10 3
Insert(18) 10
2 3 2 2
5 15 5 20 2 2
5 15
1 0 0 2 1 0 1 1
2 9 12 20 2 9 15 30 1 0 0 1
2 9 12 20
0 0 1 0 0 0 0
3 17 30 3 12 17 33 0 0 0
Unbalanced? 3 17 30
0
33 How to fix?

7
Double Rotation (Step #1)
Single Rotation (oops!) 3 3
3 3 10 10
10 10
2 3 2 3
2 3 2 3 5 15 5 15
5 15 5 20
1 0 0 2 1 0 0 2
1 0 0 2 1 0 2 0 2 9 12 20 2 9 12 17
2 9 12 20 2 9 15 30
0 1 0 0 1
0 1 0 0 0 1 3 17 30 3 20
3 17 30 3 12 17
0 0 0
0 0 18 18 30
18 18

## Double Rotation (Step #2) AVL Trees Revisited

3 3
10 10
• Balance condition:
For every node x, -1 ≤ balance(x) ≤ 1
2 3 2 2 › Strong enough : Worst case depth is O(log n)
5 15 5 17 › Easy to maintain : one single or double rotation
1 0 0 2 1 0 1 1
2 9 12 17 2 9 15 20
• Guaranteed O(log n) running time for
0 1 0 0 0 0 › Find ?
3 20 3 12 18 30 › Insert ?
0 0 › Delete ?
18 30 › buildTree ?

## AVL Trees Revisited Other Possibilities?

• Could use different balance conditions,
• What extra info did we maintain in each different ways to maintain balance, different
node? guarantees on running time, …
• Why aren’t AVL trees perfect?
• Many other balanced BST data structures
• Where were rotations performed? › Red-Black trees
› AA trees
› Splay Trees
› 2-3 Trees
• How did we locate this node? › B-Trees
› …

8
Implementation Single Rotation
RotateFromRight(n : reference node pointer) {
balance (1,0,-1) p : node pointer;
key p := n.right; n
n.right := p.left;
left right p.left := n;
n := p
}

Y Z

## Double Rotation AVL Tree Deletion

• Class participation • Similar to insertion
• Implement Double Rotation in two lines.
› Rotations and double rotations needed to
DoubleRotateFromRight(n : reference node pointer) {
????
rebalance
n
} › Imbalance may propagate upward so that
many rotations may be needed.

V W

## Pros and Cons of AVL Trees

Arguments for AVL trees:
1. Search is O(log N) since AVL trees are always well balanced.
2. The height balancing adds no more than a constant factor to the
speed of insertion, deletion, and find.

## Arguments against using AVL trees:

1. Difficult to program & debug; more space for height info.
2. Asymptotically faster but rebalancing costs time.
3. Most large searches are done in database systems on disk and use
other structures (e.g. B-trees).
4. May be OK to have O(N) for a single operation if total run time for
many consecutive operations is fast (e.g. Splay trees).