Struktur Data & Algoritme (Data Structures & Algorithms) B Trees
Denny (
[email protected]) Suryana Setiawan (
[email protected]) Fakultas Ilmu Komputer Universitas Indonesia Semester Genap - 2004/2005 Version 2.0 - Internal Use Only
Outline
Motivation B-Tree B+Tree
SDA/BTREE/V2.0/2
1
Motivation
Perhatikan kasus berikut ini: Kamu harus membuat program basisdata untuk menyimpan data di yellow pages daerah Jakarta, misalnya ada 500.000 data. Setiap entry terdapat nama, alamat, nomor telepon, dll. Asumsi setiap entry disimpan dalam sebuah record yang besarnya 512 byte. Total file size = 500,000 * 512 byte = 256 MB. • terlalu besar untuk disimpan dalam memory (primary storage) • perlu disimpan di disk (secondary storage)
Jika kita menggunakan disk untuk penyimpanan, kita harus menggunakan struktur blok pada disk untuk menyimpan basis data tsb. Secondary storage dibagi menjadi blok-blok yang ukurannya sama. Umumnya 512 byte, 2 KB, 4 KB, 8 KB. Block adalah satuan unit transfer antar disk dengan memory. Walaupun program hanya membaca 10 byte dari disk, 1 block akan dibaca dari disk dan disimpan ke memory. SDA/BTREE/V2.0/3
Motivation
Misalnya 1 disk block 8.192 byte (8 KB) Maka jumlah blok yang diperlukan = 256 MB / 8 KB per block = 31,250 blocks. Setiap blok menyimpan = 8,192 / 512 = 16 records. A disk access is really expensive due to mechanical limitation. Disk access is approx. 10,000 times slower than main memory. One disk access is worth about 200,000 instructions. The number of disk accesses will dominate the running time. Goal: a multiway search tree that will minimize disk accesses. SDA/BTREE/V2.0/4
2
B-Tree
B-tree adalah (a,b)-tree di mana b = 2a - 1. a dan b biasanya merupakan angka-angka yang cukup besar, misalnya a = 100 dan b = 199. B-tree banyak digunakan untuk external data structure. Setiap node berukuran sesuai dengan ukuran block pada disk, misalnya 1 block = 8 KB. Tujuannya: meminimalkan jumlah block transfer.
SDA/BTREE/V2.0/5
B Trees
The problem with Binary Trees is balance, the tree can easily deteriorate to a linked list. Consequently, the reduced search times are lost, this problem is overcome in B-Trees.
B stands for Balanced, where all the leaves are the same distance from the root. B-Trees guarantee a predictable efficiency.
There are several varieties of B-Trees, most applications use the B+Tree.
SDA/BTREE/V2.0/6
3
B Tree
B Tree of degree m has the following properties:
All non-leaf nodes (except the root which is not bound by a lower limit) have between ⎡m/2⎤ and m non-empty children.
A non-leaf node that has n branches will contain n-1 keys.
All leaves are at the same level, that is the same depth from the root. < 0625
1000
1250 1291 1277
1282
>=
1425
2000 SDA/BTREE/V2.0/7
B+Tree
B+Tree adalah variant dari B-tree: semua key value disimpan dalam leaf. disertakan suatu pointer tambahan untuk menghubungkan setiap leaf node tersebut sebagai suatu linear linked-list. • Struktur ini memungkinkan akses sikuensial data dalam B-tree tanpa harus turun-naik pada struktur hirarkisnya.
node internal digunakan sebagai ‘indeks’. Beberapa key value dapat muncul dua kali di dalam tree.
SDA/BTREE/V2.0/8
4
B+Tree
Leaf Nodes
< 0625
0350
>=
1250
1000
1425
1000
0625
2000
1425 1600
1250 1300
2000
SDA/BTREE/V2.0/9
B+Tree Node Structure A high level node (internal node) K1
P1
Pointer to subtree for keys< K 1
K2
P2
..
Pointer to subtree for keys>= K 1& < K
.....
2
P n-1
K n-1
Pointer to subtree for keys>= K n-2& < K
P n
Pointer to subtree for keys>= Kn-1
n-1
A leaf node (Every key value appears in a leaf node) P1
Pointer to record (block) with key K 1
K1
P2
K2
Pointer to record (block) with key K 2
.......
P n-1
K n-1
Pointer to record (block) with key K n-1
P n
Pointer to leaf with smallest key greater than K n-1 SDA/BTREE/V2.0/10
5
Example of a B+Tree
Leaf Nodes
<
>=
1250
0625 1000
0350
0625
0350
0625
1425 2000
1000
1000
1250 1300
1425 1600
2000
1425
1250 1300
2000
1600
Actual Data Records SDA/BTREE/V2.0/11
Queries on B+Trees
Find all records with a search-key value of k. 1. Start with the root node 1. Examine the node for the smallest search-key value > k. 2. If such a value exists, assume it is Kj. Then follow Pi to the child node 3. Otherwise k ≥ Km–1, where there are m pointers in the node. Then follow Pm to the child node. 2.
3.
If the node reached by following the pointer above is not a leaf node, repeat the above procedure on the node, and follow the corresponding pointer. Eventually reach a leaf node. If for some i, key Ki = k follow pointer Pi to the desired record (or bucket). Else no record with search-key value k exists.
SDA/BTREE/V2.0/12
6
Queries on B+Trees: Range Query
Find all records with a search-key value > k and < l (range query). Find all records with a search-key value of k. while the next search-key value < l, follow the corresponding pointer to the records. • when the current search-key is the last search-key in a node, follow the last pointer Pn to the next leaf node.
SDA/BTREE/V2.0/13
Insertion on B+Trees
Find the leaf node in which the search-key value would appear If the search-key value is already there in the leaf node (non-unique seach-key), record is added to data file, and if necessary search-key and the corresponding pointer is inserted into the leaf node
SDA/BTREE/V2.0/14
7
Insertion on B+Trees If the search-key value is not there, then add the record to the data file: If there is room in the leaf node, insert (key-value, pointer) pair in the leaf node (should be sorted) Otherwise, split the node (along with the new (key-value, pointer) entry) as shown in the next slides. Splitting a node: Take the new (search-key value, pointer) pairs (including the one being inserted) in sorted order. Place the first ⎡n/2⎤ in the original node, and the rest in a new node. When splitting a leaf, promote the middle/median key in the parent of the node being split, but retain the copy in the leaf. When splitting an internal node, promote the middle/median key in the parent of the node being split, but DO NOT retain the copy in the leaf. If the parent is full, split it and propagate the split further up.
SDA/BTREE/V2.0/15
Building a B+Tree 67, 123, 89, 18, 34, 87, 99, 104, 36, 55, 78, 9 root node < 67
89
123
leaf node
18 67
split
89 123
The split at leaf nodes
data records root node
89 < 18
promote but retain a copy
67
>= 89
123
why promote 89, not 67?
data records SDA/BTREE/V2.0/16
8
67, 123, 89, 18, 34, 87, 99, 104, 36, 55, 78, 9 root node
89 < 18
34
>= 89
67
123
split
67
root node
89
<
>=
18
34
67
87
89
123
SDA/BTREE/V2.0/17
67, 123, 89, 18, 34, 87, 99, 104, 36, 55, 78, 9 67
root node
89
<
split
>=
18
34 67
67 89
104
87
89
99
123
root node
< 18
34
89 67
99
104
123
87
SDA/BTREE/V2.0/18
9
67, 123, 89, 18, 34, 87, 99, 104, 36, 55, 78, 9 67
89
104
double node split
split
< 18
34
36
split
89 67
99
104
promote and do not retain a copy
87
The splitting of nodes proceeds upwards till a node that is not full is found. In the worst case the root node may be split increasing the height of the tree by 1.
36 67
89 104
The split at non-leaf nodes
89 36
123
104
67
< 18
34
89 36
55
67
99 87
104
123
SDA/BTREE/V2.0/19
Observations about B+Trees
The B+Tree contains a relatively small number of levels (logarithmic in the number of data), thus searches can be conducted efficiently. In processing a query, a path is traversed in the tree from the root to some leaf node. If there are K search-key values in the file, the path is no longer than ⎡log⎡m/2⎤(K)⎤, where b is index blocking factor • Q: Why “log⎡m/2⎤”, not just “logm” ?
SDA/BTREE/V2.0/20
10
Deletion on B+Trees
Remove (search-key value, pointer) from the leaf node If the node has too few entries due to the removal (minimum requirement: ⎡m/2⎤ children), and the entries in the node and a sibling fit into a single node, then Merge the two nodes into a single node Delete the pair (Ki–1, Pi), where Pi is the pointer to the deleted node, from its parent, recursively using the above procedure.
SDA/BTREE/V2.0/21
Deletion on B+Trees
Otherwise, if the node has too few entries due to the removal, and the entries in the node and a sibling does not fit into a single node, then Redistribute the pointers between the node and a sibling such that both have more than the minimum number of entries. Update the corresponding search-key value in the parent of the node. The node deletions may cascade upwards till a node which has ⎡n/2 ⎤ or more pointers is found.
SDA/BTREE/V2.0/22
11
Summary
B Tree is mostly used as an external data structure for databases. B Tree of degree m has the following properties:
All non-leaf nodes (except the root which is not bound by a lower limit) have between ⎡m/2⎤ and m children.
A non-leaf node that has n branches will contain n-1 keys.
All leaves are at the same level, that is the same depth from the root. B+Tree is a variant from B Tree where all key values are stored in leaves
SDA/BTREE/V2.0/23
Further Reading
applet simulasi B+Tree
http://www.cs.msstate.edu/~cs2314/global/BTreeAnimation/visual ization.html
SDA/BTREE/V2.0/24
12
What’s Next
Priority Queue, Heap
SDA/BTREE/V2.0/25
13