AVL Trees Readings Binary Search Tree

Readings
• Reading Sec. 4.4
AVL Trees
CSE 373
Data Structures
Winter 2007
AVL Trees
Binary Search Tree - Best
Time
Binary Search Tree - Worst
Time
• All BST operations are O(d), where d is
tree depth
• minimum d is d = ⎣log2N⎦ for a binary tree
with N nodes
› What is the best case tree?
› What is the worst case tree?
1
2
1
1
3
5
Approaches to balancing trees
• Strict balance
› The tree must always be balanced perfectly
3
• Pretty good balance
› Only allow a little out of balance
• Adjust on access
6
7
4
› May end up with some nodes very deep
5
5
6
AVL Trees
• Don't balance
4
3
2
• Insert: 2, 4, 6, 8, 10, 12 into an empty BST
› Problem: Lack of “balance”:
3
Balanced and unbalanced BST
4
› What happens when you Insert elements in
ascending order?
• compare depths of left and right subtree
AVL Trees
4
• Worst case running time is O(N)
› Unbalanced degenerate tree
• So, best case running time of BST
operations is O(log N)
2
2
› Self-adjusting
7
AVL Trees
5
AVL Trees
6
1
Balancing Binary Search
Trees
Perfect Balance
• Want a complete tree after every operation
• Many algorithms exist for keeping
binary search trees balanced
› tree is full except possibly in the lower right
• This is expensive
› Adelson-Velskii and Landis (AVL) trees
(height-balanced trees)
› Weight-balanced trees
› Red-black trees;
› Splay trees and other self-adjusting trees
› B-trees and other (e.g. 2-4 trees) multiway
search trees
AVL Trees
› For example, insert 2 in the tree on the left and
then rebuild as a complete tree
6
5
4
1
7
9
5
Insert 2 &
complete tree
8
1
2
8
4
6
AVL Trees
9
8
AVL - Good but not Perfect
Balance
AVL Trees (1962)
• AVL trees are height-balanced binary
search trees
• Balance factor of a node
• Named after 2 Russian mathematicians
• Georgii Adelson-Velsky (1922 - ?)
• Evgenii Mikhailovich Landis (1921-1997)
› height(left subtree) - height(right subtree)
• An AVL tree has balance factor
calculated at every node
› For every node, heights of left and right
subtree can differ by no more than 1
› Store current heights in each node
AVL Trees
9
Height of an AVL Tree
› N(0) = 1, N(1) = 2
› N(h) = N(h-1) + N(h-2) + 1
h-1
h-2
• Solution (recall Fibonacci analysis)
› N(h) > φh (φ ≈ 1.62)
AVL Trees
10
Height of an AVL Tree
• N(h) = minimum number of nodes in an
AVL tree of height h.
h
• Basis
• Induction
AVL Trees
11
• N(h) > φh (φ ≈ 1.62)
• Suppose we have n nodes in an AVL
tree of height h.
› n > N(h)
› n > φh hence logφ n > h (relatively well
balanced tree!!)
› h < 1.44 log2n (i.e., Find takes O(logn))
AVL Trees
12
2
Node Heights
Tree A (AVL)
2
Tree A (AVL)
2
Tree B (AVL)
2
6
1
Node Heights after Insert 7
6
4
0
1
1
1
9
4
9
4
6
Tree B (not AVL)
balance factor
1-(-1) = 2
3
6
1
1
2
9
4
9
0
0
0
0
0
0
0
0
0
0
1
1
5
1
5
8
1
5
7
1
5
8
-1
0
height of node = h
balance factor = hleft-hright
empty height = -1
height of node = h
balance factor = hleft-hright
empty height = -1
AVL Trees
13
7
AVL Trees
14
Insert and Rotation in AVL
Trees
Worst-case AVL Trees
4 nodes: h = 3 vs 3
7 nodes: h = 4 vs 3
12 nodes: h = 5 vs 4
20 nodes: h = 6 vs 5
33 nodes: h = 7 vs 6
54 nodes: h = 8 vs 6
88 nodes: h = 9 vs 7
143 nodes: h = 10 vs 8
232 nodes: h = 11 vs 8
376 nodes: h = 12 vs 9
609 nodes: h = 13 vs 10
986 nodes: h = 14 vs 10
1596 nodes: h = 15 vs 11
• Insert operation may cause balance factor
to become 2 or –2 for some node
› only nodes on the path from insertion point to
root node have possibly changed in height
› So after the Insert, go back up to the root
node by node, updating heights
› If a new balance factor (the difference hlefthright) is 2 or –2, adjust tree by rotation around
the node
AVL Trees
15
AVL Trees
Single Rotation in an AVL
Tree
1
Double rotation
2
2
3
6
6
20
4
2
1
9
4
16
3
1
1
3
1
8
10
30
10
0
0
1
0
0
0
0
0
1
5
8
1
5
7
9
5
2
0
Imbalance 25
20
0
40
2
35
1
5
40 1
30
0
0
1 35
7
Insertion of 34 0
AVL Trees
17
45 0
0 25
34
45
34
AVL Trees
18
3
Insertions in AVL Trees
AVL Insertion: Outside Case
Let the node that needs rebalancing be α.
There are 4 cases:
Outside Cases (require single rotation) :
1. Insertion into left subtree of left child of α.
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 α.
k
h
Z
h
h
X
The rebalancing is performed through four
separate rotation algorithms.
AVL Trees
j
Consider a valid
AVL subtree
Y
19
AVL Trees
20
AVL Insertion: Outside Case
AVL Insertion: Outside Case
j
j
k
h+1
Inserting into X
destroys the AVL
property at node j
k
h
Z
h
Do a “right rotation”
h
h+1
Z
h
Y
Y
X
X
AVL Trees
21
Single right rotation
j
k
h+1
AVL Trees
Outside Case Completed
“Right rotation” done!
(“Left rotation” is mirror
symmetric)
k
Do a “right rotation”
h
h
22
j
h+1
Z
h
h
Y
X
X
Y
Z
AVL property has been restored
AVL Trees
23
AVL Trees
24
4
AVL Insertion: Inside Case
AVL Insertion: Inside Case
j
Consider a valid
AVL subtree
j
Inserting into Y
destroys the
AVL property
at node j
k
k
h
Z
h
h
X
h
h
h+1
X
25
AVL Insertion: Inside Case
k
j
h
X
AVL Trees
j
Consider the structure
of subtree Y…
k
h
h
h
Z
h+1
X
Y
AVL Trees
26
AVL Insertion: Inside Case
“Right rotation”
does not restore
balance… now k is
out of balance
h+1
Z
Y
Y
AVL Trees
Does “right rotation”
restore balance?
Z
Y
27
AVL Trees
28
AVL Insertion: Inside Case
AVL Insertion: Inside Case
j
j
Y = node i and
subtrees V and W
k
X
k
h
i
h
h+1
Z
W
AVL Trees
V
29
Z
i
X
h or h-1
V
We will do a left-right
“double rotation” . . .
W
AVL Trees
30
5
Double rotation : first rotation
Double rotation : second
rotation
j
j
left rotation complete
i
i
Z
k
Z
k
W
W
V
X
Now do a right rotation
X
AVL Trees
V
31
Double rotation : second
rotation
AVL Trees
32
Implementation
right rotation complete
balance (1,0,-1)
Balance has been
restored
i
j
k
h
V
Once you have performed a rotation (single or double) you won’t
need to go back up the tree
Z
W
AVL Trees
33
Single Rotation
AVL Trees
34
Double Rotation
RotateFromRight(n : reference node pointer) {
p : node pointer;
p := n.right;
n
n.right := p.left;
p.left := n;
n := p
}
You also need to
modify the heights
or balance factors
of n and p
right
You can either keep the height or just the difference in height,
i.e. the balance factor; this has to be modified on the path of
insertion even if you don’t perform rotations
h
h or h-1
X
key
left
DoubleRotateFromRight(n : reference node pointer) {
RotateFromLeft(n.right);
n
RotateFromRight(n);
}
X
X
Z
Insert
Y
Z
V
AVL Trees
35
AVL Trees
W
36
6
Example of Insertions in an
AVL Tree
Insert in AVL trees
Insert(T : tree pointer, x : element) : {
if T = null then
T := new tree; T.data := x; height := 0;
case
T.data = x : return ; //Duplicate do nothing
T.data > x : return Insert(T.left, x);
if ((height(T.left)- height(T.right)) = 2){
if (T.left.data > x ) then //outside case
T = RotatefromLeft (T);
else
//inside case
T = DoubleRotatefromLeft (T);}
T.data < x : return Insert(T.right, x);
code similar to the left case
Endcase
T.height := max(height(T.left),height(T.right)) +1;
return;
}
3
20
1
2
10
30
3
AVL Trees
2
2
2
10
30
10
1
1
25
5
3
30
1
5
4
20
20
1
35
38
Single rotation (outside case)
4
20
5
35
37
Example of Insertions in an
AVL Tree
1
1
1
25
AVL Trees
Insert 5, 40
2
35
25
2
4
2
10
30
10
1
1
5
25
3
1
35
5
20
3
30
1
40
Now Insert 45
1
Imbalance
39
5
3
5
4
4
2
30
10
3
1
Imbalance 25
• Similar but more complex than insertion
20
3
35
1
40
2
5
40 2
30
1
2 35
Insertion of 34 1
45 1
1 25
40
AVL Tree Deletion
4
10
45
Now Insert 34
AVL Trees
Double rotation (inside case)
20
1
35
2 40
1 45
AVL Trees
40 2
25
1
34
› Rotations and double rotations needed to
rebalance
› Imbalance may propagate upward so that
many rotations may be needed.
45
34
AVL Trees
41
AVL Trees
42
7
Deletion
Deletion
6
6
100
100
5
4
3
17
62
1
117
2
1
111
1
72
162
3
2
132
150
2
78
50
1
2
101 128
144
44
3
4
1
32
5
4
144
44
2
3
17
62
2
1
178
1
1
132 147
1
117
2
1
32
1
1
111
1
72
162
2
150
1
2
101 128
1
1
129
43
6
6
100
44
5
5
144
44
117
111
3
2
132
150
1
72
1
2
101 128
44
1
117
78
2
17
72
50
111
1
1
172 181
1
129
Deletion
6
6
100
100
5
3
5
62
144
2
1
46
144
78
3
4
117
162
3
2
132
150
2
1
72
111
1
1
2
101 128
AVL Trees
1
172 181
AVL Trees
3
2
178
1
1
132 147
62
44
50
1
2
1
Deletion
1
2
150
101 128
45
2
3
132
129
AVL Trees
17
162
2
178
1
132 147
3
4
162
2
78
144
62
3
4
50
1
172 181
Deletion
100
62
1
1
AVL Trees
Deletion
17
178
132 147
129
AVL Trees
2
3
132
2
78
50
172 181
3
4
1
132 147
2
1
178
1
1
172 181
17
2
2
4
3
44
78
117
162
1
3
2
1
50 72
2
128
132
1
1
129
129
47
AVL Trees
178
150
1
1
101
2
2
132
111
1
147
1
1
172 181
48
8
Deletion
Deletion
100
100
144
62
44
17
117
78
50 72
101
162
132
111
128
132
117
62
150
147
44
178
17
78
144
111
50 72
132
101
172 181
128
129
162
132
129
AVL Trees
49
178
150
147
172 181
AVL Trees
Deletion
50
Deletion
5
117
117
144
100
4
4
100
144
3
62
111
132
162
44
17
78
101
50 72
128
132
129
150
147
1
172 181
AVL Trees
51
111
17
1
3
162
1
2
1
78
101
2
1
128
50 72
1
129
AVL Trees
2
2
132
178
150
1
147
1
1
172 181
52
Non-recursive insertion or the
hacker’s delight
Pros and Cons of AVL Trees
Arguments for AVL trees:
1. Search is O(log n) since AVL trees are always balanced.
2. Insertion and deletions are also O(log n)
3. The height balancing adds no more than a constant factor to the
speed of insertion.
Arguments against using AVL trees:
1. Difficult to program & debug; more space for balance factor.
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).
AVL Trees
44
178
3
132
2
62
2
53
• Key observations;
› At most one rotation
› Balance factor: 2 bits are sufficient (-1 left,
0 equal, +1 right)
› There is one node on the path of insertion,
say S, that is “critical”. It is the node where
a rotation can occur and nodes above it
won’t have their balance factors modified
AVL Trees
54
9
Non-recursive insertion
Non-recursive insertion (ct’d)
• Step 1 (Insert and find S):
• Step 3 (Balance if necessary):
› Find the place of insertion and identify the last node S on the
path whose BF ≠ 0 (if all BF on the path = 0, S is the root).
› Insert
› If BF(S) = 0 (S was the root) set BF(S) to the direction of
insertion (the tree has become higher)
› If BF(S) = -1 (+1) and we traverse right (left) set BF(S) = 0
(the tree has become more balanced)
› If BF(S) = -1 (+1) and we traverse left (right), the tree
becomes unbalanced. Perform a single rotation or a double
rotation depending on whether the path is left-left (right-right)
or left-right (right-left)
• Step 2 (Adjust BF’s)
› Restart from the child of S on the path of insertion. (note: all
the nodes from that node on on the path of insertion have BF = 0.)If
the path traversed was left (right) set BF to –1 (+1) and
repeat until you reach a null link (at the place of insertion)
AVL Trees
55
AVL Trees
56
Non-recursive Insertion with
BF’s
Step 1 & 2
Step 3
+1
S
20
-1
+1
-1
10
30
10
0
0
5
25
0 ->1
0
35
0
40
+1
20
0
5
30
40 1
0
Insertion of 34
0->-1 35
45 0
0 25
34 0
45
0 34
AVL Trees
57
10