B Tree
B Tree
B-Tree: Insert X
• As in M-way tree find the leaf node to which X should be added
• Add X to this node in the appropriate place among the values already there (there are no
subtrees to worry about)
• Number of values in the node after adding the key:
o Fewer than 2t-1: done
o Equal to 2t: overflowed
• Fix overflowed node
Fix an Overflowed
• Split the node into three parts, M=2t:
o Left: the first t values, become a left child node
o Middle: the middle value at position t, goes up to parent
o Right: the last t-1 values, become a right child node
• Continue with the parent:
o Until no overflow occurs in the parent
o If the root overflows, split it too, and create a new root node
Insert Example
Complexity Insert
• Inserting a key into a B-tree of height h is done in a single pass down the tree and a single pass
up the tree.
• Complexity: O(h)= O(logt n)
B-Tree: Delete X
• Delete as in M-way tree
• A problem: – might cause underflow: the number of keys remain in a node < t-1
• Deletion from:
o A key is in leaf node
o A key is in internal node
• If Order (m) = 5
o Min children = ceil(m/2) = 3
o Max children = m = 5
o Min keys = ceil(m/2) – 1
o Max keys = m-1 = 4
• Leaf node
o Leaf node contains more than min. no. of keys
o Leaf node contains min. no. of keys