26.08.09IT 60101: Lecture #121 Foundation of Computing Systems Lecture 12 Trees: Part VII.
-
Upload
ursula-hubbard -
Category
Documents
-
view
218 -
download
1
description
Transcript of 26.08.09IT 60101: Lecture #121 Foundation of Computing Systems Lecture 12 Trees: Part VII.
![Page 1: 26.08.09IT 60101: Lecture #121 Foundation of Computing Systems Lecture 12 Trees: Part VII.](https://reader036.fdocuments.us/reader036/viewer/2022062503/5a4d1ad07f8b9ab059971391/html5/thumbnails/1.jpg)
26.08.09 IT 60101: Lecture #12 1
Foundation of Computing Systems
Lecture 12
Trees: Part VII
![Page 2: 26.08.09IT 60101: Lecture #121 Foundation of Computing Systems Lecture 12 Trees: Part VII.](https://reader036.fdocuments.us/reader036/viewer/2022062503/5a4d1ad07f8b9ab059971391/html5/thumbnails/2.jpg)
26.08.09 IT 60101: Lecture #12 2
Splay Tree• A binary search tree is one of the most important non-
linear data structures.
• a variety of methods are available to keep the tree balanced and hence to guarantee that operations such as searching, insertion and deletion can be done in O(log2n) time in the worst case.
• Height-balancing by AVL-rotations (AVL trees)
• Node colouring (red-black trees)
• No doubt that these trees are efficient so far searching, insertion and deletion of a node is concerned.
• But there are some situations, when the ways of balancing may not guarantee the optimal performance.
![Page 3: 26.08.09IT 60101: Lecture #121 Foundation of Computing Systems Lecture 12 Trees: Part VII.](https://reader036.fdocuments.us/reader036/viewer/2022062503/5a4d1ad07f8b9ab059971391/html5/thumbnails/3.jpg)
26.08.09 IT 60101: Lecture #12 3
Splay Tree• For example, consider the binary search trees T1 and T2
• Both the trees T1 and T2 contain the same set of key elements say, k1, k2, k3, k4, k5 in their five nodes such that k1 < k2 < k3 < k4 < k5
k2
k4k1
k3 k5 k4
k1
k3
k2 k5
T1 T2
![Page 4: 26.08.09IT 60101: Lecture #121 Foundation of Computing Systems Lecture 12 Trees: Part VII.](https://reader036.fdocuments.us/reader036/viewer/2022062503/5a4d1ad07f8b9ab059971391/html5/thumbnails/4.jpg)
26.08.09 IT 60101: Lecture #12 4
Splay Tree
Key Number of Number of Number of accesses comparisons comparisons
in tree T1 in tree T2
k1 1 1×2 1×3k2 1 1×1 1×2k3 10 10×3 10×1k4 2 2×2 2×3k5 3 3×3 3×2
Total no. of comparisons 46 27
k2
k4k1
k3 k5 k4
k1
k3
k2 k5
T1 T2
![Page 5: 26.08.09IT 60101: Lecture #121 Foundation of Computing Systems Lecture 12 Trees: Part VII.](https://reader036.fdocuments.us/reader036/viewer/2022062503/5a4d1ad07f8b9ab059971391/html5/thumbnails/5.jpg)
26.08.09 IT 60101: Lecture #12 5
Splay Tree• Definition
A splay tree is a binary search tree and it adjusts its balance dynamically providing lower access time of higher occurrence nodes.
![Page 6: 26.08.09IT 60101: Lecture #121 Foundation of Computing Systems Lecture 12 Trees: Part VII.](https://reader036.fdocuments.us/reader036/viewer/2022062503/5a4d1ad07f8b9ab059971391/html5/thumbnails/6.jpg)
26.08.09 IT 60101: Lecture #12 6
Splay Tree• Operations
• Searching
• Insertion
• Deletion
• Splaying
![Page 7: 26.08.09IT 60101: Lecture #121 Foundation of Computing Systems Lecture 12 Trees: Part VII.](https://reader036.fdocuments.us/reader036/viewer/2022062503/5a4d1ad07f8b9ab059971391/html5/thumbnails/7.jpg)
26.08.09 IT 60101: Lecture #12 7
Splaying Operation• The basic idea of the splaying operation in a tree is that
after a node is accessed, it is pushed to the root. – Not a simple swap operation (this may violates binary search
tree property)
– Way out• Restructuring is by a series of AVL rotations
![Page 8: 26.08.09IT 60101: Lecture #121 Foundation of Computing Systems Lecture 12 Trees: Part VII.](https://reader036.fdocuments.us/reader036/viewer/2022062503/5a4d1ad07f8b9ab059971391/html5/thumbnails/8.jpg)
26.08.09 IT 60101: Lecture #12 8
Splaying Operation: Bottom Up AVL Rotations
k 5
k 2
k 3
k 4
k 1
A
E
C
D
B
F
T A su b tree
k A tre e n o d e
A cess p a th to k 2
(a ) A sp la y tre e a n d k 2 is b e in g a c c e sse d
k 5
k 2
k 3
k 4
k 1
A
E
C
D
B
F
(b ) T h e s p la y t re e a fte r th e ro ta tio n R 1b e tw e e n k 2 a n d k 1 ( in F ig . (a ) )
R 1
A fte r ro ta tio n R 1
R 2
![Page 9: 26.08.09IT 60101: Lecture #121 Foundation of Computing Systems Lecture 12 Trees: Part VII.](https://reader036.fdocuments.us/reader036/viewer/2022062503/5a4d1ad07f8b9ab059971391/html5/thumbnails/9.jpg)
26.08.09 IT 60101: Lecture #12 9
Splaying Operation: Bottom Up AVL Rotations
A fter ro ta tio n R 2
k 5
k 2
k 3
k 4
k 1
A
E
C
D
B
F
(b ) T h e sp la y t re e a f te r th e ro ta tio n R 1b e tw e e n k 2 a n d k 1 ( in F ig . (a ))
R 2
k 5
k 2
k 3
k 4
k 1
A
E
CB
F
R 3
D
(c ) T h e sp la y tr e e a f te r th e ro ta t io n R 2b e tw e e n k 2 a n d k 3 ( in F ig . (b ) )
![Page 10: 26.08.09IT 60101: Lecture #121 Foundation of Computing Systems Lecture 12 Trees: Part VII.](https://reader036.fdocuments.us/reader036/viewer/2022062503/5a4d1ad07f8b9ab059971391/html5/thumbnails/10.jpg)
26.08.09 IT 60101: Lecture #12 10
Splaying Operation: Bottom Up AVL Rotations
k 5
k 2
k 3
k 4
k 1
A
E
CB
F
R 3
D
(c ) T h e sp la y tre e a fte r th e ro ta t io n R 2b e tw e e n k 2 a n d k 3 ( in F ig . (d ) )
k 5
k 2
k 3
k 4k 1
A E
C
B
F
R 4
D
(d ) T h e sp la y tre e a f te r th e ro ta tio n R 3b e tw e e n k 2 a n d k 4 ( in F ig . (c ) )
A fter ro ta tio n R 3
![Page 11: 26.08.09IT 60101: Lecture #121 Foundation of Computing Systems Lecture 12 Trees: Part VII.](https://reader036.fdocuments.us/reader036/viewer/2022062503/5a4d1ad07f8b9ab059971391/html5/thumbnails/11.jpg)
26.08.09 IT 60101: Lecture #12 11
Splaying Operation: Bottom Up AVL Rotations
k 5
k 2
k 3
k 4k 1
A E
C
B
F
R 4
D
(d ) T h e sp la y tr e e a f te r th e ro ta t io n R 3b e tw e e n k 2 a n d k 4 ( in F ig . ( c ) ) k 5
k 2
k 3
k 4
k 1
A
E
C
B F
D
(e ) T h e sp la y tr e e a f te r th e ro ta tio n R 4 b e tw e e n k 2 a n d k 5 ( in F ig . (d ))
A fter ro ta tio n R 4
![Page 12: 26.08.09IT 60101: Lecture #121 Foundation of Computing Systems Lecture 12 Trees: Part VII.](https://reader036.fdocuments.us/reader036/viewer/2022062503/5a4d1ad07f8b9ab059971391/html5/thumbnails/12.jpg)
26.08.09 IT 60101: Lecture #12 12
Splaying Operation: Bottom Up AVL Rotations
k 5
k 2
k 3
k 4
k 1
A
E
C
D
B
F
T A su b tree
k A tree n o d e
A ce ss p a th to k 2
A sp la y tre e a n d k 2 is b e in g a c c e sse d
R 1
k 5
k 2
k 3
k 4
k 1
A
E
C
B F
D
T h e s p la y tre e a fte r th e ro ta t io n s
![Page 13: 26.08.09IT 60101: Lecture #121 Foundation of Computing Systems Lecture 12 Trees: Part VII.](https://reader036.fdocuments.us/reader036/viewer/2022062503/5a4d1ad07f8b9ab059971391/html5/thumbnails/13.jpg)
26.08.09 IT 60101: Lecture #12 13
Side Effect of Bottom Up AVL Rotations
• Although repeated applications of single AVL-rotation work, it possibly deteriorates the situation for the other nodes on (original) their access paths.
• As evident in the splaying operation just discussed, we see that the repeated applications of single AVL rotations have the effect of pushing k2 all the way to the root and subsequently making easy the future accessing of k2.
• Unfortunately, it has pushed up another node k1 almost as deep as k2.
• Another side effect happens to a node like k3 which was earlier closer to root now goes far away from the root
• A possible way to avoid the side effects is to follow splaying strategy of both double and single AVL-rotations, but selectively.
![Page 14: 26.08.09IT 60101: Lecture #121 Foundation of Computing Systems Lecture 12 Trees: Part VII.](https://reader036.fdocuments.us/reader036/viewer/2022062503/5a4d1ad07f8b9ab059971391/html5/thumbnails/14.jpg)
26.08.09 IT 60101: Lecture #12 14
Splaying Operationsp
x
T1 T 2
T 3
x
pT1
T2 T 3
Single A VL-rotation
(left-to -right)
p
XT1
T2 T 3
x
p T 3
T 2T1
Single A VL-rotation
(right-to -left)
(a) rotationZig
(b) rotationZag
![Page 15: 26.08.09IT 60101: Lecture #121 Foundation of Computing Systems Lecture 12 Trees: Part VII.](https://reader036.fdocuments.us/reader036/viewer/2022062503/5a4d1ad07f8b9ab059971391/html5/thumbnails/15.jpg)
26.08.09 IT 60101: Lecture #12 15
Splaying Operations
g
T4
T 3
T 2T1
x
p1
2
x
p
g
T1
T2
T 3 T4
Double left-to-left
AVL-rotation
(c) rotationZig-Zig
![Page 16: 26.08.09IT 60101: Lecture #121 Foundation of Computing Systems Lecture 12 Trees: Part VII.](https://reader036.fdocuments.us/reader036/viewer/2022062503/5a4d1ad07f8b9ab059971391/html5/thumbnails/16.jpg)
26.08.09 IT 60101: Lecture #12 16
Splaying Operations
Double right-to-righ t
AVL-rotation
g
p
x
T4T3
T 2
T1
1
2
x
p
g
T4
T3
T2T1
(d) rotationZag-Zag
![Page 17: 26.08.09IT 60101: Lecture #121 Foundation of Computing Systems Lecture 12 Trees: Part VII.](https://reader036.fdocuments.us/reader036/viewer/2022062503/5a4d1ad07f8b9ab059971391/html5/thumbnails/17.jpg)
26.08.09 IT 60101: Lecture #12 17
Splaying Operations
g
T4
T3T 2
T1
p
x1
2
x
p g
T1 T2 T 3 T4
Double AVL ro tation
righ t-to-left andle ft-to-right rotation
(e) rotationZig-Zag
![Page 18: 26.08.09IT 60101: Lecture #121 Foundation of Computing Systems Lecture 12 Trees: Part VII.](https://reader036.fdocuments.us/reader036/viewer/2022062503/5a4d1ad07f8b9ab059971391/html5/thumbnails/18.jpg)
26.08.09 IT 60101: Lecture #12 18
Splaying Operations
g
T1 p
T4
T3T2
x
2
1
x
pg
T1 T2 T 3 T4
Double AVL ro tation
le ft-to-right andrigh t-to-left rotation
(f) rotationZag-Zig
![Page 19: 26.08.09IT 60101: Lecture #121 Foundation of Computing Systems Lecture 12 Trees: Part VII.](https://reader036.fdocuments.us/reader036/viewer/2022062503/5a4d1ad07f8b9ab059971391/html5/thumbnails/19.jpg)
26.08.09 IT 60101: Lecture #12 19
Case 1: Zig or Zag
• Case 1: zig or zag
• If the parent of the x is the root of the tree then
• if x is the left child – perform zig
• else (i.e. x is the right child)
– perform zag
p
x
T 1 T 2
T 3
x
pT 1
T 2 T 3
S in g le A V L -ro ta tio n(left-to -righ t)
Z ig ro ta tio n
p
xT 1
T 2 T 3
x
p
T 1 T 2
T 3S in g le A V L -ro ta tio n
(righ t-to -le ft)
Za g ro ta tio n
![Page 20: 26.08.09IT 60101: Lecture #121 Foundation of Computing Systems Lecture 12 Trees: Part VII.](https://reader036.fdocuments.us/reader036/viewer/2022062503/5a4d1ad07f8b9ab059971391/html5/thumbnails/20.jpg)
26.08.09 IT 60101: Lecture #12 20
Case 2: Zig-Zig or Zag-Zag
• Case 2: zig-zig or zag-zag
• If x has both a parent (p) and a grandparent (g)
• If x is the left child of p and p is the left child of g
– perform zig-zig
• else (i.e. x is the right child of p and p is the right child of g)
– perform zag-zag
x
pT 1
T 2
T 4
g
p
T 1
T 3
T 4
D o u b le left- to - le ftA V L -ro tatio n
Z ig -Z ig ro ta t io n
g
T 3
x
T 2
1
2
g
pT 1
T 2
T 4
x
p
T 1
T 3
T 4
D o u b le rig h t-to -rig h tA V L -ro ta tio n
Zag -Za g r o ta t io n
x
T 3
g
T 2
1
2
![Page 21: 26.08.09IT 60101: Lecture #121 Foundation of Computing Systems Lecture 12 Trees: Part VII.](https://reader036.fdocuments.us/reader036/viewer/2022062503/5a4d1ad07f8b9ab059971391/html5/thumbnails/21.jpg)
26.08.09 IT 60101: Lecture #12 21
Case 3: Zig-Zag or Zag-Zig
• Case 3: zig-zag or zag-zig
• If x has both a parent (p) and a grandparent (g)
• If x is the right child of p and p is the left child of g then
– perform zig-zag
• else (i.e. x is the left child of p and p is the right child of g)
– perform zag-zig
g
p
T 1
T 2
T 4
x
p
T 1 T 3 T 4
D o u b le A V L ro ta tio nr ig h t-to -left an d
le ft-to -righ t ro ta tio n
Z ig -Za g ro ta t io n
x
T 3
g
T 2
1
2
g
pT 1
T 2
T 4
x
p
T 1 T 3 T 4
D o u b le A V L ro ta tio nleft-to -r igh t an d
rig h t-to -le ft ro ta tio n
Zag -Z ig ro ta tio n
x
T 3
g
T 2
1
2
![Page 22: 26.08.09IT 60101: Lecture #121 Foundation of Computing Systems Lecture 12 Trees: Part VII.](https://reader036.fdocuments.us/reader036/viewer/2022062503/5a4d1ad07f8b9ab059971391/html5/thumbnails/22.jpg)
26.08.09 IT 60101: Lecture #12 22
Illustration of Splaying Operation
k5
k4
k3
k1
A
B C
k2
D
E
Fzig-zag
zig-zig k5
k4
k2
k3k1
A B C D
E
Fk2
k1
k3
k4
k5A B
C D E F
(a) A splay tree wherek is being accessed2
(b) Splay tree afterthe zig-zag
(c) F inal splay tree after the zig-zig
![Page 23: 26.08.09IT 60101: Lecture #121 Foundation of Computing Systems Lecture 12 Trees: Part VII.](https://reader036.fdocuments.us/reader036/viewer/2022062503/5a4d1ad07f8b9ab059971391/html5/thumbnails/23.jpg)
26.08.09 IT 60101: Lecture #12 23
Illustration of Splaying Operation
32
31
30
3
2
1
(a) A skewed binary search tree with 1, 2, 3, ..., 32 numbers insertedin their descending order. This tree contains only left children
![Page 24: 26.08.09IT 60101: Lecture #121 Foundation of Computing Systems Lecture 12 Trees: Part VII.](https://reader036.fdocuments.us/reader036/viewer/2022062503/5a4d1ad07f8b9ab059971391/html5/thumbnails/24.jpg)
26.08.09 IT 60101: Lecture #12 24
Illustration of Splaying Operation
3 2
3 0
2 6
1
2
3
R e su lt o f sp la y in g th e tre e in a t n o d e 1 .
4
5
2 7
3 1
3 2
3 1
3 0
1
2
3
A sk e w e d b in a ry se a rc h tre e w ith
![Page 25: 26.08.09IT 60101: Lecture #121 Foundation of Computing Systems Lecture 12 Trees: Part VII.](https://reader036.fdocuments.us/reader036/viewer/2022062503/5a4d1ad07f8b9ab059971391/html5/thumbnails/25.jpg)
26.08.09 IT 60101: Lecture #12 25
Illustration of Splaying Operation
2 8
2 4
2 0
1
3
2
R e su lt o f sp la y in g th e tre e a t n o d e 2 .
4
6
2 7
2 6
75
2 5
3 1
3 02 9
3 2
3 2
3 0
2 6
1
2
3
A b in a ry s e ra c h t re e
4
5
2 7
3 1
![Page 26: 26.08.09IT 60101: Lecture #121 Foundation of Computing Systems Lecture 12 Trees: Part VII.](https://reader036.fdocuments.us/reader036/viewer/2022062503/5a4d1ad07f8b9ab059971391/html5/thumbnails/26.jpg)
26.08.09 IT 60101: Lecture #12 26
Illustration of Splaying Operation h
i
g
c
d
ea
b
f
b
e
g
c
f
h
i
a
d
zig-zig
e
a g
c
b
i
h
f
d
zig-zag
e
g
c
f
d
zig i
h
a
b
![Page 27: 26.08.09IT 60101: Lecture #121 Foundation of Computing Systems Lecture 12 Trees: Part VII.](https://reader036.fdocuments.us/reader036/viewer/2022062503/5a4d1ad07f8b9ab059971391/html5/thumbnails/27.jpg)
26.08.09 IT 60101: Lecture #12 27
For detail implementation of splaying operation on binary search trees see the book
Classic Data StructuresChapter 7
PHI, 2nd Edn., 17th Reprint