Upload
mariah-boyd
View
220
Download
0
Embed Size (px)
Citation preview
CSE 326: Data StructuresLecture #11
AVL and Splay Trees
Steve Wolfman
Winter Quarter 2000
Today’s Outline
• Extra operations in AVL trees • Splaying and Splay Trees
AVL Tree Dictionary Data Structure
4
121062
115
8
14137 9
• Binary search tree properties– binary tree property
– search tree property
• Balance property– balance of every node is:
-1 b 1– result:
• depth is (log n)
15
Deletion (Hard Case #1)
2092
175
10
303
121 100
2 2
3
00
Delete(12)
Single Rotation on Deletion
2092
175
10
303
1 10
2 2
3
00
3092
205
10
17
3
1 00
2 1
3
0
0
Something very bad happened!
Deletion (Hard Case #2-4)
Delete(9)
2092
175
10
303
121 220
2 3
4
0
33
15
130 0
1
0
20
30
12
33
15
13
1
0 0
110
180
Double Rotation on Deletion
2
3
0
202
173
10
30
121 22
2 3
4
33
15
13
1
0 0
111
0180
2052
173
10
30
120 220
1 3
4
33
15
13
1
0 0
111
01800
Deletion with Propagation: Choose Your Own Adventure!
We get to choose whether to single or double rotate!
If you take the gold and single rotate, flip to Slide 10.
If you decide to search further into the scary cavern, continue on to the next slide.
If you decide not to rotate atall, jump to Slide 11.
2052
173
10
30
120 220
1 3
4
33
15
13
1
0 0
111
0180
Propagated Double Rotation
0
17
12
11
52
3
10
4
2 3
1 0
0 0
2052
173
10
30
120 220
1 3
4
33
15
13
1
0
111
0180
151
0
20
30
33
1180
130
2
Propagated Single Rotation
0
30
20
17
33
12
15
13
1
0
52
3
10
4
3 2
1 2 1
0 0 011
0
2052
173
10
30
120 220
1 3
4
33
15
13
1
0
111
0180
180
You Didn’t Rotate
Casually walking away from the AVL tree, minding your own business, you fall into a sewer and are eaten by wild Red-Black trees (some people say they grow as big as B-Trees in the sewers!).
You die.
AVL Deletion Algorithm• Recursive1. Search downward for
node
2. Delete node
3. Unwind stack,
correcting heights
a. If imbalance #1,
single rotate
b. If imbalance #2,
double rotate
• Iterative1. Search downward for
node, stacking
parent nodes
2. Delete node
3. Unwind stack,
correcting heights
a. If imbalance #1,
single rotate
b. If imbalance #2,
double rotate
AVL buildTree8 10 15 20 30 35 405
17
17
8 10155 20303540
Divide & Conquer– Divide the problem into parts
– Solve each part recursively
– Merge the parts into a general solution
How long does divide & conquer take?
BuildTree Example
35
17
15
5
8
10
3
2 2
1 0
0
301
40
200
0
8 10 15 20 30 35 405 17
8 10 155
85
30 35 4020
3020
BuildTree Analysis(Approximate)
T(1) = 1
T(n) = 2T(n/2) + 1
T(n) = 2(2T(n/4)+1) + 1
T(n) = 4T(n/4) + 2 + 1
T(n) = 4(2T(n/8)+1) + 2 + 1
T(n) = 8T(n/8) + 4 + 2 + 1
T(n) = 2kT(n/2k) +
let 2k = n, log n = k
T(n) = nT(1) +
T(n) = (n)
k
i
i
1
22
1
n
i
ilog
1
22
1
2
1n
2
1n
2
1n
2
1n
BuildTree Analysis (Exact)
Precise Analysis: T(0) = bT(n) = T( ) + T( ) + c
By induction on n:
T(n) = (b+c)n + b
Base case:
T(0) = b = (b+c)0 + b
Induction step:
T(n) = (b+c) + b +
(b+c) + b + c
= (b+c)n + b
QED: T(n) = (b+c)n + b = (n)
12
1
2
1
nnn
Thinking About AVL
• Observations+ Worst case height of an AVL tree is about 1.44 log n
+ All operations supported in worst case O(log n)
+ Only one (single or double) rotation needed on insertion
– O(log n) rotations needed on deletion
– Height fields must be maintained (or 2-bit balance)
? Coding complexity
Splay Trees
• Problems with AVL Trees– extra storage/complexity for height fields
– ugly delete code
• Solution: splay trees– blind adjusting version of AVL trees
– amortized time for all operations is O(log n)
– worst case time is O(n)
– insert/find always rotates node to the root!
Idea
17
10
92
5
3
You’re forced to make a really deep access:
Since you’re down there anyway,fix up a lot of deep nodes!
Zig-Zig*
*I told you it was a technical term!
n
Z
Y
p
X
g
W
g
W
X
p
Y
n
Z
Zig-Zag*
g
Xp
Y
n
Z
W
*This is just a double rotation
n
Y
g
W
p
ZX
Zig
p
X
n
Y
Z
n
Z
p
Y
X
root root
Splaying Example
2
1
3
4
5
6
Find(6)
2
1
3
6
5
4
zig-zig
Still Splaying 6
zig-zig2
1
3
6
5
4
1
6
3
2 5
4
Almost There, Stay on Target*
zig
1
6
3
2 5
4
6
1
3
2 5
4
Splay Again
Find(4)
zig-zag
6
1
3
2 5
4
6
1
4
3 5
2
Example Splayed Out
zig-zag
6
1
4
3 5
2
61
4
3 5
2
To Do
• Turn in Project II• Read chapter 4 in the book• Do the new worksheet• Prepare for the midterm• Finish HW#4
– Just kidding
Coming Up
• Polish off Splay Trees• Talk about Project III• Second project due (today)• Midterm (Friday)