Self‐Adjusting Data Structures
1
Self Adjusting Data Structures 1 Self Adjusting Data Structures t - - PowerPoint PPT Presentation
Self Adjusting Data Structures 1 Self Adjusting Data Structures t ve to front 2 7 4 1 9 5 3 Search(2), Search(2), Search(2) , Search(5), Search(5), Search(5) mov 5 2 7 4 1 9 3 Lists [D.D. Sleator, R.E. Tarjan,
1
t
Search(2), Search(2), Search(2) , Search(5), Search(5), Search(5)
ve‐to‐front
Lists [D.D. Sleator, R.E. Tarjan, Amortized Efficiency of List Update Rules, Proc. 16th Annual ACM Symposium on
mov
[ , j , ff y f p , y p Theory of Computing, 488‐492, 1984] Dictionaries [D.D. Sleator, R.E. Tarjan, Self‐Adjusting Binary Search Trees, Journal of the ACM, 32(3): 652‐686, 1985] → splay trees → splay trees Priority Queues [C.A. Crane, Linear lists and priority queues as balanced binary trees, PhD thesis, Stanford University, 1972] [D.E. Knuth. Searching and Sorting, volume 3 of The Art of Computer Programming, Addison‐Wesley, 1973] l f h → leftist heaps [D.D. Sleator, R.E. Tarjan, Self‐Adjusting Heaps, SIAM Journal of Computing, 15(1): 52‐69, 1986] → skew heaps [C Okasaki Alternatives to Two Classic Data Structures Symposium on Computer Science Education 162‐
2
[C. Okasaki, Alternatives to Two Classic Data Structures, Symposium on Computer Science Education, 162 165, 2005] → maxiphobix heaps Okasaki: maxiphobix heaps are an alternative to leftist heaps ... but without the “magic”
5 2 3
2 2 3
[C.A. Crane, Linear lists and priority queues as balanced binary
Meld Cut root + Meld
= =
4 6 8 9 7
1 1 1 1 2 1
trees, PhD thesis, Stanford University, 1972] [D.E. Knuth. Searching and Sorting, volume 3 of The Art of Computer Programming, Addison‐Wesley, 1973]
8 10 11 12
1 1 2 1
2
2 3 3
Meld ( , ) =
13 13 4 7 9
2 2 4 2 3
4 13
2 1 2
7 2
2 2 3
9 13
1 1 1 1
9
4 1 1
= Meld ( , ) Meld ( , )
T
y x x
[C. Okasaki, Alternatives to Two Classic Data Structures, Symposium on Computer Science Education, 162‐165, 2005]
( , )
3
T3 T1 T2 Ti Tj Tk
largest size two smallest
x < y
[D.D. Sleator, R.E. Tarjan, Self‐Adjusting Heaps, SIAM Journal of Computing, 15(1): 52‐69, 1986]
2
Meld Cut root + Meld
= =
5 3 4 6 8 10 9 7 12
Meld Cut root Meld 2
10
Meld ( , ) =
4 7 4 13 7 2
v heavy if |Tv| > |Tp(v)|/2, otherwise light ⇒ any path ≤ log n light nodes
9 13 9
Potential Φ = # heavy right children in tree
4
[D.D. Sleator, R.E. Tarjan, Self‐Adjusting Heaps, SIAM Journal of Computing, 15(1): 52‐69, 1986]
1 1
Meld ( , ) =
4 2 4 13 2 1 5 3 4 2 6 3
=
7 9 7 9 8 11 6 11 5 8 13
Φ = # heavy right children in tree + 2 ∙ # light children on minor & major path
O(1) amortized Meld Heavy right child on merge path before meld → replaced by light child ⇒ 1 potential released Light nodes disappear from major paths (but might → heavy) ⇒ ≥ 1 potential released and become a heavy or light right children on major path ⇒ potential increase by ≤ 4
4 5
5
and become a heavy or light right children on major path ⇒ potential increase by ≤ 4 O(log n) amortized DeleteMin Cutting root ⇒ 2 new minor paths, i.e. ≤ 2∙log n new light children on minor & major paths
4 5
[D.D. Sleator, R.E. Tarjan, Self‐Adjusting Binary Search Trees, Journal of the ACM, 32(3): 652‐686, 1985]
i i zig zag
y x C x y A
zig
y x y z
zig‐zig
z D x A y x x z
zig‐zag
z D y
root
B A C B B A C D C B C B A D C B A
v u x
zag‐zag i i insert
y F z A E z C
zig‐zig
u v D y x u v D y
insert
6
D C x B A B A C F E D B A z C F E
[D.D. Sleator, R.E. Tarjan, Self‐Adjusting Binary Search Trees, Journal of the ACM, 32(3): 652‐686, 1985]
7