AVL Tree
AVL trees
Outline
Background
Define height-balancing
Maintaining balance within a tree
– AVL trees
– Difference of heights
– Maintaining balance after insertions and erases
2
AVL trees
Background
From previous lectures:
– Binary search trees store linearly ordered data
– Best case height: O(ln(n))
– Worst case height: O(n)
Requirement:
– Define and maintain a balance to ensure O(ln(n)) height
3
AVL trees
Prototypical Examples
These two examples demonstrate how we can correct for
imbalances: starting with this tree, add 1:
4
AVL trees
Prototypical Examples
This is more like a linked list; however, we can fix this…
5
AVL trees
Prototypical Examples
Promote 2 to the root, demote 3 to be 2’s right child, and 1 remains
the left child of 2
6
AVL trees
Prototypical Examples
The result is a perfect, though trivial tree
7
AVL trees
Prototypical Examples
Alternatively, given this tree, insert 2
8
AVL trees
Prototypical Examples
Again, the product is a linked list; however, we can fix this, too
9
AVL trees
Prototypical Examples
Promote 2 to the root, and assign 1 and 3 to be its children
10
AVL trees
Prototypical Examples
The result is, again, a perfect tree
These examples may seem trivial, but they are the basis for the
corrections in the next data structure we will see: AVL trees
11
AVL trees
AVL Trees
We will focus on the first strategy: AVL trees
– Named after Adelson-Velskii and Landis
Balance is defined by comparing the height of the two subtrees
Recall:
– An empty tree has height –1
– A tree with a single node has height 0
12
AVL trees
AVL Trees
A binary search tree is said to be AVL balanced if:
– the difference between the heights between the left and right subtrees is
at most 1, and
– Both subtrees are themselves AVL trees
13
AVL trees
AVL Trees
AVL trees with 1, 2, 3, and 4 nodes:
14
AVL trees
AVL Trees
Here is a larger AVL tree (42 nodes):
15
AVL trees
AVL Trees
The root node is AVL-balanced:
– Both subtrees are of height 4:
16
AVL trees
AVL Trees
All other nodes (e.g., AF and BL) are AVL balanced
– The subtrees differ in height by at most one
17
AVL trees
Height of an AVL Tree
Let F(h) be the fewest number of nodes in a tree of height h
From a previous slide:
F(0) = 1
F(1) = 2
F(2) = 4
Can we find F(h)?
18
AVL trees
Height of an AVL Tree
The worst-case AVL tree of height h would have:
– A worst-case AVL tree of height h – 1 on one side,
– A worst-case AVL tree of height h – 2 on the other, and
– The root node
We get: F(h) = F(h – 1) + 1 + F(h – 2)
19
AVL trees
Height of an AVL Tree
This is a recurrence relation:
1 h0
F( h ) 2 h 1
F( h 1) F( h 2) 1 h 1
The solution?
– Note that F(h) + 1 = (F(h – 1) + 1) + (F(h – 2) + 1)
– Therefore, F(h) + 1 is a Fibonacci number:
F(0) + 1 = 2 → F(0) = 1
F(1) + 1 = 3 → F(1) = 2
F(2) + 1 = 5 → F(2) = 4
F(3) + 1 = 8 → F(3) = 7
F(4) + 1 = 13 → F(4) = 12
F(5) + 1 = 21 → F(5) = 20
F(6) + 1 = 34 → F(6) = 33
20
AVL trees
Height of an AVL Tree
This is approximately
F(h) ≈ 1.8944 f h – 1 ≈ O(f h)
where f ≈ 1.6180 is the golden ratio
Given a number of nodes N, the maximum value of h of AVL Tree will be:
1.8944 f h – 1 = N => h = 1.4404*log(N+1)= O(logN)
21
Operations in AVL Tree
Searching, Complexity? O(log N)
FindMin, Complexity? O(log N)
Deletion? Insertion?
AVL trees
Maintaining Balance
To maintain AVL balance, observe that:
– Inserting a node can increase the height of a tree by at most 1
– Removing a node can decrease the height of a tree by at most 1
23
AVL trees
Maintaining Balance
Consider this AVL tree
24
AVL trees
Maintaining Balance
Consider inserting 15 into this tree
– In this case, the heights of none of the trees change
25
AVL trees
Maintaining Balance
The tree remains balanced
26
AVL trees
Maintaining Balance
Consider inserting 42 into this tree
– In this case, the heights of none of the trees change
27
AVL trees
Maintaining Balance
Consider inserting 42 into this tree
– Now we see the heights of two subtrees have increased by one
– The tree is still balanced
28
AVL trees
Maintaining Balance
Only insert and erase may change the height
– This is the only place we need to update the height
– These algorithms are already recursive
29
AVL trees
Maintaining Balance
If a tree is AVL balanced, for an insertion to cause an imbalance:
– The heights of the subtrees must differ by 1
– The insertion must increase the height of the deeper sub-tree by 1
30
AVL trees
Maintaining Balance
Suppose we insert 23 into our initial tree
31
AVL trees
Maintaining Balance
The heights of each of the subtrees from here to the root are
increased by one
32
AVL trees
Maintaining Balance
However, only two of the nodes are unbalanced: 17 and 36
33
AVL trees
Maintaining Balance
However, only two of the nodes are unbalanced: 17 and 36
– We only have to fix the imbalance at the lowest node
34
AVL trees
Maintaining Balance
We can promote 23 to where 17 is, and make 17 the left child of 23
35
AVL trees
Maintaining Balance
Thus, that node is no longer unbalanced
– Incidentally, neither is the root now balanced again, too
36
AVL trees
Maintaining Balance
Consider adding 6:
37
AVL trees
Maintaining Balance
The height of each of the trees in the path back to the root are
increased by one
38
AVL trees
Maintaining Balance
The height of each of the trees in the path back to the root are
increased by one
– However, only the root node is now unbalanced
39
AVL trees
Maintaining Balance
To fix this, we will look at the general case…
40
Maintaining Balance
If an insertion causes an imbalance, which nodes can be
affected?
Nodes on the path of the inserted node.
Let f be the node that must be rebalanced.
Case 1: insertion in the left subtree of the left child of f
Case 2: insertion in the right subtree of the left child of f
Case 3: insertion in the left subtree of the right child of f
Case 4: insertion in the right subtree of the right child of f
Case 1 and 4 (or Case 2 and 3) are the same. We only solve case 1 and
2
AVL trees
Maintaining Balance: Case 1
Single Rotation
Consider the following setup
– Each blue triangle represents a tree of height h
42
AVL trees
Maintaining Balance: Case 1
Single Rotation
Insert a into this tree: it falls into the left subtree BL of b (a<b<f)
– Assume BL remains balanced
– Thus, the tree rooted at b is also balanced
43
AVL trees
Maintaining Balance: Case 1
Single Rotation
The tree rooted at node f is now unbalanced
– We will correct the imbalance at this node
44
AVL trees
Maintaining Balance: Case 1
Single Rotation
Here are examples of when the
insertion of 7 may cause this
situation when h = –1, 0, and 1
45
AVL trees
Maintaining Balance: Case 1
Single Rotation
We will modify these three pointers
– At this point, this references the unbalanced root node f
46
AVL trees
Maintaining Balance: Case 1
Single Rotation
Specifically, we will rotate these two nodes around the root:
– Recall the first prototypical example
– Promote node b to the root and demote node f to be the right child of b
47
AVL trees
Maintaining Balance: Case 1
This requires the address of node f to be assigned to the right
tree member variable of node b
48
AVL trees
Maintaining Balance: Case 1
Assign any former parent of node f to the address of node b
Assign the address of the tree BR to left tree of node f
49
AVL trees
Maintaining Balance: Case 1
Single Rotation
The nodes b and f are now balanced and all remaining nodes of the
subtrees are in their correct positions
– The height of f is now h + 1 while b remains at height h + 2
50
AVL trees
Maintaining Balance: Case 1
Single Rotation
Additionally, height of the corrected tree rooted at b equals the
original height of the tree rooted at f
– Thus, this insertion will no longer affect the balance of any ancestors all
the way back to the root
51
AVL trees
Summary: Case 1&4
Single Rotation
52
AVL trees
Maintaining Balance: Case 1
Single Rotation
In our example case, the correction is
53
AVL trees
Maintaining Balance: Case 1
In our three sample cases
with h = –1, 0, and 1, the
node is now balanced
and the same height
as the tree before the
insertion
54
AVL trees
Maintaining Balance: Case 2
Double Rotation
Alternatively, consider the insertion of c where b < c < f into our
original tree
55
AVL trees
Maintaining Balance: Case 2
Assume that the insertion of c increases the height of BR
– Once again, f becomes unbalanced
56
AVL trees
Maintaining Balance: Case 2
Here are examples of when the
insertion of 14 may cause this
situation when h = –1, 0, and 1
57
AVL trees
Maintaining Balance: Case 2
Unfortunately, the previous correction does not fix the imbalance at
the root of this sub-tree: the new root, b, remains unbalanced
58
AVL trees
Maintaining Balance: Case 2
In our three sample cases
with h = –1, 0, and 1,
doing the same thing
as before results in
a tree that is still
unbalanced…
– The imbalance is just
shifted to the other
side
59
AVL trees
Maintaining Balance: Case 2
Double Rotation
Re-label the tree by dividing the left subtree of f into a tree rooted at
d with two subtrees of height h – 1
60
AVL trees
Maintaining Balance: Case 2
Double Rotation
Now an insertion causes an imbalance at f
– The addition of either c or e will cause this
61
AVL trees
Maintaining Balance: Case 2
Double Rotation
We will reassign the following pointers
62
AVL trees
Maintaining Balance: Case 2
Double Rotation
Specifically, we will order these three nodes as a perfect tree
– Recall the second prototypical example
63
AVL trees
Maintaining Balance: Case 2
Double Rotation
To achieve this, b and f will be assigned as children of the new root d
64
AVL trees
Maintaining Balance: Case 2
Double Rotation
We also have to connect the two subtrees and original parent of f
65
AVL trees
Maintaining Balance: Case 2
Double Rotation
Now the tree rooted at d is balanced
– After the correction, height of b and f become h + 1 and d is h + 2
66
AVL trees
Maintaining Balance: Case 2
Again, the height of the root did not change
– The heights of all three nodes changed in this process
67
AVL trees
Summary: Case 2 and 3
Double Rotation
68
AVL trees
Maintaining Balance: Case 2
In our three sample cases
with h = –1, 0, and 1, the
node is now balanced
and the same height
as the tree before the
insertion
69
AVL trees
Insertion
Consider this AVL tree
70
AVL trees
Insertion
Insert 73
71
AVL trees
Insertion
The node 81 is unbalanced
– A left-left imbalance
72
AVL trees
Insertion
The node 81 is unbalanced
– A left-left imbalance
73
AVL trees
Insertion
The node 81 is unbalanced
– A left-left imbalance
– Promote the intermediate node to the imbalanced node
74
AVL trees
Insertion
The node 81 is unbalanced
– A left-left imbalance
– Promote the intermediate node to the imbalanced node
– 75 is that node
75
AVL trees
Insertion
The node 81 is unbalanced
– A left-left imbalance
– Promote the intermediate node to the imbalanced node
– 75 is that node
76
AVL trees
Insertion
The tree is AVL balanced
77
AVL trees
Insertion
Insert 77
78
AVL trees
Insertion
The node 87 is unbalanced
– A left-right imbalance
79
AVL trees
Insertion
The node 87 is unbalanced
– A left-right imbalance
80
AVL trees
Insertion
The node 87 is unbalanced
– A left-right imbalance
– Promote the intermediate node to the imbalanced node
81
AVL trees
Insertion
The node 87 is unbalanced
– A left-right imbalance
– Promote the intermediate node to the imbalanced node
– 81 is that value
82
AVL trees
Insertion
The node 87 is unbalanced
– A left-right imbalance
– Promote the intermediate node to the imbalanced node
– 81 is that value
83
AVL trees
Insertion
The tree is balanced
84
AVL trees
Insertion
Insert 76
85
AVL trees
Insertion
The node 78 is unbalanced
– A left-left imbalance
86
AVL trees
Insertion
The node 78 is unbalanced
– Promote 77
87
AVL trees
Insertion
The tree is now balanced again.
88
AVL trees
Insertion
Insert 80
89
AVL trees
Insertion
The node 69 is unbalanced
– A right-left imbalance
– Promote the intermediate node to the imbalanced node
90
AVL trees
Insertion
The node 69 is unbalanced
– A left-right imbalance
– Promote the intermediate node to the imbalanced node
– 75 is that value
91
AVL trees
Insertion
The tree is now balanced again.
92
AVL trees
Insertion
Insert 74
93
AVL trees
Insertion
The node 72 is unbalanced
– A right-right imbalance
– Promote the intermediate node to the imbalanced node
– 75 is that value
94
AVL trees
Insertion
The node 72 is unbalanced
– A right-right imbalance
– Promote the intermediate node to the imbalanced node
95
AVL trees
Insertion
The tree is now balanced again.
96
AVL trees
Insertion
Insert 64
97
AVL trees
Insertion
This causes no imbalances
98
AVL trees
Insertion
Insert 55
99
AVL trees
Insertion
The node 69 is imbalanced
– A left-left imbalance
– Promote the intermediate node to the imbalanced node
100
AVL trees
Insertion
The node 69 is imbalanced
– A left-left imbalance
– Promote the intermediate node to the imbalanced node
– 63 is that value
101
AVL trees
Insertion
The tree is now balanced
102
AVL trees
Insertion
Insert 70
103
AVL trees
Insertion
The root node is now imbalanced
– A right-left imbalance
– Promote the intermediate node to the root
– 75 is that value
104
AVL trees
Insertion
The root node is imbalanced
– A right-left imbalance
– Promote the intermediate node to the root
– 63 is that node
105
AVL trees
Insertion
The result is AVL balanced
106
AVL trees
Erase
• Removing a node from an AVL tree may cause more than one AVL
imbalance
– Like insert, erase must check after it has been successfully called on a child
to see if it caused an imbalance
– Unfortunately, it may cause many imbalances that must be corrected
• Insertions will only cause one imbalance that must be fixed
• Three different cases:
– A leaf node
• Just delete node and balance tree if needed
– A node with 1 child
• Delete node and link its parent node to its child node and then balance tree if
needed
– A node with 2 children:
• Replace this node by the largest node in left subtree. Delete this largest node and
balance tree if needed OR
• Replace this node by the smallest node in right subtree. Delete this smallest node
and balance tree if needed
107
AVL trees
Erase
Consider the following AVL tree
108
AVL trees
Erase
Suppose we erase the front node: 1
109
AVL trees
Erase
While its previous parent, 2, is not unbalanced, its grandparent 3 is
– The imbalance is in the right-right subtree
110
AVL trees
Erase
We can correct this with a simple balance
111
AVL trees
Erase
The node of that subtree, 5, is now balanced
112
AVL trees
Erase
Recursing to the root, however, 8 is also unbalanced
– This is a right-left imbalance
113
AVL trees
Erase
Promoting 11 to the root corrects the imbalance
114
AVL trees
Erase
At this point, the node 11 is balanced
115
AVL trees
Erase
Still, the root node is unbalanced
– This is a right-right imbalance
116
AVL trees
Erase
Again, a simple balance fixes the imbalance
117
AVL trees
Erase
The resulting tree is now AVL balanced
118
AVL trees
Implementation of AVL Tree
Because height of a AVL node is usually calculated, time complexity of height
calculation should be O(1).
We add 1 field in struct AVL node: height of node
119
AVL trees
Summary
• AVL Tree is useful
• If you have free time, please read Red-Black Tree, Splay Tree, and B-Tree
• Next week: Hashing and Priority Queues
120