Unit-4 Tree Notes
Unit-4 Tree Notes
General Tree
Tree Traversal
1) Pre-Order Traversal.
2) In Order Traversal
3) Post Order Traversal
child with min value of the given search interval. If greater, then repeat these steps for left child
otherwise, for right child.
Complexity O(logn)
10.18 B-Tree
A B-Tree is a self-balancing tree that maintains sorted data and allows sequential access,
insertion, deletion in logarithmic time.B-Tree is commonly used in database and file systems.
10.18.3.1 Insertion
All insertions take place in the leaf nodes. The search is followed starting from root node that
directs us to reach the target leaf node (same as in BST). If the target leaf node contains the
maximum keys, the key is virtually inserted in that node, and the node splits in two parts while
sending the median key up to the parent node. In this process, if the parent node also contains
the maximum key, it also splits in the same manner and the median key is sent up. The process
continues until we reach the parent that contains the keys less than maximum or to the root
node. If the root also contains the maximum keys, it also splits into two parts and the median
key is sent up. A new root node is created with this median key.
Insert 2
Insert 3
(Splitting after virtually inserting 3)
Insert 4
Insert 5
(Splitting after virtually inserting 5)
Insert 6,7
(Splitting after virtually inserting 7)
Insert 8
Insert 9
Insert 10
Step 2
Insert 2: - The second key 2 is greater than 1 hence inserted next to 2.
Step 3
Insert 3: - The third key 3 is inserted to right of 2 as 3>2.
Step 4
Insert 4: Now the node has maximum keys so it splits from the middle and grows upwards. Here
2 will be the root node. Element smaller than 2 will be on LHS and greater than 2 will be on
RHS. Note that we have already discussed that root can have 1 key (minimum).
Step 5
Insert 5: - Now 5 will be inserted at leaf node, we search the correct position of 5 from root
node, here 5 is greater than 2 hence will be inserted as a right child of 2. Here 5 is inserted to
the right to key 4 as 5 > 4.
Step 6
Insert 6: - Now the node has maximum keys, so it splits from middle and grows upwards. Here 4
will shift in the root node. Keys less than 4 will be on LHS and greater than 4 will be on RHS.
Key 6 will be inserted as the Right child of node 4 and right to node 5 in leaf node.
Step 7
Insert 7: - Key 7 will be inserted as the Right child of node 4 and right to node 6 in leaf node.
Step 8
Insert 8: - Now the node is having maximum keys, so it splits from the middle and grows
upwards. Here 6 will shift towards the root node. Keys less than 6 will be on LHS and greater
than 6 will be on RHS.
Key 8 will be inserted as the Right child of node 6 and right to key 7 in leaf node.
Step 9
Insert 9: - Key 9 will be inserted as the Right child of node containing 6 and right to key 8 in the
leaf node.
Step 10
Insert 10: - Now the target leaf node is having maximum keys so it splits from the middle and
grows upwards. Here 8 will shift towards the root node. Now root node also contains maximum
keys; hence another splitting will occur and 4 will move upwards as a new root node of the
tree.
Element less than 4 will be on LHS and greater than 4 will be on RHS.
Node 10 will be inserted as the Right child of node containing key 8 and right to key 9 in leaf
node.
Figure: Delete 23
Since the leaf node contains the minimum number of keys, the solution to such case regards
the help of the following: -
a. Immediate Left Sibling node of the node containing key to be deleted,
b. Immediate Right Sibling node of the node containing key to be deleted.
c. The parent node of the node containing key to be deleted.
For case a & b, if the immediate sibling contains more than minimum keys, we can transfer keys
from sibling to sibling via parent node.
In case c, if both the immediate left and right sibling nodes contain minimum required keys
then we can merge these nodes with the parent node.
Here in the above case, the immediate right sibling consists of keys that are greater than
minimum required, so we can transfer the key via root node.
Figure: Delete 13
Take another case; if we want to delete the key 16 from the above formed tree, its immediate
right sibling contains the minimum number of keys. In this case, we merge the node which
contains key 16 with its right sibling along with bringing the separator key from the parent to
this node. After deletion, the tree will be like,
10.19 B+ Tree
B+ Tree is an m-way search Tree in which internal nodes behave like the indexes and leaf nodes
as data nodes.
Let us take an example where we insert the following keys in B+Tree of order 3:-
5, 10, 12, 14, 13, 1, 2, 3, 4.
Minimum children will be 2 Minimum keys will be 1
Maximum children will be 3 Max keys will be 2
Step 1
Insert 5
Since key 5 is the first value to be inserted, it can easily be placed into a node.
Step 2
Insert 10
Now key 10 is being inserted into root node and since maximum keys that can be inserted is 2,
it is inserted in increasing order into root node.