B tree program in c is another type of tree data structure. It is also called a multiway search tree.

B tree Definition
In External searching out aim is to minimize the file access and this can be done by reducing the height of the trees. The height to M-way tree is less because of its large branching factor, but its height can still be reduced. If it is balanced so a new tree structure was developed by Bayer which was a height-balanced M-way tree that is named as B tree.

B tree properties
All leaf nodes are at the same level.
And All non-leaf (except root) should have at least [M/2] Child.
All nodes should have at least [M/2-1] keys.
If the root node is nonleaf then it has at least 2 children and at least one key.
A non-leaf with N-1 key value should have non-null child.

Insertion in B Tree

B tree Deletion

Searching in B tree

Page Contents

## B tree program in c

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

#define MAX 4
#define MIN 2

struct btreeNode {
int val[MAX + 1], count;
};

struct btreeNode *root;

/* creating new node */
struct btreeNode * createNode(int val, struct btreeNode *child) {
struct btreeNode *newNode;
newNode = (struct btreeNode *)malloc(sizeof(struct btreeNode));
newNode->val[1] = val;
newNode->count = 1;
return newNode;
}

/* Places the value in appropriate position */
void addValToNode(int val, int pos, struct btreeNode *node,
struct btreeNode *child) {
int j = node->count;
while (j > pos) {
node->val[j + 1] = node->val[j];
j--;
}
node->val[j + 1] = val;
node->count++;
}

/* split the node */
void splitNode (int val, 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)->val[j - median] = node->val[j];
j++;
}
node->count = median;
(*newNode)->count = MAX - median;

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

/* sets the value val in the node */
int setValueInNode(int val, int *pval,
struct btreeNode *node, struct btreeNode **child) {

int pos;
if (!node) {
*pval = val;
*child = NULL;
return 1;
}

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

/* insert val in B-Tree */
void insertion(int val) {
int flag, i;
struct btreeNode *child;

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

/* copy successor for the value to be deleted */
void copySuccessor(struct btreeNode *myNode, int pos) {
struct btreeNode *dummy;

myNode->val[pos] = dummy->val[1];

}

/* removes the value from the given node and rearrange values */
void removeVal(struct btreeNode *myNode, int pos) {
int i = pos + 1;
while (i <= myNode->count) {
myNode->val[i - 1] = myNode->val[i];
i++;
}
myNode->count--;
}

/* shifts value from parent to right child */
void doRightShift(struct btreeNode *myNode, int pos) {
int j = x->count;

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

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

/* shifts value from parent to left child */
void doLeftShift(struct btreeNode *myNode, int pos) {
int j = 1;
struct btreeNode *x = myNode->link[pos - 1];

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

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

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

/* merge nodes */
void mergeNodes(struct btreeNode *myNode, int pos) {
int j = 1;

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

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

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

/* adjusts the given node */
void adjustNode(struct btreeNode *myNode, int pos) {
if (!pos) {
doLeftShift(myNode, 1);
} else {
mergeNodes(myNode, 1);
}
} else {
if (myNode->count != pos) {
if(myNode->link[pos - 1]->count > MIN) {
doRightShift(myNode, pos);
} else {
if (myNode->link[pos + 1]->count > MIN) {
doLeftShift(myNode, pos + 1);
} else {
mergeNodes(myNode, pos);
}
}
} else {
if (myNode->link[pos - 1]->count > MIN)
doRightShift(myNode, pos);
else
mergeNodes(myNode, pos);
}
}
}

/* delete val from the node */
int delValFromNode(int val, struct btreeNode *myNode) {
int pos, flag = 0;
if (myNode) {
if (val < myNode->val[1]) {
pos = 0;
flag = 0;
} else {
for (pos = myNode->count;
(val < myNode->val[pos] && pos > 1); pos--);
if (val == myNode->val[pos]) {
flag = 1;
} else {
flag = 0;
}
}
if (flag) {
copySuccessor(myNode, pos);
if (flag == 0) {
printf("Given data is not present in B-Tree\n");
}
} else {
removeVal(myNode, pos);
}
} else {
}
}
}
return flag;
}

/* delete val from B-tree */
void deletion(int val, struct btreeNode *myNode) {
struct btreeNode *tmp;
if (!delValFromNode(val, myNode)) {
printf("Given value is not present in B-Tree\n");
return;
} else {
if (myNode->count == 0) {
tmp = myNode;
free(tmp);
}
}
root = myNode;
return;
}

/* search val in B-Tree */
void searching(int val, int *pos, struct btreeNode *myNode) {
if (!myNode) {
return;
}

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

/* B-Tree Traversal */
void traversal(struct btreeNode *myNode) {
int i;
if (myNode) {
for (i = 0; i < myNode->count; i++) {
printf("%d ", myNode->val[i + 1]);
}
}
}

int main() {
int val, ch;
while (1) {
printf("1. Insertion\t2. Deletion\n");
printf("3. Searching\t4. Traversal\n");
scanf("%d", &ch);
switch (ch) {
case 1:
scanf("%d", &val);
insertion(val);
break;
case 2:
printf("Enter the element to delete:");
scanf("%d", &val);
deletion(val, root);
break;
case 3:
printf("Enter the element to search:");
scanf("%d", &val);
searching(val, &ch, root);
break;
case 4:
traversal(root);
break;
case 5:
exit(0);
default:
printf("U have entered wrong option!!\n");
break;
}
printf("\n");
}
}
```

Output :