B-tree Why B-Trees • When the data is too big, we will have to use disk storage instead of putting all the data in main memory • In such case, we have to take into account the fact that disk access takes much longer time than many other instructions Disk Access Time • Access time = seek time + rotational delay (latency) + transfer time – Seek time is slow, it depends on the mechanical movement of the disk head to position the head at the correct track of the disk – Latency is the time required to position the head above the correct block and on average, it is one-half of a revolution. • Ex. to transfer 5kb from a disk requires 40ms to locate a track, making 7200 RPM and with a data transfer rate of 1000kb per second, access time = 40ms + 4ms + 5ms = 49 ms Why B-Trees • Assume that we use a Balanced Binary tree to store all 20 million recordslog2 20,000,000 is about 25, end up with a very deep tree • it will take more than 1 second to transfer a record • cannot improve on the log n for a binary tree • The solution is to use more branches and thus less height, as branching increases, depth decreases Definition of a B-tree • A B-tree of order m is an m-way tree (i.e., a tree where each node may have up to m children) in which: 1.the root has at least two subtree unless it is a leaf 2. each non-leaf and nonroot nodes have k-1 keys and k pointers where m / 2 <= k <= m 3. Each leaf node holds k-1 keys where m / 2 <= k <= m 4. All leaves are on the same level. An example B-Tree A B-tree of order 5 3 1 2 6 7 8 9 12 14 16 19 B-Trees Example: 2-3 Tree If we take m = 3, we get a 2-3 tree, in which non-leaf nodes have two or three children (i.e., one or two keys), and it is balanced as B-Trees are always balanced (since the leaves are all at the same level), Search B-Tree B-Tree: Insertion • insert the new key into a leaf • If the resulting leaf becoming too big, split the leaf into two, promoting the middle key to the leaf’s parent • If the promotion results in the parent becoming too big, split the parent into two, promoting the middle key • This strategy might have to be repeated all the way to the top • If necessary, the root is split in two and the middle key is promoted to a new root, making the tree one level higher B-tree insertion example (order 3) From Wiki B-tree: Deletion During insertion, the key always goes into a leaf. For deletion we wish to remove from a leaf. There are three possible ways we can do this: 1. If the key is already in a leaf node, and removing it doesn’t cause that leaf node to have too few keys, then simply remove the key to be deleted. 2. If the key is in a non-leaf node, then delete the key and promote the predecessor or successor key to the non-leaf deleted key’s position B-tree: Deletion If 1 or 2 cause a leaf node containing less than the minimum number of keys then we either get help from sibling or merge nodes. 3. Check if one of the siblings immediately adjacent to the leaf in question has more than the minimum number of keys, if yes, then promote one of its keys to the parent and take the parent key into the lacking leaf 4. if neither of them has more than the minimum number of keys then merge the lacking leaf and one of its neighbours with their shared parent (the opposite of promoting a key) if the merge step causes the parent with too few keys, then we repeat the process up to the root, if required Analysis of B-Trees • The maximum number of items in a B-tree of order m and height h: root level 1 level 2 . . . level h m–1 m(m – 1) m2(m – 1) mh(m – 1) • the total number of items is (1 + m + m2 + m3 + … + mh)(m – 1) = [(mh+1 – 1)/ (m – 1)] (m – 1) = mh+1 – 1 • When m = 5 and h = 2 this gives 53 – 1 = 124 • Demo Revisit: B-Trees Motivation • When searching tables held on disc, the cost of each disc transfer is high – If we use a B-tree of order 101, and assume that we can transfer each node in one disc read operation – A B-tree of order 101 and height 3 can hold 1014 – 1 items (approximately 100 million) and any item can be accessed with 3 disc reads (assuming we hold the root in memory)