Presentation is loading. Please wait.

Presentation is loading. Please wait.

IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture17.

Similar presentations


Presentation on theme: "IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture17."— Presentation transcript:

1 IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture17 th Apr 2007 B-Trees

2 2 Ruli Manurung (Fasilkom UI)IKI10100: Lecture17 th Apr 2007 Outline Motivation B-Tree B+Tree Insertion Deletion

3 3 Ruli Manurung (Fasilkom UI)IKI10100: Lecture17 th Apr 2007 Disks are slow! So far, data fits in RAM If data is too large, must store on Disk Big-Oh analysis changes (“constant time”?) Pentium (‘90s): 500 MIPS IDE HDD (’00s): 7200 RPM = 120 accesses per second 1 disk access = 4.000.000 instructions Number of disk accesses dominates the running time! In reality, the numbers are even worse (multi-*) We are willing to do lots of CPU work to reduce disk access!

4 4 Ruli Manurung (Fasilkom UI)IKI10100: Lecture17 th Apr 2007 Example Analyse the following case: You are asked to implement a database program that stores the data of all phonebook entries in Jakarta, e.g. there are 5.000.000 entries. Each entry contains: name, address, telephone number, etc. Assume each entry fits in a record of 512 bytes. Total file size = 5.000.000 * 512 byte = 2,4GB. Assume: CPU: 25 million instructions per second Disk: 6 accesses per second

5 5 Ruli Manurung (Fasilkom UI)IKI10100: Lecture17 th Apr 2007 Disk blocks When storing data on disks, we use blocks: Secondary storage is divided into blocks of equal size. Common sizes: 512 bytes, 2 KB, 4 KB, 8 KB, etc. A block is the smallest unit transferred between disk & memory. Although a program reads 10 bytes, it must access 1 whole block.

6 6 Ruli Manurung (Fasilkom UI)IKI10100: Lecture17 th Apr 2007 Using Binary Search Trees Unbalanced binary search is a disaster. 5.000.000 disk accesses = 9.6 days! Average BST: 1.38 log N = 30 accesses = 5 seconds Randomly, expect nodes 3x deeper = 15 seconds Red-black tree: worst case = 1.44 log N = 32 accesses = 5.3 seconds How can we do better than this?

7 7 Ruli Manurung (Fasilkom UI)IKI10100: Lecture17 th Apr 2007 Make fatter trees! Complete binary tree = log 2 N nodes Complete M-ary tree = log M N nodes For N=31, best binary has height = 5, 5-ary tree has height = 3

8 8 Ruli Manurung (Fasilkom UI)IKI10100: Lecture17 th Apr 2007 B-Tree B-Tree of order M is an M-ary tree with following properties: Data items stored at leaves Non-leaf nodes store M-1 keys: key i represents smallest key in subtree i+1. Root is either leaf or has between 2 and M children. All non-leaf nodes (except root) have between  M/2  and M children. All leaves are at the same level. 1250 625 1000 1277 1282 <>= 1291 1425 2000

9 9 Ruli Manurung (Fasilkom UI)IKI10100: Lecture17 th Apr 2007 B+Tree B+Tree is a variant of B-Tree: All key values (data) are stored at leaf nodes. Add pointer to connect leaf nodes as a linked- list. Enables sequential access of data without requiring tree traversal. Internal nodes contain keys, and are used as index.

10 10 Ruli Manurung (Fasilkom UI)IKI10100: Lecture17 th Apr 2007 B+Tree 1250 0625 1000 1425 2000 1250 1300 1425 1600 2000 03500625 1000 Leaf Nodes >=<

11 11 Ruli Manurung (Fasilkom UI)IKI10100: Lecture17 th Apr 2007 B+Tree Node Structure P K P K P K P 1122 n -1 n P K P K P K P 1122 n....... A high level node (internal node) A leaf node (Every key value appears in a leaf node) Pointer to subtree for keys>= K & < K Pointer to subtree for keys>= K 1 n- 2 n -1 Pointer to subtree for keys>= K & < K 12 Pointer to subtree for keys< K n -1 Pointer to record (block) with key K Pointer to record (block) with key K Pointer to leaf with smallest key greater than K Pointer to record (block) with key K 12 n -1 n-1n-1

12 12 Ruli Manurung (Fasilkom UI)IKI10100: Lecture17 th Apr 2007 Example of a B+Tree 1250 0625 1000 1425 2000 03500625 1300 1250 13001425 16002000 03500625 1000 1600 1425 20001000 1250 Leaf Nodes Actual Data Records >=<

13 13 Ruli Manurung (Fasilkom UI)IKI10100: Lecture17 th Apr 2007 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 K j. Then follow P i to the child node 3.Otherwise k  K m–1, where there are m pointers in the node. Then follow P m to the child node. 2. 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. 3. Eventually reach a leaf node. If for some i, key K i = k follow pointer P i to the desired record (or bucket). Else no record with search-key value k exists.

14 14 Ruli Manurung (Fasilkom UI)IKI10100: Lecture17 th Apr 2007 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 P n to the next leaf node.

15 15 Ruli Manurung (Fasilkom UI)IKI10100: Lecture17 th Apr 2007 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 search-key), record is added to data file, and if necessary search-key and the corresponding pointer is inserted into the leaf node

16 16 Ruli Manurung (Fasilkom UI)IKI10100: Lecture17 th Apr 2007 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  M/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.

17 17 Ruli Manurung (Fasilkom UI)IKI10100: Lecture17 th Apr 2007 Building a B+Tree B+Tree of order 4 (4-ary tree) Insert 67, 123, 89, 18, 34, 87, 99, 104, 36, 55, 78, 9 data records Root =leaf node 671889123 The split at leaf nodes 6789123 promote but retain a copy < data records root node 186789 123 >= split why promote 89, not 67?

18 18 Ruli Manurung (Fasilkom UI)IKI10100: Lecture17 th Apr 2007 67, 123, 89, 18, 34, 87, 99, 104, 36, 55, 78, 9 89123 < root node 18346789 >= 6787 < root node 1834 6789 >= 89123 split Building a B+Tree

19 19 Ruli Manurung (Fasilkom UI)IKI10100: Lecture17 th Apr 2007 67, 123, 89, 18, 34, 87, 99, 104, 36, 55, 78, 9 6787 < root node 1834 6789 >= 8999123 6787 < root node 1834 6789104 8999104123 split Building a B+Tree

20 20 Ruli Manurung (Fasilkom UI)IKI10100: Lecture17 th Apr 2007 67, 123, 89, 18, 34, 87, 99, 104, 36, 55, 78, 9 6787 < 183436 6789104 8999104123 6787 < 1834 3667 8999104123 3655 104 89 673689104 The split at non-leaf nodes promote & don’t retain a copy double node split split The splitting of nodes proceeds upwards till a node that isn’t full is found. In the worst case: root node may be split increasing the height of the tree by 1. Building a B+Tree

21 21 Ruli Manurung (Fasilkom UI)IKI10100: Lecture17 th Apr 2007 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 (K i–1, P i ), where P i is the pointer to the deleted node, from its parent, recursively using the above procedure.

22 22 Ruli Manurung (Fasilkom UI)IKI10100: Lecture17 th Apr 2007 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  M/2  or more pointers is found.

23 23 Ruli Manurung (Fasilkom UI)IKI10100: Lecture17 th Apr 2007 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


Download ppt "IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture17."

Similar presentations


Ads by Google