Presentation is loading. Please wait.

Presentation is loading. Please wait.

B-TREE. Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if we have so much data that it won’t.

Similar presentations


Presentation on theme: "B-TREE. Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if we have so much data that it won’t."— Presentation transcript:

1 B-TREE

2 Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if we have so much data that it won’t fit? We will have to use disk storage but when this happens our time complexity fails The problem is that Big-Oh analysis assumes that all operations take roughly equal time This is not the case when disk access is involved

3 Problem with Big `O’ notation Big ‘O’ assumes that all operations take equal time Suppose all data does not fit in memory Then some part of data may be stored on hard disk. CPU speed is in millions of instructions per second  3GHz machines common now Equals roughly 3000 million instructions per seconds Typical disk speeds about 7,200 RPM  Roughly 120 disk accesses per second So accessing disk is incredibly expensive. So we may be willing to do more computation to organize our data better and make fewer disk accesses.

4 Problem with binary trees There is no guarantee that binary trees will be balanced If stored on disk, we have potentially O(N) disk operations

5 M-ary Trees Allows up to M children for each node  Instead of max. 2 for binary trees A complete M -ary tree of N nodes has a depth of log M N Example of complete 5-ary tree of 31 nodes

6 M -ary search tree Similar to binary search tree, except that  Each node has (M-1) keys to decide which of the M branches to follow. Larger M  smaller tree depth But how to make M -ary tree balanced?

7 B-tree Yet another technique of making a search tree balanced. It is not a binary search tree There are many new ideas. B-tree is used in organising objects in files and it is a part of file structure You will read in detail the application of B- tree in your Database course.

8 B-tree B-tree is developed with many new ideas.

9 Let’s see the BST 51015720231124 How do you start building the binary search tree for this set of objects? First step to decide who should be the root node? Then decide the root of the left subtree and root of right subtree and soon.

10 Top-down vs. bottom up Can we think of generating a search tree that is bottom-up. That is, we first decide the leaf-nodes and gradually make the process evolve the root node and subtrees.

11 Formal Definition Structure of a node  A node contains some keys and some pointers.  Number of pointers in a node is one more than the number of keys.  pointers point to descendants.  The keys are sorted in non-decreasing order.

12 A node key1key2key3key4key5 This is full node. key1key2key3

13 Formal Definition B-tree of order m  Every node has a maximum of m child-nodes  A node, other than the root, contains at least  m/2  -1 keys and no more than m-1 keys.  Every node, except the root and the leaves, has at least  m/2  child nodes.  The root has atleast 2 child nodes  All leaf nodes are on the same level.  A nonleaf node with k descendents contains k-1 keys

14 Let’s build a B-tree 51015720231124

15 Let’s build a B-tree 51015720231124 5

16 Let’s build a B-tree 51015720231124 523

17 Now the node is full 51015720231124 510111523

18 24 comes in 51015720231124 510111523

19 Let’s build a B-tree 51015720231124 15 11 105 24 23

20 Let’s build a B-tree 51015720231124 15 11105 24 23 7

21 Searching a B-tree By comparing the key we decide which internal node to reach. At each internal node, use a linear search to decide the next descendant.

22 Constructing a B-tree Add 25 to the tree 1 12 82 25 6 142817 7 52164868 3 2629535545 1281225 Exceeds Order. Promote middle and split.

23 Constructing a B-tree (contd.) 6, 14, 28 get added to the leaf nodes: 1 12 82 25 6 142817 7 52164868 3 2629535545 12 8 1225 12 8 1225 612 28 14

24 Constructing a B-tree (contd.) Adding 17 to the right leaf node would over-fill it, so we take the middle key, promote it (to the root) and split the leaf 1 12 82 25 6 142817 7 52164868 3 2629535545 12 8 2 25 61 2 28 14 28 17

25 Constructing a B-tree (contd.) 7, 52, 16, 48 get added to the leaf nodes 1 12 82 25 6 142817 7 52164868 3 2629535545 12 8 25 6 1 2 28 14 17 7 52 16 48

26 Constructing a B-tree (contd.) Adding 68 causes us to split the right most leaf, promoting 48 to the root 1 12 82 25 6 142817 7 52164868 3 2629535545 817 7 6 2 1 16 14 12 52 48 28 2568

27 Constructing a B-tree (contd.) Adding 3 causes us to split the left most leaf 1 12 82 25 6 142817 7 52164868 3 2629535545 48 17 8 7 6 2 1 16 14 12 2528 5268 3 7

28 Constructing a B-tree (contd.) 1 12 82 25 6 142817 7 52164868 3 2629535545 Add 26, 29, 53, 55 then go into the leaves 48 17 8 3 1267 52682528 16 14 12 26 29 53 55

29 Constructing a B-tree (contd.) Add 45 increases the trees level 1 12 82 25 6 142817 7 52164868 3 2629535545 48 17 8 3 29 28 26 25 68 55 53 52 16 14 12 67 12 45 Exceeds Order. Promote middle and split.

30 Inserting into a B-Tree Attempt to insert the new key into a leaf If this would result in that leaf becoming too big, split the leaf into two, promoting the median key to the leaf’s parent If this would result 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

31 Delete from a B-tree 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:  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.  If the key is not in a leaf then it is guaranteed (by the nature of a B-tree) that its predecessor or successor will be in a leaf -- in this case can we delete the key and promote the predecessor or successor key to the non- leaf deleted key’s position.

32 Delete from a B-tree (2) If a leaf node containing less than the minimum number of keys  CONCATENATE if one of them has more than the min’ number of keys then we can promote one of its keys to the parent and take the parent key into our lacking leaf  REDISTRIBUTE if neither of them has more than the min’ number of keys then the lacking leaf and one of its neighbours can be combined with their shared parent (the opposite of promoting a key) and the new leaf will have the correct number of keys; if this step leave the parent with too few keys then we repeat the process up to the root itself, if required

33 Simple leaf deletion Simple leaf deletion 122952 27915225669723143 Delete 2: Since there are enough keys in the node, just delete it Assuming a 5-way B-Tree, as before...

34 Simple non-leaf deletion 122952 7915225669723143 Delete 52 Borrow the predecessor or (in this case) successor 56

35 Too few keys in node and its siblings Too few keys in node and its siblings 122956 79152269723143 Delete 72 Too few keys! Join back together

36 Too few keys in node and its siblings Too few keys in node and its siblings 1229 79152269563143

37 Enough siblings Enough siblings 1229 79152269563143 Delete 22 Demote root key and promote leaf key

38 Enough siblings Enough siblings 12 297915 31 695643

39 Analysis of B-Trees The maximum number of items in a B-tree of order m and height h: rootm – 1 level 1m(m – 1) level 2m 2 (m – 1)... level hm h (m – 1) m h+1 – 1So, the total number of items is (1 + m + m 2 + m 3 + … + m h )(m – 1) = [(m h+1 – 1)/ (m – 1)] (m – 1) = m h+1 – 1 When m = 5 and h = 2 this gives 5 3 – 1 = 124

40 Reasons for using B-Trees When searching tables held on disc, the cost of each disc transfer is high but doesn't depend much on the amount of data transferred, especially if consecutive items are transferred  If we use a B-tree of order 101, say, we can transfer each node in one disc read operation  A B-tree of order 101 and height 3 can hold 101 4 – 1 items (approximately 100 million) and any item can be accessed with 3 disc reads (assuming we hold the root in memory) 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)  B-Trees are always balanced (since the leaves are all at the same level), so 2-3 trees make a good type of balanced tree


Download ppt "B-TREE. Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if we have so much data that it won’t."

Similar presentations


Ads by Google