Presentation is loading. Please wait.

Presentation is loading. Please wait.

B-Trees .

Similar presentations


Presentation on theme: "B-Trees ."— Presentation transcript:

1 B-Trees

2 Why limit yourself? BSTs, AVLs and other basic trees limit themselves to only two children per node. Why? Because it’s easy So let’s lift that restriction. Now we can make our nodes store as many children as we want. Even so many that one node will fill a disk block.

3 B-Trees B-Trees that are tuned will do just this.
Each node will be a disk block and as such can be read off disk as a discrete unit B-Trees have several properties that must be held.

4 B-Tree of order m Properties
The root has at least two subtrees unless it is a leaf Each nonroot and each nonleaf holds k-1 keys and k pointers to subtrees where ceiling(m/2) ≤ k ≤ m Each leaf node holds k-1 keys where ceiling(m/2) ≤ k ≤ m All leaves are on the same level

5 More B-Trees m is usually large (50-500)
The nodes usually hold an array for the pointers and an array for the keys

6 Insertion B-Trees are built from the ground up.
As keys are added, if need be the root grows up and levels are added This is because all the leaves are at the same level

7 Insertion Cases Case 1: The key is inserted in a node that has space for it. Case 2: The key is inserted in a node that is full A new node is made and half the keys from the old node to the new node. The middle key is passed up so that the new leaf can be incorporated in the tree This can continue for a while

8 Insertion Cases Case 3: This is a special case of the last case.
The splitting continues up to the root, which causes the root to split. This will create a new root and the tree has grown.

9 Deletion Deletion is always trickier.
Deletion must leave the tree in a state that is consistent with the B-Tree definition So leaves must be at least half full. Deletion is more or less the opposite of insertion with some more special cases.

10 Deletion Cases Case 1: Deletion leaves the leaf with more than m/2 keys. The keys are shifted to fill the hole. Case 2: Deletion leaves the leaf with less than m/2 keys. This is known as underflow. If there is a sibling node that is more than half full, then the nodes are merged, the sibling is discarded and the separating key from the parent are all put in the leaf. The parent must also be shifted. This could cause the parent to underflow

11 Deletion Case Case 2: Special case 2
When the parent node is the root with only one key, then the key is pulled down in the leaf and the root and the sibling are discarded. This is the only time two nodes are discarded. Case 3: Deleting a key from a non-leaf. Key is swapped with its immediate predecessor and the deletion goes from case 1.

12 B-Tree Forest The are many variations on the basic B-tree
The B*-Tree tightens the ½ full to 2/3 full. B+-Trees push all the data to the leaves and then link them all across the bottom.


Download ppt "B-Trees ."

Similar presentations


Ads by Google