L13 B TreeVariants - Amortizedanalysis
L13 B TreeVariants - Amortizedanalysis
Amortized Analysis
B-Tree Variants.
Accounting method. Splay
Trees
B+ Tree Definition
B+ Tree Example
Data pages
45 51
11 21 30
11 13 15
21 23
30 33
41 43
45 47
51 53
record with
search key 3
You may also have sparse B+-tree, e.g., entries in leaf nodes correspond to pages
45 51
11 21 30
11 13 15
21 23
30 33
41 43
45 47
51 53
record with
search key 3
record with
search key 1
45 51
11 21 30
11 13 15
21 23
30 33
41 43
45 47
51 53
record with
search key 3
45 51
11 21 30
11 13 15
pointers to
records with
search key 1
21 23
30 33
41 43
45 47
51 53
pointers to
records with
search key 3
B+ Tree Insertions
B+ Tree Deletions
10
B+ Tree Performance
11
Applications of B+ Trees
12
B+/B-Trees Comparison
B-trees:
no key repetition,
better for random accesses (do not always reach
a leaf),
data pages on any node
B+-trees:
key repetition,
data page on leaf nodes only,
better for range queries,
easier implementation
DSA - lecture 13 - T.U. Cluj - M. Joldo
13
Amortized Analysis
14
Amortized Analysis
15
Amortized Analysis
AMORTIZED ANALYSIS:
You try to estimate an upper bound of the total
work T(n) required for a sequence of n
operations
Some operations may be cheap some may be
expensive. Overall, your algorithm does T(n) of
work for n operations
Therefore, by simple reasoning, the amortized
cost of each operation is T(n)/n
16
Amortized Analysis
17
Amortized Analysis
Stack example
Binary counter example
We describe Insert/Search/Delete/Join/Split in
Splay Trees. Accounting method can show that
these operations have O(log n) amortized cost (run
time) and they are balanced just like AVL trees
We do not show the analysis behind the O(log n) run time
DSA - lecture 13 - T.U. Cluj - M. Joldo
18
POP(S)
19
20
Accounting method:
operation
Some is deposited to pay for future operations
Stack element credit invariant: 1 deposited on it
Actual cost
Amortized cost
PUSH
1
POP
1
MULTIPOP min(k, S)
PUSH
POP
MULTIPOP
2
0
0
21
22
b
1 a
a 1
Push(a) = 2
1 pays for push
and 1 is deposited
Push(b) = 2
1 pays for push
and 1 is deposited
c 1
b 1
a 1
Push(c) = 2
1 pays for push
and 1 is deposited
23
Accounting Method
A sequence of n POP(),
MULTIPOP(), and
PUSH() operations needs a budget T(n) of at
most 2n
Each operation costs
24
25
i=0;
while i < length(A) and A[i]=1 do
A[i]=0;
i=i+1;
if i < length(A) then
A[i] = 1
This procedure resets the first i-th sequence of 1 bits and sets
A[i] equal to 1 (ex. 0011 0100, 0101 0110, 0111 1000)
DSA - lecture 13 - T.U. Cluj - M. Joldo
26
4-bit counter:
Counter value
0
1
2
3
4
5
6
7
8
COUNTER
0000
0001
0010
0011
0100
0101
0110
0111
1000
A3A2A1A0
27
OBSERVATION:
28
29
0 0 0
0 0
0 1 1 0
1 0
0 1 0 0
30
Splay Trees
SplayInsert()
SplaySearch()
SplayDelete()
Split()
Join()
Splay Trees
32
SplayInsert(x)
- insert x as in BST;
- splay(x);
SplaySearch(x)
- search for x as
in BST;
- if you locate x
splay(x);
SplayDelete(x)
- delete x as in BST;
if successful then
- splay() at successor
or predecessor of x;
33
Splay(x)
- moves node x at
the root of the tree
perform zig, zig-zig, zig-zag
rotations until the element
becomes the root of the tree
DSA - lecture 13 - T.U. Cluj - M. Joldo
34
ZIG (1 rotation):
x
y
x
zig
C
A
y
A
B
35
ZIG-ZIG (2 rotations):
z
y
D
x
C
z
B
zig-zig
A
C
DSA - lecture 13 - T.U. Cluj - M. Joldo
D
36
ZIG-ZAG (2 rotations):
z
x
y
D
zig-zag
y
D
37
Splaying: Example
Splaying at a node splay(a):
e
d
c
b
Aa
ZIG
c
a
E
b
ZIG-ZAG
B C
C D
DSA - lecture 13 - T.U. Cluj - M. Joldo
38
c
b
A B
ZIG-ZAG
a
G
ZIG-ZIG
e
G
A Bd
b
F
E
D
39
Splaying
http://www.link.cs.cmu.edu/splay/
http://www.ibr.cs.tu-bs.de/courses/ss98/audii/applets/BST/
SplayTree-Example.html
DSA - lecture 13 - T.U. Cluj - M. Joldo
40
x
T1
T2
T1
T2
T1
T2
41
x
T
T1
T2
T1
T2
42
SplayInsert()
SplaySearch()
SplayDelete()
43
Reading
44