b tree in c

Solutions on MaxInterview for b tree in c by the best coders in the world

showing results for - "b tree in c"
Glynis
02 Jun 2017
1// Searching a key on a B-tree in C
2
3#include <stdio.h>
4#include <stdlib.h>
5
6#define MAX 3
7#define MIN 2
8
9struct btreeNode
10{
11  int val[MAX + 1], count;
12  struct btreeNode *link[MAX + 1];
13};
14
15struct btreeNode *root;
16
17struct btreeNode *createNode(int val, struct btreeNode *child)
18{
19  struct btreeNode *newNode;
20  newNode = (struct btreeNode *)malloc(sizeof(struct btreeNode));
21  newNode->val[1] = val;
22  newNode->count = 1;
23  newNode->link[0] = root;
24  newNode->link[1] = child;
25  return newNode;
26}
27
28void addValToNode(int val, int pos, struct btreeNode *node,
29          struct btreeNode *child)
30{
31  int j = node->count;
32  while (j > pos)
33  {
34    node->val[j + 1] = node->val[j];
35    node->link[j + 1] = node->link[j];
36    j--;
37  }
38  node->val[j + 1] = val;
39  node->link[j + 1] = child;
40  node->count++;
41}
42
43void splitNode(int val, int *pval, int pos, struct btreeNode *node,
44         struct btreeNode *child, struct btreeNode **newNode)
45{
46  int median, j;
47
48  if (pos > MIN)
49    median = MIN + 1;
50  else
51    median = MIN;
52
53  *newNode = (struct btreeNode *)malloc(sizeof(struct btreeNode));
54  j = median + 1;
55  while (j <= MAX)
56  {
57    (*newNode)->val[j - median] = node->val[j];
58    (*newNode)->link[j - median] = node->link[j];
59    j++;
60  }
61  node->count = median;
62  (*newNode)->count = MAX - median;
63
64  if (pos <= MIN)
65  {
66    addValToNode(val, pos, node, child);
67  }
68  else
69  {
70    addValToNode(val, pos - median, *newNode, child);
71  }
72  *pval = node->val[node->count];
73  (*newNode)->link[0] = node->link[node->count];
74  node->count--;
75}
76
77int setValueInNode(int val, int *pval,
78           struct btreeNode *node, struct btreeNode **child)
79{
80
81  int pos;
82  if (!node)
83  {
84    *pval = val;
85    *child = NULL;
86    return 1;
87  }
88
89  if (val < node->val[1])
90  {
91    pos = 0;
92  }
93  else
94  {
95    for (pos = node->count;
96       (val < node->val[pos] && pos > 1); pos--)
97      ;
98    if (val == node->val[pos])
99    {
100      printf("Duplicates not allowed\n");
101      return 0;
102    }
103  }
104  if (setValueInNode(val, pval, node->link[pos], child))
105  {
106    if (node->count < MAX)
107    {
108      addValToNode(*pval, pos, node, *child);
109    }
110    else
111    {
112      splitNode(*pval, pval, pos, node, *child, child);
113      return 1;
114    }
115  }
116  return 0;
117}
118
119void insert(int val)
120{
121  int flag, i;
122  struct btreeNode *child;
123
124  flag = setValueInNode(val, &i, root, &child);
125  if (flag)
126    root = createNode(i, child);
127}
128
129void search(int val, int *pos, struct btreeNode *myNode)
130{
131  if (!myNode)
132  {
133    return;
134  }
135
136  if (val < myNode->val[1])
137  {
138    *pos = 0;
139  }
140  else
141  {
142    for (*pos = myNode->count;
143       (val < myNode->val[*pos] && *pos > 1); (*pos)--)
144      ;
145    if (val == myNode->val[*pos])
146    {
147      printf("%d is found", val);
148      return;
149    }
150  }
151  search(val, pos, myNode->link[*pos]);
152
153  return;
154}
155
156void traversal(struct btreeNode *myNode)
157{
158  int i;
159  if (myNode)
160  {
161    for (i = 0; i < myNode->count; i++)
162    {
163      traversal(myNode->link[i]);
164      printf("%d ", myNode->val[i + 1]);
165    }
166    traversal(myNode->link[i]);
167  }
168}
169
170int main()
171{
172  int val, ch;
173
174  insert(8);
175  insert(9);
176  insert(10);
177  insert(11);
178  insert(15);
179  insert(16);
180  insert(17);
181  insert(18);
182  insert(20);
183  insert(23);
184
185  traversal(root);
186
187  printf("\n");
188  search(11, &ch, root);
189}
190
similar questions
tree traversal in c
queries leading to this page
b tree c 2b 2bb tree search complexitydesign file system using balanced treeb tree vs b 2b treeb tree formulab tree codetree b javab 2fb 2b 2b treegfg b treeimplement b tree data structure with the following functions 3ab tree javastate application of b 2c b 2b 2c avl 2c huffman trees 5dimplementation of b tree in cmaxed out b tree of 3 degree2 3 tree is a specific form of immersive reader b e2 80 93 tree b 2b e2 80 93 tree avl tree heapprogram to implement b trees in cbtree javab tree cppif you are making a b tree of order 4 and you have 10 values 2c what can be the maxium hight of that b treepotential function of b tree calculation b tree gfgb tree algorithmb tree code explanation with examplehow to set b 2btree degrees orderb tree code in cwhat is a b tree3 level b tree exampleb tree implementation in cb tree exampleb tree code in cb tree with 3 levelswhich type of data structure is having less number of levels b tree b 2b treebtree to find root nodehow to make b tree in c 2b 2bb tree program in cppstate application of b 2c b 2b 2c avl 2c huffman treeschoose correct statements with respect b treesbinary tree with b in cb tree in cwhat are the rules of b treeswhat is b tresstree a 3eb 3ecb tree implementationoptimal b 2btree propertiesin b tree c programing what is the order of the following b tree mentioned here 3f 5b1 2cbtl3 2cco5 2cpo1 2cpo2 5d select one 3a a b tree of order 3 b none of the mentioned options c b tree of order 2 d b tree of order 4algorithm for creating b treeb tree implementation in cb tree in b tree search algorithmb tree using chow to create a b tree in cwhat is the complexity of a searching a key in b tree of n elemntsthe root node has atmost m non empty chilren in b treeb tree time complexityimplementing b tree in c 2b 2b using linked listb tree with degree 2what are b treeb tree c code btree data structure lookup timeb tree exampledefine btree variables c 2b 2bhow to set b 2btree order describe what problem b trees solve and how they solve itbtree diagramwhat are b trees in cb tree degreehow to make a b tree in c 2b 2bb tree program in cvalid way of a b tree node to growb tree creation softwareb tree in data structureif order of the b tree is 3 then how many minimum number of sub treesb tree ic cb tree and b 2b treeb tree example fixed size blockwhat is b treeif the sub tree s1 has the height of 1 2c what is the minimum number of nodes and maximum number of nodes in the subtree s1 3f count the intermediate nodes and the leaf nodes in the sub tree s1 b tree implmentation codeb tree traversal to replicate antoher b treeb 2a treeb tree properties b tree pythonb treehow to find in a btree implementcomplete b treeb tree in dbmsa bit tree is a special treeb tree javab tree noteb tree in c