Professional Documents
Culture Documents
ADS01AVL
ADS01AVL
ADS01AVL
1
AVL Trees, Splay Trees, and Amortized Analysis
AVL Trees
Target : Speed up searching (with insertion and
deletion)
root
Tool : Binary search trees
small large
2
AVL Trees, Splay Trees, and Amortized Analysis
A balanced tree 3
AVL Trees, Splay Trees, and Amortized Analysis
2
121
01
May
210 00
Mar Nov
10
Aug
0
Apr
6
AVL Trees, Splay Trees, and Amortized Analysis
Jan 2
112
01 Double Rotation
1
2
01
0
May
1
010 00 Mar
1
010 1
0
Aug Nov
0 01 Aug May
0 0 0
Apr Mar
Apr Jan Nov
0 LR
Jan
Rotation
In general:
1 LR 2 LR
0
A A 0
Insertion 1 Rotation
B C
0 B 1 0 or 1 1 or 0
C AR C AR
B A
BL BL
CL CR CL CR CL CR
BL AR
OR
OR
7
AVL Trees, Splay Trees, and Amortized Analysis
Dec July Feb
1
2
01
21
Mar 1
2
01
1
RL
1
2
10 1
0 Mar
Aug May Rotation 1
02
1
0 1
0
0 01 0 Dec May
Apr Jan 10 01
10 0
Nov
1
0 0 Aug Jan Nov
Dec July 0 0 0
0 Apr Feb July
Feb
In general:
1 RL 2 RL
A 0 A 0
Insertion 1 Rotation
B B C
0 1 0 or 1 1 or 0
AL C AL C A B
BR BR
CL CR CL CR CL CR
AL BR
OR
OR 8
AVL Trees, Splay Trees, and Amortized Analysis
June Oct Sept
1
0 2
1
01
21
1
0
Jan
Mar
1
12
1
0 1
02
1
0 Jan 1
01
0
1
12
1
0 1
0
Dec Mar
Dec Dec MarMay
1 10 0 01
10 1 2
10
1 Aug 0 1
Jan 1
0
Aug Feb July May
Nov
Aug Feb0 July 1
0 Nov
0 0 0 1
0
0 Apr Feb 0 July 1
Apr June 0 Nov 0
Apr June May
0 Oct
0
June Oct 0
Sept
Note: Several bf’s
might be changed even if
we don’t need to reconstruct
the tree.
A A
nh = Fh+2 1, for h 0 i
1 1 5
Fibonacci number theory gives that Fi
h 2 5 2
1 1 5
nh 1 h O(ln n)
5 2
10
AVL Trees, Splay Trees, and Amortized Analysis
Splay Trees
11
AVL Trees, Splay Trees, and Amortized Analysis
k5
k1 k5
k5 k5
k4
k2 k4 k5 F
k4 F
k3 k1 F F
A k3 k4 E
B k1 E F
k2 k2 k4
E
k1 k3 D
k2 k1 k3D E
A k3
A k2 B E
C CC D
A B B D
C C D
A B
Does NOT work!
12
AVL Trees, Splay Trees, and Amortized Analysis
An even worse case:
Insert: 1, 2, 3, … N 1 2
3 N Find: 1 1
2 N
1 2 3
1
3
2 3
1 2
Find: 2 2 …… Find: N N
1 N
3
2 T (N) = O ( N2 )
1
13
AVL Trees, Splay Trees, and Amortized Analysis
Zig-zag G X
Double rotation
P
D P G
X
A
A B C D
B C
Zig-zig G X
P A P
D
X Single rotation B G
C
A B C D
14
AVL Trees, Splay Trees, and Amortized Analysis
15
AVL Trees, Splay Trees, and Amortized Analysis
Insert: 1, 2, 3, 4, 5, 6, 7 Find: 1
7 7 7 1
6 6 6 6
5 5 1 4 7
4 4 4 2 5
3 1 2 5 3
2 2 3
1 3
Read the 32-node example
given in Figures 4.52 – 4.60
16
AVL Trees, Splay Trees, and Amortized Analysis
Deletions:
X will be at the root.
Step 1: Find X ;
There will be two
subtrees TL and TR .
Step 2: Remove X ;
The largest element
Step 3: FindMax ( TL ) ; will be the root of TL ,
and has no right
child.
17
AVL Trees, Splay Trees, and Amortized Analysis
Research Project 1
Binary Search Trees (26)
18
AVL Trees, Splay Trees, and Amortized Analysis
Amortized Analysis
Probability
is not involved
Aggregate analysis
Accounting method
Potential method
19
AVL Trees, Splay Trees, and Amortized Analysis
Aggregate analysis
Idea : Show that for all n, a sequence of n
operations takes worst-case time T(n) in
Wetotal. Ineach
can pop the object
worst case, the average cost,
from the or amortized
stack cost,
at most once forper
eachoperation is
therefore
time T(n)/n.it
we have pushed Total =
onto the stack O( n2 ) ?
〖 Example 〗 Stack with MultiPop( int k, Stack S )
Algorithm { Consider a sequence of n
while ( !IsEmpty(S) && k>0 ) { Push, Pop, and MultiPop
Pop(S); operations on an initially
k - -;
} /* end while-loop */ empty stack.
}
T = min ( sizeof(S), k ) sizeof(S) n
Tamortized= O( n )/n = O(1)
20
AVL Trees, Splay Trees, and Amortized Analysis
Accounting method
Idea : When an operation’s amortized cost ĉi
exceeds its actual cost ci , we assign the
difference to specific objects in the data
Savings structure as credit. Credit can help pay
Account for later operations whose amortized
cost is less than their actual cost.
cˆ c
i 1
i
i 1
i
Tamortized =
n
21
AVL Trees, Splay Trees, and Amortized Analysis
22
AVL Trees, Splay Trees, and Amortized Analysis
Potential method
Idea : Take a closer look at the credit --
cˆ i ci Credit i ( Di ) ( Di 1 )
Potential function
n n
cˆ c
i 1
i
i 1
i ( Di ) ( Di 1 )
n
ci ( Dn ) ( D0 )
i 1
0
In general, a good potential function should always
assume its minimum at the start of the sequence.
23
AVL Trees, Splay Trees, and Amortized Analysis
cˆ O(1) O(n) c
i 1
i
i 1 i 1
i Tamortized= O( n )/n = O(1)
24
AVL Trees, Splay Trees, and Amortized Analysis
25
(T ) Rank ( i ) AVL Trees, Splay Trees, and Amortized Analysis
iT
Zig P X cˆ i 1 R2 ( X ) R1 ( X )
Single rotation
X
C A P R2 ( P ) R1 ( P )
A B B C 1 R2 ( X ) R1 ( X )
Zig-zag G X cˆ i 2 R2 ( X ) R1 ( X )
Double rotation
P
D
R2 ( P ) R1 ( P )
cXˆi 1 3 R2 ( X ) R1 ( X )
P G
R2 (G ) R1 (G )
A
A B C D 2 R2 ( X ) R1 ( X )
B C
Lemma 11.4 on [1] p.448
Zig-zig G X
cˆ i 2 R2 ( X ) R1 ( X )
P A P
D R2 ( P ) R1 ( P )
X Single rotation B G
C R2 (G ) R1 (G )
A B C D 3 R2 ( X ) R1 ( X )
27