- Prepare
All Platforms Programming Aptitude Syllabus Interview Preparation Interview Exp. Off Campus - Prime Video
- Prime Mock

- Interview Experience
- Prime VideoNew
- Prime Mock
- Interview Prep
- Nano Degree
- Prime Video
- Prime Mock

# B Tree: Deletion

**Deletion in a B-Tree**

The deletion of nodes in a B-Tree can be broadly classified into two vivid cases:

*deletion at leaf node.**deletion at internal node.*

Let us say the node to be deleted is called the *target key*. The *target key* can either be at the leaf node or an internal node.

Let us now consider the various cases that follow:

**1. If the ***target key* is at the leaf node:

*target key*is at the leaf node:

If the target key is at the leaf node, we further study the given data to check if any of the following cases exist:

**Case 1:**If the leaf node consists of the min number of keys according to the given

*degree/order*, then the key is simply deleted from the node.**Case 2****:**If the leaf contains the minimum number of keys, then:

**Case 2a****:****The node can borrow a key from the immediate left sibling node**,if it has more than the minimum number of keys.The transfer of the keys take place through the parent node, i.e, the maximum key of the left sibling moves upwards and replaces the parent; while the parent key moves down to the target node from where the target key is simply deleted.

**Case 2b****:****The node can borrow a key from the immediate right sibling node**, if it has more than the minimum number of keys.The transfer of the keys take place through the parent node, i.e, the minimum key of the right sibling moves upwards and replaces the parent; while the parent key moves down to the target node from where the target key is simply deleted.

**Case 2c:**If

**neither of the siblings have keys more than the minimum number of keys required**then, merge the target node with either the left or the right sibling along with the parent key of respective node.

**2.If the ***target key* is at the internal node:

*target key*is at the internal node:

If the target key is at an internal node, we further study the given data to check if any of the following cases exist:

**Case 1:****replaced by its inorder predecessor**,i.e, the largest element of the left child node.

**Case 2:**If the right child has more than the minimum number of keys, the target key in the internal node is

**replaced by it’s inorder successor**,i.e, the smallest element of the right child node.

### Example:

Let us consider the given tree.

From the given tree we are to delete the following elements:

**A = 20 , 53 , 89 , 90 , 85.**

Assuming we have **order = 5;**

**minimum keys =****⌈****m/2****⌉ – 1 = 2****maximum keys =****⌈****m/2****⌉ + 1 = 4****minimum children =****⌈****m/2****⌉ = 3****maximum children =****m = 5**

**Step 1:**

- The first element to be deleted from the tree structure is
**20.** - We can see the key lies in the leaf node.

**Step 2:**

- The key
**20**exists in a leaf node. - The node has more than the minimum number of keys required.
- Thus the key is simply deleted from the node.
- The tree after deletion is shown as follows.

**Step 3:**

- The next element to be deleted from the tree is
**53.** - We can see from the image that the key exists in the leaf node.

**Step 4:**

- Since the node in which the target key
**53**exists has just the minimum number of keys, we cannot delete it directly. - We check if the target node can borrow a key from it’s sibling nodes.
- Since the target node doesn’t have any right sibling, it
**borrows from the left sibling node.** - As we have studied above how the process of borrow and replace takes place, we apply it to the given structure.

**Step 5:**

- The key
**49**moves upwards to the parent node. - The key
**50**moves down to the target node.

#### Step 6:

- Now, since the target node has keys more than the minimum number of keys required, the key can be deleted directly.
- The tree structure after deletion is shown as follows.

**Step 7:**

- The next element to be deleted is
**89.** - The target key lies within a leaf node as seen from the image.

**Step 8:**

- Again, the target node holds just the minimum number of keys required and hence the node cannot be deleted directly.
- The target node now has to borrow a key from either of it’s siblings.
- We check the left sibling; it also holds just the minimum number of keys required.
- We check the right sibling node; it has one more than the minimum number of nodes so the target node can borrow a key from it.

#### Step 9:

- The key
**93**moves up to the parent node. - The parent key
**90**moves down to the target node.

**Step 10:**

- Now, as the target node has sufficient number of keys the target key can directly be deleted from the target node.
- The tree structure after deletion is shown as follows.

**Step 11:**

- The next key to be deleted is
**90**. - The key exists within a leaf node as shown in the image.

**Step 12:**

- We can see that the target node has just the minimum number of keys.
- The target node has to borrow a key from either of it’s siblings.
- Since each of the siblings just have the number of the minimum keys, it cannot borrow the keys directly.

#### Step 13:

- Since the target node cannot borrow from either of the siblings, we merge the target node, either of the sibling node and the corresponding parent to them.
- The process of merging is shown as follows.

**Step 14:**

- Since the target node now has sufficient number of keys, the target key
**90**can be deleted directly. - The tree structure after the deletion of the element is shown as follows.

**Step 15:**

- The next target node is
**85**. - Now here the node to be deleted is not a leaf node but an
*internal node.*

#### Step 16:

- In case, when an internal node is to be deleted, we replace the key with it’s inorder predecessor or inorder successor.
- We can select either of the child nodes if they have sufficient number of keys.
- But as we can see in this case the target internal node can only borrow from it’s right child, i.e,
*inorder predecessor.* - The key
**85**moves down to the child node; key**87**moves up to the parent node.

**Step 17:**

- Now, as the target key is moved to the leaf node, it can be simply deleted from the leaf node.
- The final tree structure after deletion of various nodes and preserving the
*b-tree properties*is shown as follows.

Login/Signup to comment