AVL trees are self-balancing binary search trees that maintain logarithmic time complexity for operations such as search, insert, and delete. They achieve this balance by ensuring that the heights of the left and right subtrees of every node differ by no more than one. When an insertion causes imbalance, AVL trees perform single or double rotations to restore balance. Single rotations are used for imbalances involving exterior nodes, while double rotations involving left-right rotations are used for interior node imbalances.
2. Recall
Motivation: Linked lists and queues work well enough
for most applications, but provide slow service for large
data sets.
3
3
4
4
835
835
837
837
836
836
Ordered insertion takes too long for large sets.
6. Balance
If we had some
way of keeping the
tree “in balance”, our
searches would be
closer to O(log N)
9
9
4
4
1
1
12
12
7
7
17
17
6
6
But how does one
measure balance?
8. Balance: The Key
4
4
1
1
Subtree’s right
child too tall for
its sibling
7
7
6
6
Balance is a
relative
property, not
an absolute
measure...
9
9
4
4
1
1
9
9
12
12
12
12
7
7
6
6
17
17
…as with
all things.
17
17
9. Measuring “Balance”
Adelson-Velskii and Landis suggested that,
since trees may be defined recursively, balance
may also be determined recursively.
9
9
Internal nodes are
also roots of
subtrees.
If each node’s
subtree has a similar
height, the overall
tree is balanced
4
4
1
1
12
12
7
7
6
6
17
17
10. Measuring Balance
Thus, the actual numerical height does not determine
balance; rather, it’s the relative height of both
subtrees.
9
9
4
4
H
1 7
1 7
9
9
12
12
17
17
H-2
H-1
6
6
Balanced Tree Test: subtrees differ in height by no more
than one. This recursively holds true for all nodes!
11. Balance Factor
To measure the balance of a node, there are
two steps:
Record the node height (NH)
--an absolute numeric measure
Record the node’s balance factor (BF)
--a relative measure
The node height alone does not indicate
balance: we have to compare it to the height
of other nodes.
12. EP
EP
ST
ST
Node / Tree Height
Tree height is easy, Here are three trees.
1
1
H=3
H=1
4
4
9
9
Height?
H=2
12
12
17
17
1
1
7
7
6
6
13. Node / Tree Heights for AVLs
By convention, a null
reference will return a
height of zero (0).
Therefore:
The Balance Factor of
is Ht of
17
17
= 1 – (0)
= +1
12
12
0
12
12
minus (0)
17
17
0
0
14. EP
EP
ST
ST
Balance Factor
2
2
Remember that balance is relative.
relative
So we define the Balance Factor (BF) at:
BF = (right subtree height - left subtree height)
9
9
0
+1
12
12
4
4
0
17
17
1
1
+1
0
7
7
6
6
0
-1
15. Balance Factor
Each node has a balance factor. Remember,
it’s based on (right ht - left ht).
-1
9
9
4
4
1 7
1 7
6
6
+1
12
12
17
17
9
9
4
4
0
12
12
-1
1 7
1 7
6
6
+1
0
17
17
0
(We will note BF outside the node, to avoid confusion with the key
values. Some texts don’t even give the keys, because values are
irrelevant once you have a proper BST.)
16. Quiz Yourself
Calculate the balance factor for each node in
each tree:
9
9
9
9
4
4
4
4
1 7
1 7
9
9
4
4
12
12
6
6
9
9
4
4
1 7
1 7
12
12
17
17
16
16
17. Quiz Yourself- Ans
Calculate the balance factor for each node in each tree:
Here, the height is written inside the
node, covering the key value it holds.
Hopefully, this graphical clue helps.
2
2
-1
1
1
1
3
3
2
2
1
1
4
4
0
1
1
1 2
1 2
1
1
4
4
-3
-1
2
2
1
3
3
2
1 1
1 1
2
2
1
1
-1
18. Using Balance Factors
How do the Balance Factors help?
Recall: Adelson-Velskii and Landis
used a recursive definition
of trees. Our BF measure
+1
is likewise
4
4
recursive.
-2
0
Not
Balanced
0
9
9
4
4
0
1 7
1 7
0
-1
9
9
+1
12
12
-1
0
1 7
1 7
6
6
17
17
0
Balanced
If any BF is > +1 or < -1, there’s imbalance
19. AVL’s “Weak” Balancing
•
A fully balanced tree guarantees that, for nodes on each level, all
left and right subtrees have the same height, and imposes this
recursively.
•
AVL trees use a weaker balance definition which still maintains the
logarithmic depth requirement:
12
12
– for any node in an AVL tree, the
height of the left and right nodes
differs by at most 1.
•
For example, this is a valid AVL
tree, but not strictly balanced:
8
8
4
4
6
6
10
10
16
16
2
17
17
20. Addressing Imbalance
If we build a tree from scratch, and check nodes
for imbalance after a new insertion, we can guard
against imbalance.
Q:
So what do we do when the tree is unbalanced?
A:
Rotate the tree.
21. Identifying Offending Scenarios
The key to fixing an unbalanced tree is knowing what
caused it to break. Let’s inductively look at what can
cause a good tree to go bad.
Given an AVL balanced tree:
Two possible left insertions could
break balance:
#1
Insertion to
left subtree
of left child
-2
-1
1
1
9
9
4
4
0
#2
+1
9
9
4
4
-1
0
-2
9
9
4
4
7
7
0
Insertion to
right subtree
of left child
22. #1
Patterns
of Problems
-2
Insertion to
99
left subtree -1
44
of left child
11
#2
-2
99
+1
44
0
0
77
Insertion to
right subtree
of left child
… and the mirror of these insertions
#3
Insertion to
left subtree
of right child
+2
9
9 -1
11
11
0
10
10
#4
+2
Insertion to
right subtree
of right child
9
9 +1
11
11
0
13
13
23. #1
-2
Insertion to
99
left subtree -1
44
of left child
11
#2
Imbalance found
regardless of height
0
9
9
matches
-2
-2
+1
99
44
0
77
Insertion to
right subtree
of left child
H-1
H
H+1
#3
+2
99
Insertion to
left subtree
of right child
11 -1
11
10
10 0
4
4
(H-1) - (H+1) = -2
#4
+2
99
11
Insertion to 11 +1
right subtree
of right child
13 0
13
Given our recursive definition of trees, we know these four scenarios
are complete. The same insertion into a larger AVL balanced tree
does not create a new category.
24. #1
#2
-2
Insertion to
99
left subtree -1
44
of left child
11
0
#3
+2
99
Insertion to
left subtree
of right child
11 -1
11
10
10 0
Let’s look at two
insertions that
cause imbalance.
Recall they are
mirrors.
They both deal with
“outside” insertions
on the tree’s exterior
face: the left-left and
right-right insertions.
-2
+1
99
44
0
77
Insertion to
right subtree
of left child
#4
+2
99
11
Insertion to 11 +1
right subtree
of right child
13 0
13
25. Single Rotation
#1
Insertion to
left subtree
of left child
-2
-1
1
1
9
9
4
4
These insertions can
be cured by swapping
parent and child . . .
0
#4
. . . provided you
maintain search
order. (An AVL tree is
a BST)
+2
Insertion to
right subtree
of right child
9
9 +1
11
11
0
13
13
26. Single Rotation: Right
#1
0
-2
-1
Insertion to
left subtree
of left child
9
9
4
4
1
1
0
X
X
1
1
9
9
0
A single rotation round the
unbalanced node restores AVL
balance in all cases
W
W
W
W
A
0
4
4
X
X
B
C
A B
C
28. #4
Single Rotation: Left
+2
Insertion to
right subtree
of right child
0
9
9 +1
11
11
0
13
13
11
11
0
0
9 13
9 13
A similar rotation is used for right-right
insertions that cause an imbalance
X
X
W
W
W
W
X
X
A
B
C
A
B
C
30. #1
#2
-2
Insertion to
99
left subtree -1
44
of left child
11
0
#3
+2
99
Insertion to
left subtree
of right child
11 -1
11
10
10 0
Let’s look at the
other two insertions
causing problems.
Recall they are
mirrors.
They both deal with
“inside” insertions on
the tree’s interior:
right-left and left-right
-2
+1
99
44
0
77
Insertion to
right subtree
of left child
#4
+2
99
11
Insertion to 11 +1
right subtree
of right child
13 0
13
31. Double Rotation
#2
These insertions are
not solved with a
single rotation.
+1
-2
9
9
4
4
7
7
#3
Insertion to
left subtree
of right child
+2
9
9 -1
11
11
0
10
10
0
Insertion to
right subtree
of left child
Instead, two single
rotations, left-right
and right-left are
performed.
32. Why Doesn’t Single Rotation Work?
X
X
Single
Right
W
W
A
C
B
W
W
Rotation
Attempted
X
X
A
B
C
Unbalanced insertions into the “interior” of
the tree remain unbalanced with only a
single rotation
33. Double Rotation
#3
+2
0
9
9 -1
Insertion to
left subtree
11
11
of right child
0
10
10
10
10
0
0
9 11
9 11
(trivial case)
Y
Y
X
X
X
X
W
W
A
Y
Y
D
B
C
A
W
W
B
C
D
36. Double Rotation Strategy
On a left-right insert, we first rotate left
9
9
4
4
D
7
7
A
7
7
at 4
D
4
4
C
B
C
A
SRR
9
9
SLR
B
7
7
4
4
at 9
A
9
9
B C
D
Double left rotate =
single left rot + single right rot
This places the tree in a position for which we already
have a working solution. (Code reuse!)
37. Double Rotation Strategy
9
9
11
11
A
10
10
D
B
C
SRR
at 11
A
SLR
9
9
10
10
11
11
10
10
9
9
at 9
B
A
C
11
11
B C
D
Similarly, we perform a right and left rotation on
insertions that went left-right. The first rotation reforms
the tree into an exterior imbalance. We already have a
way of fixing this: single left rotations
D