0% found this document useful (0 votes)
66 views4 pages

Lab 2 - B Tree

This document contains the code for implementing B-tree operations in C including functions for creating nodes, inserting nodes, splitting nodes, setting values, inserting values, searching nodes, and traversing nodes. The main function provides a menu to insert elements, print the traversal, search the tree, or exit.

Uploaded by

kartheekmurala19
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views4 pages

Lab 2 - B Tree

This document contains the code for implementing B-tree operations in C including functions for creating nodes, inserting nodes, splitting nodes, setting values, inserting values, searching nodes, and traversing nodes. The main function provides a menu to insert elements, print the traversal, search the tree, or exit.

Uploaded by

kartheekmurala19
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

//Program to implement B-tree operations in C

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

#define MAX 3
#define MIN 2

struct BTreeNode {
int val[MAX + 1], count;
struct BTreeNode *link[MAX + 1];
};

struct BTreeNode *root;

// Create a 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;
newNode->link[0] = root;
newNode->link[1] = child;
return newNode;
}

// Insert node
void insertNode(int val, int pos, struct BTreeNode *node,
struct BTreeNode *child)
{
int j = node->count;
while (j > pos) {
node->val[j + 1] = node->val[j];
node->link[j + 1] = node->link[j];
j--;
}
node->val[j + 1] = val;
node->link[j + 1] = child;
node->count++;
}

// Split 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];
(*newNode)->link[j - median] = node->link[j];
j++;
}
node->count = median;
(*newNode)->count = MAX - median;

if (pos <= MIN) {


insertNode(val, pos, node, child);
} else {
insertNode(val, pos - median, *newNode, child);
}
*pval = node->val[node->count];
(*newNode)->link[0] = node->link[node->count];
node->count--;
}

// Set the value


int setValue(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 are not permitted\n");
return 0;
}
}
if (setValue(val, pval, node->link[pos], child)) {
if (node->count < MAX) {
insertNode(*pval, pos, node, *child);
} else {
splitNode(*pval, pval, pos, node, *child, child);
return 1;
}
}
return 0;
}

// Insert the value


void insert(int val) {
int flag, i;
struct BTreeNode *child;

flag = setValue(val, &i, root, &child);


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

// Search node
void search(int val, struct BTreeNode *myNode)
{
int pos;
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("%d is found", val);
return;
}
}
search(val, myNode->link[pos]);

return;
}
// Traverse then nodes
void traversal(struct BTreeNode *myNode) {
int i;
if (myNode) {
for (i = 0; i < myNode->count; i++) {
traversal(myNode->link[i]);
printf("%d ", myNode->val[i + 1]);
}
traversal(myNode->link[i]);
}
}

int main()
{

int val,it,s;

while(1)
{

int op;
printf("\n\n\t1.Insert an element into B_tree");
printf("\n\t 2.Print traversal of B_tree");
printf("\n\t 3.Search B_tree");
printf("\n\t 4.Exit");
printf("\n\tNter your choice :: ");
scanf("%d",&op);
switch(op)
{
case 1:
printf("\n Nter the element to be inserted ::");
scanf("%d",&it);
insert(it);
break;
case 2:
printf("\nB-Tree contains ::\n");
traversal(root);
break;
case 3:
printf("\n Nter the element to search B-tree :: ");
scanf("%d",&s);
search(s, root);
break;
case 4:
exit(1);
}
}
}

You might also like