Title: Recursive Data Structures
1Chapter 3.4
- Recursive Data Structures
- Part 3
- Storing Trees on Disk
2Trees on Disk
- To save a tree between uses
- e.g. An AVL dictionary used for spelling
- Saving pointer values is useless
- Tree has to be
- Written in preorder on a sequential file
- Rebuild with a straight building program
- Because central memory isn't large enough
- Beware of virtual memory
- Use multiway BTrees
3Storing a Tree on diskWrite in PreOrder on
sequential file
h
l
d
f
root (left subtree) (right subtree) hdbacfegljikn
mo
4Restoring a Tree from diskUse ordinary binary
tree building
hdbacfegljiknmo
h
5Restoring a Tree from diskUse ordinary binary
tree building
hdbacfegljiknmo
h
d
6Restoring a Tree from diskUse ordinary binary
tree building
hdbacfegljiknmo
h
d
b
7Restoring a Tree from diskUse ordinary binary
tree building
hdbacfegljiknmo
h
d
b
a
8Restoring a Tree from diskUse ordinary binary
tree building
hdbacfegljiknmo
h
d
b
a
c
9Restoring a Tree from diskUse ordinary binary
tree building
hdbacfegljiknmo
h
d
b
f
a
c
10Restoring a Tree from diskUse ordinary binary
tree building
hdbacfegljiknmo
h
d
b
f
a
c
e
11Restoring a Tree from diskUse ordinary binary
tree building
hdbacfegljiknmo
h
d
f
12Restoring a Tree from diskUse ordinary binary
tree building
hdbacfegljiknmo
h
l
d
f
13Restoring a Tree from diskUse ordinary binary
tree building
hdbacfegljiknmo
h
l
d
b
f
j
a
c
e
g
14Restoring a Tree from diskUse ordinary binary
tree building
hdbacfegljiknmo
h
l
d
b
f
j
a
c
e
g
i
15Restoring a Tree from diskUse ordinary binary
tree building
hdbacfegljiknmo
h
l
d
f
16Restoring a Tree from diskUse ordinary binary
tree building
hdbacfegljiknmo
h
l
d
b
f
j
n
e
g
17Restoring a Tree from diskUse ordinary binary
tree building
hdbacfegljiknmo
h
l
d
b
f
j
n
a
c
e
g
i
k
m
18Restoring a Tree from diskUse ordinary binary
tree building
hdbacfegljiknmo
19Restoring a Tree from diskWhy not AVL tree
building ???
hdbacfegljiknmo
h
20Restoring a Tree from diskWhy not AVL tree
building ???
hdbacfegljiknmo
h
d
21Restoring a Tree from diskWhy not AVL tree
building ???
hdbacfegljiknmo
h
d
b
22Restoring a Tree from diskWhy not AVL tree
building ???
hdbacfegljiknmo
d
h
b
23Restoring a Tree from diskWhy not AVL tree
building ???
hdbacfegljiknmo
d
h
b
a
24Restoring a Tree from diskWhy not AVL tree
building ???
hdbacfegljiknmo
d
h
b
a
c
25Restoring a Tree from diskWhy not AVL tree
building ???
hdbacfegljiknmo
d
h
b
a
c
f
26Restoring a Tree from diskWhy not AVL tree
building ???
hdbacfegljiknmo
d
h
b
a
c
f
e
27Restoring a Tree from diskWhy not AVL tree
building ???
hdbacfegljiknmo
d
f
b
a
c
e
h
28Restoring a Tree from diskWhy not AVL tree
building ???
hdbacfegljiknmo
d
f
b
a
c
e
h
g
29Restoring a Tree from diskWhy not AVL tree
building ???
hdbacfegljiknmo
d
f
b
a
c
e
h
g
l
30Restoring a Tree from diskWhy not AVL tree
building ???
hdbacfegljiknmo
d
f
b
a
c
e
h
g
l
j
31Restoring a Tree from diskWhy not AVL tree
building ???
hdbacfegljiknmo
d
h
b
a
c
f
l
e
g
j
32Restoring a Tree from diskWhy not AVL tree
building ???
hdbacfegljiknmo
d
h
b
a
c
f
l
e
g
j
i
33Restoring a Tree from diskWhy not AVL tree
building ???
hdbacfegljiknmo
d
h
b
a
c
f
j
e
g
i
l
34Restoring a Tree from diskWhy not AVL tree
building ???
hdbacfegljiknmo
d
h
b
a
c
f
j
e
g
i
l
k
35Restoring a Tree from diskWhy not AVL tree
building ???
hdbacfegljiknmo
h
j
d
b
f
i
l
a
c
e
g
k
36Restoring a Tree from diskWhy not AVL tree
building ???
hdbacfegljiknmo
h
j
d
b
f
i
l
a
c
e
g
k
n
37Restoring a Tree from diskWhy not AVL tree
building ???
hdbacfegljiknmo
h
j
d
b
f
i
l
a
c
e
g
k
n
m
38Restoring a Tree from diskWhy not AVL tree
building ???
hdbacfegljiknmo
h
l
d
b
f
j
n
a
c
e
g
i
k
m
39Restoring a Tree from diskWhy not AVL tree
building ???
hdbacfegljiknmo
h
l
d
b
f
j
n
a
c
e
g
i
k
m
o
40Storing a Tree on diskWhy not use InOrder or
PostOrder ?
h
l
d
f
InOrder (left subtree) root (right
subtree) abcdefghijklmno
41Restoring a Tree from disk Why not use InOrder
???
abcdefghijklmno
a
b
42Restoring a Tree from disk Why not use InOrder
???
abcdefghijklmno
a
b
c
43Restoring a Tree from disk Why not use InOrder
???
abcdefghijklmno
a
b
c
d
44Trees on Disk
- To save a tree between uses
- e.g. An AVL dictionary used for spelling
- Saving pointer values is useless
- Tree has to be
- Written in preorder on a sequential file
- Rebuild with a straight building program
- Because central memory isn't large enough
- Beware of virtual memory
- Use multiway BTrees
45BTrees
- Objectives
- Build well balanced trees
- take properties of disk into account
- space inexpensive
- access time very long
- data transfers by entire sectors or tracks
- Minimize number of disk accesses
- one node of the tree sector or track
- leave spare space to minimize rebalancing
46BTrees
- Definitions
- A node contains k ordered data items
- n is a system dependant constant such that a
sector or a track of the disk can hold 2n data
items and 2n1 pointers - In the root 0 lt k lt 2n
- In all other nodes n lt k lt 2n
- A node has 0 or k1 successors
- All nodes with 0 successors are at the same level
47Multiway Trees
a
b
c
d
48BTree Growth
XX
49BTrees
Example n 2.
Insertion 25
50BTrees
Insertion 25, 36
51BTrees
Insertion 25, 36, 15
52BTrees
Insertion 25, 36, 15, 31
53BTrees
Insertion 25, 36, 15, 31, 18
54BTrees
Insertion 25, 36, 15, 18, 31, 43
55BTrees
Insertion 25, 36, 15, 18, 31, 43, 51
56BTrees
Insertion 25, 36, 15, 18, 31, 43, 51, 66
57BTrees
Insertion 25, 36, 15, 18, 31, 43, 51, 66, 42
58BTrees
Insertion 25, 36, 15, 18, 31, 43, 51, 66, 42, 75
59BTrees
Insertion 25, 36, 15, 18, 31, 43, 51, 66, 42,
75, 81
60BTrees
Insertion 25, 36, 15, 18, 31, 43, 51, 66, 42,
75, 81
61BTrees
Removal 75
15
18
51
66
75
81
62BTrees
Removed 75
15
18
51
66
81
63BTrees
Removal 15
15
18
64BTrees
Removal 15
18
65BTrees
Removed 15
18
25
66BTrees
Removal 43
31
43
18
25
67BTrees
Removal 43
31
42
18
25
36
68BTrees
Removed 43
42
18
25