-
Notifications
You must be signed in to change notification settings - Fork 3
/
B+.h
320 lines (320 loc) · 6.41 KB
/
B+.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#pragma once
#include<iostream>
#include<string>
using namespace std;
# define count 100
//a tree node declared as class
class node;
class nodeprt
{
public:
string data;
node * down;
nodeprt()
{
data = "";
down = NULL;
}
};
class node
{
public:
//an array pointing to the nodeparts
nodeprt * parts[count];
bool isleaf;
node * left_down;
node * up;
node * right;
node()
{
for (int i = 0; i < count; i++)
{
parts[i] = new nodeprt();
parts[i] = NULL;
}
}
}*root, *endleaf;
//a function to split a node which would return the root node part to be
//further inserted in the up node
nodeprt * split(node * source)
{
node * temp_child2;
temp_child2 = new node;
nodeprt * temp_root;
temp_root = new nodeprt;
temp_root = source->parts[(int)((count - 1) / 2)];
for (int i = (int)((count - 1) / 2) + 1; i < count; i++)
{
temp_child2->parts[i - (int)(count / 2)] = new nodeprt;
temp_child2->parts[i - (int)(count / 2)] = source->parts[i];
temp_child2->parts[i] = NULL;
source->parts[i] = NULL;
}
temp_root->down = temp_child2;
source->right = temp_child2;
return (temp_root);
}
//a function to insert a node part into a node
void insert(node * source, nodeprt * temp_root);
//a function to insert a srting into a node
void insert(node * source, string dat_ins)
{
//checking if the node has a vacant space and if it is not fully vacant
if (source->parts[count - 1] == NULL && source->parts[0] != NULL)
{
int i = 0;
//checking the place where the data has to be inserted
while (source->parts[i] != NULL && source->parts[i]->data < dat_ins)
{
i++;
}
//checking if there is no value existing at the place where data has
//to be inserted
if (source->parts[i] == NULL)
{
source->parts[i] = new nodeprt;
source->parts[i]->data = dat_ins;
}
//to be executed if the place where data is to be inserted is already
//full
else
{
//loop to check the place which is empty
int j = i;
while (source->parts[j] != NULL)
{
j++;
}
source->parts[j] = new nodeprt;
source->parts[j] = source->parts[j - 1];
//loop to shift all the values to the right and insert the value
//at the required place
for (int k = j; k > i - 1; k--)
{
source->parts[k] = source->parts[k - 1];
}
source->parts[i] = new nodeprt;
source->parts[i]->data = dat_ins;
}
source->isleaf = true;
source->right = NULL;
}
//if the tree doesn't exist
else if (source->parts[0] == NULL)
{
source->parts[0] = new nodeprt;
source->parts[0]->data = dat_ins;
root = source;
source->isleaf = true;
}
//if the node where the value is to be inserted is already full
else
{
nodeprt * temp;
temp = new nodeprt;
//splitting the source
temp = split(source);
//inserting data after splitting of the full node
if (dat_ins < temp->data)
{
insert(source, dat_ins);
}
else
{
insert(source->right, dat_ins);
}
//making the nodepart returned as the root node if there is no preexisting
//node above it
if (source->up == NULL)
{
node * temp1;
temp1 = new node;
temp1->parts[0] = new nodeprt;
temp1->parts[0] = temp;
for (int i = 1; i < count; i++)
{
temp1->parts[i] = new nodeprt;
temp1->parts[i] = NULL;
}
root = temp1;
source->up = root;
source->right->up = root;
root->left_down = source;
}
//inserting the nodepart in the above node
else
{
//recursive call with function overloading to insert the nodepart
//into the above node
insert(source->up, temp);
}
}
}
//a function to insert the nodepart returned after the splitting into a node
void insert(node * source, nodeprt * temp_root)
{
if (source->parts[count - 1] == NULL)
{
int i = 0;
while (source->parts[i]->data < temp_root->data)
{
i++;
}
if (source->parts[i] == NULL)
{
source->parts[i] = new nodeprt;
source->parts[i] = temp_root;
}
else
{
int j = i;
while (source->parts[j] != NULL)
{
j++;
}
source->parts[j] = new nodeprt;
source->parts[j] = source->parts[j - 1];
for (int k = j; k > i - 1; k--)
{
source->parts[k] = source->parts[k - 1];
}
source->parts[i] = new nodeprt;
source->parts[i] = temp_root;
}
}
else
{
nodeprt * temp;
temp = new nodeprt;
temp = split(source);
if (temp_root->data < temp->data)
{
insert(source, temp_root->data);
}
if (source->up == NULL)
{
node * temp1;
temp1 = new node;
temp1->parts[0] = new nodeprt;
temp1->parts[0] = temp;
for (int i = 1; i < count; i++)
{
temp1->parts[i] = new nodeprt;
temp1->parts[i] = NULL;
}
root = temp1;
source->up = root;
root->left_down = source;
}
else
{
insert(source->up, temp);
}
}
}
//a function to search for a particular data
bool search(node * source, string a)
{
bool exists = false;
if (source->isleaf == true)
{
int i = 0;
while (i < count && source->parts[i] != NULL)
{
if (source->parts[i]->data == a)
{
exists = true;
}
i++;
}
return (exists);
}
else
{
if (source->parts[0]->data >= a)
{
return (search(source->left_down, a));
}
else
{
int j = 0;
while ((source->parts[j] != NULL) && (source->parts[j]->data < a))
{
j++;
}
return(search(source->parts[j - 1]->down, a));
}
}
}
void del(node * source, string to_del)
{
if (search(root, to_del) == true)
{
if (source->parts[0]->data >= to_del)
{
del(source->left_down, to_del);
}
else
{
int i = 0;
while (source->parts[i] != NULL && source->parts[i]->data <
to_del)
{
i++;
}
if (source->isleaf == true && source->parts[i]->data == to_del)
{
source->parts[i]->data = "";
for (int j = i; j < (count - 1); j++)
{
source->parts[j] = source->parts[j + 1];
}
}
else
{
del(source->parts[i - 1]->down, to_del);
}
}
}
else
{
cout << "\n Data doesn't exist in the database ";
}
}
void insert(string dat_ins)
{
if (root == NULL)
{
endleaf = new node;
insert(endleaf, dat_ins);
root = new node;
root = endleaf;
}
else if (root == endleaf)
{
insert(endleaf, dat_ins);
}
else
{
node * temp = root;
while (temp->isleaf != true)
{
if (dat_ins < temp->parts[0]->data)
{
insert(temp->left_down, dat_ins);
break;
}
else
{
int i = 0;
while (temp->parts[i] != NULL && temp->parts[i]->data <
dat_ins)
{
i++;
}
temp = temp->parts[i - 1]->down;
insert(temp, dat_ins);
}
}
}
}