ADS01AVL

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 27

AVL Trees, Splay Trees,

and Amortized Analysis

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

Problem : Although Tp = O( height ), but the


height can be as bad as O( N ).

2
AVL Trees, Splay Trees, and Amortized Analysis

〖 Example 〗 2 binary search trees obtained for the


months of the year
Jan
Average search time = 3.5
Feb Mar
Apr June May
Average search
Aug July Sept time of the skew
Dec Oct tree = 6.5
Entered from Jan to Dec Nov

July Average search time = 3.1


Feb May
Aug Jan Mar Oct
Apr Dec June Nov Sept

A balanced tree 3
AVL Trees, Splay Trees, and Amortized Analysis

Adelson-Velskii-Landis (AVL) Trees (1962)


【 Definition 】 An empty binary treeThe height of
is height an empty If
balanced. tree
T is a
is defined to be –1.
nonempty binary tree with TL and TR as its left and right
subtrees, then T is height balanced iff
(1) TL and TR are height balanced, and
(2) | hL  hR |  1 where hL and hR are the heights of TL and TR ,
respectively.
【 Definition 】 The balance factor BF( node ) = hL  hR . In an
AVL tree, BF( node ) = 1, 0, or 1.
4 5 7
3 5 2 8 2 8
2 6 1 4 7 1 4
1 7 3 3 5
4
AVL Trees, Splay Trees, and Amortized Analysis

〖 Example 〗 Input the months Mar May Nov


1
2
0
Mar 1
2
01
0
1
0 Single rotation May
May 00 00
0 Mar Nov
Nov
 The trouble maker Nov is in the right subtree’s right
subtree of the trouble finder Mar. Hence it is called an
RR rotation.
A is not necessarily
In general: the root of the tree
2 0
1
RR B
A A 1 RR 0
0
Insertion A
B B Rotation
AL BR
AL
BL BR AL BL
BL BR
5
AVL Trees, Splay Trees, and Amortized Analysis
Aug Apr

2
121
01
May
210 00
Mar Nov
10
Aug
0
Apr

Discussion 1: What can we do now?

6
AVL Trees, Splay Trees, and Amortized Analysis
Jan 2
112
01 Double Rotation
1
2
01
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
01
21
Mar 1
2
01
1
RL
1
2
10 1
0 Mar
Aug May Rotation 1
02
1
0 1
0
0 01 0 Dec May
Apr Jan 10 01
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
01
21
1
0
Jan
Mar
1
12
1
0 1
02
1
0 Jan 1
01
0
1
12
1
0 1
0
Dec Mar
Dec Dec MarMay
1 10 0 01
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.

Another option is to keep a height field for each node.


Read the declaration and functions in [1] Figures 4.42 – 4.48
9
AVL Trees, Splay Trees, and Amortized Analysis
Let nh be the minimum number of nodes in a height balanced tree of
height h. Then the tree must look like

A A

One last question:


h2 h1 OR h1 h2  nh = nh1 + nh2 + 1
Obviously we have Tp = O( h )
where h is the height of the tree.
But h = ?
Fibonacci numbers:
F0 = 0, F1 = 1, Fi = Fi1 + Fi2 for i > 1

 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

Target : Any M consecutive tree operations


starting from an empty tree take at most
O(M log N) time.
ButDoes
Soif aone node
single
it mean takes
operationO(N) time
might
that every
Sure we can – that
The bound is weaker.only means
No. It means that the to access,
operation wetake
still
takescan keepN)
O(N)
O(log accessing
time? it
time?
that whenever a node is
But the effect is the same:accessed,
amortized time is O(log N). for
ThenM times,
what’scan’t we?
the point?
it must
There are be input
no bad moved.sequences.

Idea : After a node is accessed, it is pushed to


the root by a series of AVL tree rotations.

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

Try again -- For any nonroot node X , denote its parent


by P and grandparent by G :
Case 1: P is the root Rotate X and P
Case 2: P is not the root

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

Splaying not only moves the accessed


node to the root, but also roughly
k
halves the depth of most nodes on the5
path.
k4
F
k13
E
k2 k3 k4
D
k1
A B Ck3 D k5
A
B C
C D E F

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.

 Step 4: Make TR the right child of the root of TL .

Are splay trees really better


than AVL trees?

17
AVL Trees, Splay Trees, and Amortized Analysis

Research Project 1
Binary Search Trees (26)

This project requires you to implement operations on


unbalanced binary search trees, AVL trees, and splay trees. You are
to analyze and compare the performances of a sequence of insertions
and deletions on these search tree structures.

Detailed requirements can be found at https://pintia.cn/ .

18
AVL Trees, Splay Trees, and Amortized Analysis

Amortized Analysis

Target : Any M consecutive operations take at


most O(M log N) time.
-- Amortized time bound

worst-case bound  amortized bound  average-case bound

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.

Note: For all sequences of n operations, we must have


n n

 cˆ   c
i 1
i
i 1
i
Tamortized =
n

21
AVL Trees, Splay Trees, and Amortized Analysis

〖 Example 〗 Stack with MultiPop( int k, Stack S )


c i for Push: 1 ; Pop: 1 ; and MultiPop: min ( sizeof(S), k )

ĉ i for Push: 2 ; Pop: 0 ; and MultiPop: 0

Starting from an empty stack —— Credits for


Push: +1 ; Pop: –1 The amortized
; and MultiPop: –1 for each +1
costs of the operations
sizeof(S)  0 may differ
Credits 0 from
each other
n n
O( n)   cˆ i   ci
i 1 i 1

Tamortized= O( n )/n = O(1)

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

〖 Example 〗 Stack with MultiPop( int k, Stack S )


Di = the stack that results after the i-th operation
( Di ) = the number of objects in the stack Di
( Di )  0  ( D0 )
Push: ( Di )  ( Di 1 )  ( sizeof ( S )  1)  sizeof ( S )  1
cˆ i  ci  ( Di )  ( Di 1 )  1  1  2
Pop: ( Di )  ( Di 1 )  ( sizeof ( S )  1)  sizeof ( S )  1
cˆ i  ci  ( Di )  ( Di 1 )  1  1  0
MultiPop: ( Di )  ( Di 1 )  ( sizeof ( S )  k  )  sizeof ( S )   k 
cˆ i  ci  ( Di )  ( Di 1 )  k   k   0
n n n

 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

〖 Example 〗 Splay Trees: Tamortized = O( log N )


Di = the root of the resulting tree
( Di ) = must increase by at most O( log N ) over n
steps, AND will also cancel out the number of
rotations (zig:1; zig-zag:2; zig-zig:2).

(T )   log S ( i ) where S(i) is the number of


iT descendants of i (i included).

Rank of the subtree


 Height of the tree

Why not simply use the heights


of the trees?

25
(T )   Rank ( i ) AVL Trees, Splay Trees, and Amortized Analysis
iT

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 )

【 Theorem 】 The amortized time to splay a tree with root T at


node X is at most 3( R( T ) – R ( X ) ) + 1 = O(log N).
26
Reference:
Data Structure and Algorithm Analysis in C (2nd Edition) :
Ch.4 , p.106-128 ; Ch.11 , p.447-451 ; M.A.Weiss
著、陈越改编,人民邮件出版社, 2005
Introduction to Algorithms, 3rd Edition: Ch.17 , p.
451-478 ; Thomas H. Cormen, Charles E. Leiserson,
Ronald L. Rivest and Clifford Stein. The MIT Press. 2009

27

You might also like