Professional Documents
Culture Documents
1
• https://www.youtube.com/watch?v=aQS9D
qLWxw4
• https://www.cs.usfca.edu/~galles/visualizati
on/AVLtree.html
2
AVL Trees
Adel’son-Vel’skii and Landis
4
Balancing Factor
height(right subtree) – height(left subtree)
0 0 –1 0
5
Height of an AVL Tree
Note: “longest” possible heap Always O(log n)
with n nodes.
But cannot search efficiently
We’ll now see that the height of an AVL tree T storing n keys is O(log n).
6
Note: AVL tree with the highest possible number
of internal nodes for a given height h:
Perfect:
h
n= 2h
So h = log n
7
Easy to see that n(1) = 1 and n(2) = 2
(please note that dummy nodes are not shown but contributing to the height)
1 n(1) = 1
2 n(2) = 2
8
n(h): the minimum number of internal nodes of
an AVL tree of height h.
9
Height of an AVL Tree
11
We can continue:
n(h) > 2n(h-2)
n(h) > 4n(h-4)
n(h) > 8n(h-6)
…
n(h) > 2in(h-2i)
12
n(h) > 2in(h-2i) And we know that
n(1) = 1
n(2) = 2
h-2i = 2
Now we pick I such that h is either 1 or 2
That is pick i= ceil(h/2)-1, and substitute:
for i = ceil(h/2) - 1 n(h) > 2ceil(h/2) - 1 n(1)
14
Insertion
A +1 A 0 A +2
re-balancing
A –1 A –2 A 0
15
re-balancing
Rebalancing after insertion
16
Rebalancing
Examples …..
17
Rebalancing
Examples …..
z z
y y
x x
18
Rebalancing
Examples …..
z z
y
y
x T4
x
T4
T1
T3
T1
T2 T3 T2
19
Rebalancing
Example
c=z
a=y
b= x
20
Rebalancing
a c
T1 T2 T3 T4
Rebalance done!
21
Example: after
inserting 54
44
c
z 6
17 a2 78 7
y
xb
32 1 50 4 88
48 3 62
5
54 T3
T0 T2
T1
44 b
4 x
17 62 z6
a 2y c
32 1 50 3 78 7
The image part with relationship ID
rId3 was not found in the file.
5
48 54 88
T2
T0 T1 T3
22
Does this really work?
We need to see that the new tree is :
23
We consider 2 types of examples
Example 1
a
z y
y b
z x
h
x c
T1 h h+2
h h h-1 h
T2 h-1 T1 T2 T3 T4
h
T3 T4
Inorder: T1 z T2 y T3 x T4
24
Example 2
x
z c
y z
a y
b h
x h+2 h h-1 h h
T4
h
T1 T2 T3 T4
h-1 h
T1
T2 T3
Inorder: T1 y T2 x T3 z T4
25
An Observation…
26
rebalance (v) T1 <- a.left; T4 <- c.right
x <- v; Y <- x.parent; z <- y.parent b.left <- a; b.right <- c
while (z.isBalanced and not(z.isRoot) ) a.left <- T1; a.right <- T2
x <- y; y <- z; z <- z.parent c.left <- T3; c.right <- T4
if ( not z.isBalanced) T1.parent <- a; T2.parent <-a
if ( x = y.left) { x<=y} T3.parent <- b; T3.parent <- c
if (y = z.left) {x<=y<=z}
a <- x; b <- y; c<- z; if (z.isRoot) then
T2 <- x.right; T3 <- y.right; root <- b
else { z<=x<=y} b.parent <- NULL
a <- z; b <- x; c <- y; else if (z.isLeftChild)
T2 <- x.left; T3 <- x.right; z.parent.left<-b
else {y<=x} else z.parent.right <- b
if (y = z.left) {y<=x<=z} b.parent <- z.parent
a <- y; b <- x; c <- z; a.parent <- b; c.parent <- b
T2 <- x.left; T3 <- x.right
else { z<=y<=x}
a <- z; b <- y; c <- x;
T2 <- y.left; T3 <- x.left
27
Restructuring
(as Single Rotations)
28
Restructuring
(as Double Rotations)
29
Removal
1
44
y b3
xc
17 62
2 2
50 78
1 1 0 1
T0 48 54 88
T2
32
T1 T3
y
4
62
3
z x
2
44 78
1
2
0 1
Whew,
17 50
1 1
88
balanced!
48 54 T2
31
T0 T3
Removal (contd.)
• we could choose a different x:
az 4
44
y
1
17 62
c
3
x
2 b
50 78
2 Oh no,
T0
1
48 54
1 0
88
1
unbalanced!
32
T1 T2 T3
b
4 x
50
2
z a c
y
Whew,
3
44 62
1
17
1
48
1
54 78
2 balanced!
0 1
88
32
T0 T1 T2
Again.. x, y, z
33
COMPLEXITY
Searching: findElement(k):
Inserting: insertItem(k, o):
Removing: removeElement(k):
O(log n)
34
Some implementation details are very important:
35
Trinode restructuring using rotation operation:
36
Rotate:
Restructure:
37
Rebalancing operation for AVL insertions and deletions:
38
At this point in the class I discuss how AVL trees are
implemented in the 6th edition of the textbook by
Goodrich, Tamassia and Goldwasser.