DSA - B - Tree
DSA - B - Tree
B tree
B tree data structure
B trees are extended binary search trees that are specialized in m-way
searching, since the order of B trees is 'm'. Order of a tree is defined as
the maximum number of children a node can accommodate.
Therefore, the height of a b tree is relatively smaller than the height of
AVL tree
• The various properties of B trees include −
• Every node in a B Tree will hold a maximum of m children and (m-1) keys, since the
order of the tree is m.
• Every node in a B tree, except root and leaf, can hold at least m/2 children
• The root node must have no less than two children.
• All the paths in a B tree must end at the same level, i.e. the leaf nodes must be at the
same level.
• A B tree always maintains sorted data.
Insertion operation
• The insertion operation for a B Tree is done similar to the Binary Search Tree but the
elements are inserted into the same node until the maximum keys are reached. The
insertion is done using the following procedure −
• Step 1 − Calculate the maximum (m−1) and, minimum ( ⌈m2 ⌉−1) number of keys a
node can hold, where m is denoted by the order of the B Tree.
• Step 2 − The data is inserted into the tree using the binary search insertion and once the
keys reach the maximum number, the node is split into half and the median key
becomes the internal node while the left and right keys become its children.
• Step 3 − All the leaf nodes must be on the same level.
• The keys, 5, 3, 21, 9, 13 are all added into the node according to the binary search
property but if we add the key 22, it will violate the maximum key property. Hence, the
node is split in half, the median key is shifted to the parent node and the insertion is
then continued.
• Another hiccup occurs during the insertion of 11, so the node is split and median is
shifted to the parent.
• While inserting 16, even if the node is split in two parts, the parent node also overflows
as it reached the maximum keys. Hence, the parent node is split first and the median
key becomes the root. Then, the leaf node is split in half the median of leaf node is
shifted to its parent.
Deletion operation
• Case 1 − If the key to be deleted is in a leaf node and the deletion does not violate the
minimum key property, just delete the node.
• Case 2 − If the key to be deleted is in a leaf node but the deletion violates the minimum
key property, borrow a key from either its left sibling or right sibling. In case if both
siblings have exact minimum number of keys, merge the node in either of them.
• Case 3 − If the key to be deleted is in an internal node, it is replaced by a key in either
left child or right child based on which child has more keys. But if both child nodes
have minimum number of keys, they’re merged together.
• Case 4 − If the key to be deleted is in an internal node violating the minimum keys
property, and both its children and sibling have minimum number of keys, merge the
children. Then merge its sibling with its parent.
Time Complexity
2. Insert O(log n)
3. Delete O(log n)