M-Way Trees: Multiway Trees: M-Way Search Trees, B-Trees, Operations On B-Trees, B+-Trees
M-Way Trees: Multiway Trees: M-Way Search Trees, B-Trees, Operations On B-Trees, B+-Trees
M-way Trees
Before learning about B-Trees we need to know what M-way trees
are, and how B-tree is a special type of M-way tree. An M-way(multi-
way) tree is a tree that has the following properties:
The keys in the first K children are less than the Kth key of this
node.
The keys in the last (m-K) children are higher than the Kth key.
For example, consider the 3-way search tree that is shown above,
say, we want to search for a node having key(X) equal to 60. Then,
considering the above cases, for the root node, the second condition
applies, and (60 > 40) and hence we move on level down to the
right subtree of 40. Now, the last condition is valid only, hence we
traverse the subtree which is in between the 55 and 70. And finally,
while traversing down, we have our value that we were looking for.
If the root node is a non leaf node, then it must have at least
two children.
All nodes except the root node, must have at least [M/2]-
1 keys and at most M-1 keys.
Searching in a B Tree:
Inserting in a B Tree:
If that node contains less than M-1 keys, then we insert the
key in an increasing order.
If that node contains exactly M-1 keys, then we have two
cases ? Insert the new element in increasing order, split the
nodes into two nodes through the median, push the median
element up to its parent node, and finally if the parent node
also contains M-1 keys, then we need to repeat these steps.
Now, consider that we want to insert a key 9 into the above shown B
tree, the tree after inserting the key 9 will look something like this:
Since, a violation occurred, we need to push the median node to the
parent node, and then split the node in two parts, hence the final
look of B tree is:
Deletion in a B Tree:
o Last case would be that neither the left sibling or the right
sibling are in a state to give the current node any value,
so in this step we will do a merge with either one of them,
and the merge will also include a key from the parent,
and then we can delete that key from the node.
After we delete 23, we ask the left sibling, and then move 16 to the
parent node and then push 20 downwards, and the resultant B tree
is:
Case 2 pictorial representation:
After we delete 72, we ask the right sibling, and then move the 77 to
the parent node and then push the 75 downwards, and the resultant
B tree is:
Why B+ trees?
As the leaf nodes are connected with each other like a linked
list, we can easily search elements in sequential manners.
Inserting in B+ tree
If the bucket is not full( does not violate the B+ tree property ),
then add that node into this bucket.
Otherwise split the nodes into two nodes and push the middle
node( median node to be precise ) to the parent node and then
insert the new node.
Repeat the above steps if the parent node is there and the
current node keeps getting full.
Searching in B+ tree:
Now we know that 59 < 69, hence we traverse the left subtree.
Now we have found the internal pointer that will point us to our
required search value.
Finally, we traverse this bucket in a linear fashion to get to our
required search value.
Deletion in B+ tree:
After the deletion of 79, we are left with the following B+ tree.
Deletion if a pointer to a leaf node is there:
After we locate the node we want to delete we must also delete the
internal pointer that points to this node, and then we finally need to
move the next node pointer to move to the parent node.