Download presentation

Presentation is loading. Please wait.

Published byElvin Stiller Modified over 2 years ago

1
B-Trees © Dave Bockus Acknowledgements to: Dr Frederic Maire Brisbane, Queensland, AUSTRALIA for some of the material found in this presentation

2
2 Motivation When data is too large to fit in main memory, then the number of disk accesses becomes important. A disk access is unbelievably expensive compared to a typical computer instruction (mechanical limitations). One disk access is worth about 200,000 instructions. The number of disk accesses will dominate the running time.

3
3 Motivation Cont.. Secondary memory (disk) is divided into equal- sized blocks (typical sizes are 512, 2048, 4096 or 8192 bytes) The basic I/O operation transfers the contents of one disk block to/from main memory. Our goal is to devise a multiway search tree that will minimize file accesses (by exploiting disk block read).

4
4 m-ary Trees A node contains multiple keys. Order of subtrees is based on parent node keys If each node has m children & there are n keys then the average time taken to search the tree is log m n. Etc. K1 K2 K3 K4 T1T2T3 K < K1 K1 < K < K2

5
5 Searching m-ary Trees A generalized SOT will visit all keys in ascending order. for (i==1;i<=m-1;i++) { visit subtree to left of k i visit k i } visit subtree to right of k m-1

6
6 B-Trees & Efficiency Used in Mac, NTFS, OS2 for file structure. Allow insertion and deletion into a tree structure, based on log m n property, where m is the order of the tree. The idea is that you leave some key spaces open. So an insert of a new key is done using available space (most cases). –Less dynamic then our typical Binary Tree –Ideal for disk based operations.

7
7 Definition of a B-Tree Def: B-tree of order m is a tree with the following properties: –The root has at least 2 children, unless it is a leaf. –No node in the tree has more then m children. –Every node except for the root and the leaves have at least m/2 children. –All leaves appear at the same level. –An internal node with k children contains exactly k-1 keys.

8
8 2-3 Trees G I | M J | K C D | EA HN | O

9
9 Insertion Insert k i into B-tree of order m. -We find the insertion point (in a leaf) by doing a search. -If there is room then enter k i. -Else, promote the middle key to the parent & split the node into nodes around the middle key. If the splitting backs up to the root, then –Make a new root containing the middle key. Note: the tree grows from the leaves, balance is always maintained.

10
10 I | K | M JHN | OL G | K M L C D | EA HN | O I J G I | M J | K C D | EA HN | O K is promoted again, this gives the new tree: L is inserted into the above tree. Insertion Example

11
11 Splitting Nodes T3 T2 A | B | C T1 T4 CA B T1 T2 T3 Middle key is promoted Creating a new root

12
12 Deletion If the entry to be deleted is not in a leaf, swap it with its successor (or predecessor) under the natural order of the keys. Then delete the entry from the leaf. If leaf contains more than the minimum number of entries, then one can be deleted with no further action.

13
13 Deletion Example 1 C D | E A C EA Delete D C D | E A Delete C D EA Successor is promoted, Element D C is Deleted.

14
14 Deletion Cont... If the node contains the minimum number of entries, consider the two immediate siblings of the parent node: If one of these siblings has more than the minimum number of entries, then redistribute one entry from this sibling to the parent node, and one entry from the parent to the deficient node. –This is a rotation which balances the nodes –Note: all nodes must comply with minimum entry restriction.

15
15 Deletion Example 2 C D | E A C C Delete A C | D E D E

16
16 Deletion Cont... If both immediate siblings have exactly the minimum number of entries, then merge the deficient node with one of the immediate sibling node and one entry from the parent node. If this leaves the parent node with too few entries, then the process is propagated upward.

17
17 Deletion Example 3 G | K M L C D | EA H N | O I J G | K M L C D | EA N | O I J Delete H G | K M L C D | EA N | O I | J Combine with parent and 1 sibling of parent Node is deficient

18
18 Deletion Example 3 Cont.. G | K M L C D | EA N | O I | J Node is now deficient G L C D | EA N | O I | J K | M Deficient node is combined with 1 key from parent and sibling of parent Node G is legal so propagation up the tree stops.

19
19 Review of Deletions All Deletions take place in leaf nodes –To delete a internal key swap it with its successor or predecessor which is a leaf. –Then Delete Deficient Nodes are legalized by: –Rotation with a sibling and parent. OR –Combining with key from parent and sibling –Propagating up the tree until a legal node is encountered.

20
20 End Notes Studies have shown that on average there is about 1/( (m/2) -1) splits per insertion. –E.g. For a 2/3 tree there is 1 For a 10-ary tree there is 1/4

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google