# Deletion from a B-tree

#### In this tutorial, you will learn how to delete a key from a b-tree. Also, you will find working examples of deleting keys from a B-tree in C.

Deleting an element on a B-tree consists of three main events: searching the node where the key to be deleted exists, deleting the key and balancing the tree if required.

While deleting a tree, a condition called underflow may occur. Underflow occurs when a node contains less than the minimum number of keys it should hold.

The terms to be understood before studying deletion operation are:

1. Inorder Predecessor
The largest key on the left child of a node is called its inorder predecessor.
2. Inorder Successor
The smallest key on the right child of a node is called its inorder successor.

## Deletion Operation

Before going through the steps below, one must know these facts about a B tree of degree m.

1. A node can have a maximum of m children. (i.e. 3)
2. A node can contain a maximum of `m - 1` keys. (i.e. 2)
3. A node should have a minimum of `⌈m/2⌉` children. (i.e. 2)
4. A node (except root node) should contain a minimum of `⌈m/2⌉ - 1` keys. (i.e. 1)

There are three main cases for deletion operation in a B tree.

### Case I

The key to be deleted lies in the leaf. There are two cases for it.

1. The deletion of the key does not violate the property of the minimum number of keys a node should hold.In the tree below, deleting 32 does not violate the above properties.
2. The deletion of the key violates the property of the minimum number of keys a node should hold. In this case, we borrow a key from its immediate neighboring sibling node in the order of left to right.First, visit the immediate left sibling. If the left sibling node has more than a minimum number of keys, then borrow a key from this node.

Else, check to borrow from the immediate right sibling node.

In the tree below, deleting 31 results in the above condition. Let us borrow a key from the left sibling node.

If both the immediate sibling nodes already have a minimum number of keys, then merge the node with either the left sibling node or the right sibling node. This merging is done through the parent node.

Deleting 30 results in the above case.

### Case II

If the key to be deleted lies in the internal node, the following cases occur.

1. The internal node, which is deleted, is replaced by an inorder predecessor if the left child has more than the minimum number of keys.
2. The internal node, which is deleted, is replaced by an inorder successor if the right child has more than the minimum number of keys.
3. If either child has exactly a minimum number of keys then, merge the left and the right children.

After merging if the parent node has less than the minimum number of keys then, look for the siblings as in Case I.

### Case III

In this case, the height of the tree shrinks. If the target key lies in an internal node, and the deletion of the key leads to a fewer number of keys in the node (i.e. less than the minimum required), then look for the inorder predecessor and the inorder successor. If both the children contain a minimum number of keys then, borrowing cannot take place. This leads to Case II(3) i.e. merging the children.

Again, look for the sibling to borrow a key. But, if the sibling also has only a minimum number of keys then, merge the node with the sibling along with the parent. Arrange the children accordingly (increasing order).

## C Examples

``````// Deleting a key from a B-tree in C

#include <stdio.h>
#include <stdlib.h>

#define MAX 3
#define MIN 2

struct BTreeNode {
int item[MAX + 1], count;
};

struct BTreeNode *root;

// Node creation
struct BTreeNode *createNode(int item, struct BTreeNode *child){
struct BTreeNode *newNode;
newNode = (struct BTreeNode *)malloc(sizeof(struct BTreeNode));
newNode->item[1] = item;
newNode->count = 1;
return newNode;
}

// Add value to the node
void addValToNode(int item, int pos, struct BTreeNode *node,
struct BTreeNode *child){
int j = node->count;
while (j > pos) {
node->item[j + 1] = node->item[j];
j--;
}
node->item[j + 1] = item;
node->count++;
}

// Split the node
void splitNode(int item, int *pval, int pos, struct BTreeNode *node,
struct BTreeNode *child, struct BTreeNode **newNode){
int median, j;

if (pos > MIN)
median = MIN + 1;
else
median = MIN;

*newNode = (struct BTreeNode *)malloc(sizeof(struct BTreeNode));
j = median + 1;
while (j <= MAX) {
(*newNode)->item[j - median] = node->item[j];
j++;
}
node->count = median;
(*newNode)->count = MAX - median;

if (pos <= MIN) {
} else {
addValToNode(item, pos - median, *newNode, child);
}
*pval = node->item[node->count];
node->count--;
}

// Set the value in the node
int setValueInNode(int item, int *pval,
struct BTreeNode *node, struct BTreeNode **child){
int pos;
if (!node) {
*pval = item;
*child = NULL;
return 1;
}

if (item < node->item[1]) {
pos = 0;
} else {
for (pos = node->count;
(item < node->item[pos] && pos > 1); pos--)
;
if (item == node->item[pos]) {
printf("Duplicates not allowedn");
return 0;
}
}
if (setValueInNode(item, pval, node->linker[pos], child)) {
if (node->count < MAX) {
} else {
splitNode(*pval, pval, pos, node, *child, child);
return 1;
}
}
return 0;
}

// Insertion operation
void insertion(int item){
int flag, i;
struct BTreeNode *child;

flag = setValueInNode(item, &i, root, &child);
if (flag)
root = createNode(i, child);
}

// Copy the successor
void copySuccessor(struct BTreeNode *myNode, int pos){
struct BTreeNode *dummy;

myNode->item[pos] = dummy->item[1];
}

// Remove the value
void removeVal(struct BTreeNode *myNode, int pos){
int i = pos + 1;
while (i <= myNode->count) {
myNode->item[i - 1] = myNode->item[i];
i++;
}
myNode->count--;
}

// Do right shift
void rightShift(struct BTreeNode *myNode, int pos){
int j = x->count;

while (j > 0) {
x->item[j + 1] = x->item[j];
}
x->item[1] = myNode->item[pos];
x->count++;

myNode->item[pos] = x->item[x->count];
x->count--;
return;
}

// Do left shift
void leftShift(struct BTreeNode *myNode, int pos){
int j = 1;
struct BTreeNode *x = myNode->linker[pos - 1];

x->count++;
x->item[x->count] = myNode->item[pos];

myNode->item[pos] = x->item[1];
x->count--;

while (j <= x->count) {
x->item[j] = x->item[j + 1];
j++;
}
return;
}

// Merge the nodes
void mergeNodes(struct BTreeNode *myNode, int pos){
int j = 1;

x2->count++;
x2->item[x2->count] = myNode->item[pos];

while (j <= x1->count) {
x2->count++;
x2->item[x2->count] = x1->item[j];
j++;
}

j = pos;
while (j < myNode->count) {
myNode->item[j] = myNode->item[j + 1];
j++;
}
myNode->count--;
free(x1);
}

void adjustNode(struct BTreeNode *myNode, int pos){
if (!pos) {
leftShift(myNode, 1);
} else {
mergeNodes(myNode, 1);
}
} else {
if (myNode->count != pos) {
if (myNode->linker[pos - 1]->count > MIN) {
rightShift(myNode, pos);
} else {
if (myNode->linker[pos + 1]->count > MIN) {
leftShift(myNode, pos + 1);
} else {
mergeNodes(myNode, pos);
}
}
} else {
if (myNode->linker[pos - 1]->count > MIN)
rightShift(myNode, pos);
else
mergeNodes(myNode, pos);
}
}
}

// Delete a value from the node
int delValFromNode(int item, struct BTreeNode *myNode){
int pos, flag = 0;
if (myNode) {
if (item < myNode->item[1]) {
pos = 0;
flag = 0;
} else {
for (pos = myNode->count; (item < myNode->item[pos] && pos > 1); pos--)
;
if (item == myNode->item[pos]) {
flag = 1;
} else {
flag = 0;
}
}
if (flag) {
copySuccessor(myNode, pos);
if (flag == 0) {
printf("Given data is not present in B-Treen");
}
} else {
removeVal(myNode, pos);
}
} else {
}
}
}
return flag;
}

// Delete operaiton
void delete (int item, struct BTreeNode *myNode){
struct BTreeNode *tmp;
if (!delValFromNode(item, myNode)) {
printf("Not presentn");
return;
} else {
if (myNode->count == 0) {
tmp = myNode;
free(tmp);
}
}
root = myNode;
return;
}

void searching(int item, int *pos, struct BTreeNode *myNode){
if (!myNode) {
return;
}

if (item < myNode->item[1]) {
*pos = 0;
} else {
for (*pos = myNode->count;
(item < myNode->item[*pos] && *pos > 1); (*pos)--)
;
if (item == myNode->item[*pos]) {
printf("%d present in B-tree", item);
return;
}
}
return;
}

void traversal(struct BTreeNode *myNode){
int i;
if (myNode) {
for (i = 0; i < myNode->count; i++) {
printf("%d ", myNode->item[i + 1]);
}
}
}

int main(){
int item, ch;

insertion(8);
insertion(9);
insertion(10);
insertion(11);
insertion(15);
insertion(16);
insertion(17);
insertion(18);
insertion(20);
insertion(23);

traversal(root);

delete (20, root);
printf("n");
traversal(root);
}``````

## Deletion Complexity

Best case Time complexity: `Θ(log n)`

Average case Space complexity: `Θ(n)`

Worst case Space complexity: `Θ(n)`

# Special 95% discount

## Two Machine Learning Fields

There are two sides to machine learning:

• Practical Machine Learning:This is about querying databases, cleaning data, writing scripts to transform data and gluing algorithm and libraries together and writing custom code to squeeze reliable answers from data to satisfy difficult and ill defined questions. It’s the mess of reality.
• Theoretical Machine Learning: This is about math and abstraction and idealized scenarios and limits and beauty and informing what is possible. It is a whole lot neater and cleaner and removed from the mess of reality.

Data Science Resources: Data Science Recipes and Applied Machine Learning Recipes

Introduction to Applied Machine Learning & Data Science for Beginners, Business Analysts, Students, Researchers and Freelancers with Python & R Codes @ Western Australian Center for Applied Machine Learning & Data Science (WACAMLDS) !!!

Latest end-to-end Learn by Coding Recipes in Project-Based Learning:

Applied Statistics with R for Beginners and Business Professionals

Data Science and Machine Learning Projects in Python: Tabular Data Analytics

Data Science and Machine Learning Projects in R: Tabular Data Analytics

Python Machine Learning & Data Science Recipes: Learn by Coding

R Machine Learning & Data Science Recipes: Learn by Coding

Comparing Different Machine Learning Algorithms in Python for Classification (FREE)

`Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.  `