-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2-3_trees1.cpp
More file actions
336 lines (303 loc) · 9.34 KB
/
2-3_trees1.cpp
File metadata and controls
336 lines (303 loc) · 9.34 KB
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#include <iostream>
#include <fstream> //header file for file managing...
#include <stdlib.h>
using namespace std;
class Node
{
int n;
int key[3]; //array of keys...
Node *child[4]; //array for child...
bool leaf;
Node *parent; //variable for parent of a node...
public:
Node* getNode();
Node* getNode(int item);
Node* splitNode(Node *root);
Node* insert(Node *root , int item);
void Traversal(Node *root);
void write_item(Node *root , std::ofstream& ofile);
void Graph(Node *root);
};
//Return empty node...
Node* Node :: getNode()
{
Node *temp = new Node[10];
temp->n = 0;
for(int i=0;i<4;i++)
temp->child[i] = NULL;
return temp;
}
//Returns weighted node...
Node* Node :: getNode(int item)
{
Node *temp = new Node[10];
temp->n = 0;
for(int i=0;i<4;i++)
temp->child[i] = NULL;
temp->key[0] = item;
temp->n = 1;
temp->leaf = true;
temp->parent = NULL;
return temp;
}
//to write a file...
void Node :: write_item(Node *root , std::ofstream& ofile)
{
if(root == NULL) //to exit from recursion if no further nodes...
return;
if(root->leaf == true and root->parent==NULL) //only for root node...
{
ofile<<'"';
for(int i=0;i<root->n;i++)
ofile<<root->key[i]<<",";
ofile<<'"';
}
else if(root->n == 1) //node with two childrens...
{
int k=0;
while(k<2 and root->child[0]!=NULL) //To process both childrens...
{
ofile<<'"';
for(int i=0;i<root->n;i++)
ofile<<root->key[i]<<" ";
ofile<<'"'<<"--"<<'"';
for(int i=0;i<root->child[k]->n;i++)
ofile<<root->child[k]->key[i]<<" ";
ofile<<'"'<<endl;
k++;
}
write_item(root->child[0],ofile); //left child traversal...
write_item(root->child[1],ofile); //right child traversal...
}
else if(root->child[2]!=NULL) //node with 3 childrens...
{
int k=0;
while(k<3)
{
ofile<<'"';
for(int i=0;i<root->n;i++)
ofile<<root->key[i]<<" ";
ofile<<'"'<<"--"<<'"';
for(int i=0;i<root->child[k]->n;i++)
ofile<<root->child[k]->key[i]<<" ";
ofile<<'"'<<endl;
k++;
}
write_item(root->child[0],ofile); //1st child traversal...
write_item(root->child[1],ofile); //2nd child traversal...
write_item(root->child[2],ofile); //3rd child traversal...
}
}
//To create Graph using Graphviz...
void Node :: Graph(Node *root)
{
ofstream ofile;
ofile.open("diagram.dot"); //creating file...
ofile<<"Graph{\n";
write_item(root,ofile);
ofile<<"}\n";
ofile.close();
}
//Inorder traversal to display elements in sorted order...
void Node :: Traversal(Node *root)
{
if(root == NULL) //Base case of recursion...
return;
if(root->leaf == true)
for(int i=0;i<root->n;i++)
cout<<root->key[i]<<" ";
else if(root->n == 1) //Node containing 2 childs...
{
Traversal(root->child[0]);
for(int i=0;i<root->n;i++)
cout<<root->key[i]<<" ";
Traversal(root->child[1]);
}
else //Node containing 3 childs...
{
Traversal(root->child[0]);
cout<<root->key[0]<<" ";
Traversal(root->child[1]);
cout<<root->key[1]<<" ";
Traversal(root->child[2]);
}
}
//To split full node...
Node* Node :: splitNode(Node *node)
{
if(node->n != 3) //Base case to stop recursion...
return node;
if(node->parent == NULL) //splitting root node...
{
if(node->child[0] == NULL and node->child[1]==NULL) //when root has no childs...
{
node->child[0] = getNode(node->key[0]);
node->child[1] = getNode(node->key[2]);
node->key[0] = node->key[1];
node->child[0]->parent = node;
node->child[1]->parent = node;
node->n = 1;
node->leaf = false;
return node;
}
else //To split root which has 4 children...
{
Node *temp = getNode(node->key[1]);
temp->leaf = false;
Node *Z = getNode();
Z->leaf = false;
Z->parent = temp;
Z->key[0] = node->key[2];
Z->n = 1;
Z->child[0] = getNode();
Z->child[0] = node->child[2];
Z->child[0]->parent = Z;
node->child[2] = NULL;
Z->child[1] = getNode();
Z->child[1] = node->child[3];
Z->child[1]->parent = Z;
node->child[3] = NULL;
temp->child[1] = Z; //roots right child...
node->n = 1;
temp->child[0] = node; //root left child...
return temp;
}
}
else //if node is not root...
{
if(node->child[0] == NULL and node->child[1]==NULL) //If that node has no childs...
{
Node *temp = getNode(); //To make temp as nodes parent...
temp = node->parent;
int j = temp->n;
while(j>=0 and temp->child[j] != node) //To create nodes new sibling...
{
temp->child[j+1] = getNode();
temp->child[j+1] = temp->child[j];
j--;
}
int i = temp->n - 1;
while(i>=0 and temp->key[i] > node->key[1]) //to insert middle value to parent in sorted order...
{
temp->key[i+1] = temp->key[i];
i--;
}
temp->key[i+1] = node->key[1]; //Inserting at correct address...
temp->n += 1;
temp->child[j+1] = getNode(node->key[2]);
temp->child[j+1]->parent = temp;
temp->child[j+1]->leaf = true;
node->n = 1;
temp->child[j] = getNode();
temp->child[j] = node;
temp->child[j]->parent = temp;
temp->child[j]->leaf = true;
if(temp->n != 3) //If node->parent(temp) is not full return...else split by recursion...
return temp;
}
else //To split node which has childs...
{
Node *temp = getNode();
temp = node->parent;
int j = temp->n;
while(j>=0 and temp->child[j] != node) //To create nodes new sibling...
{
temp->child[j+1] = getNode();
temp->child[j+1] = temp->child[j];
j--;
}
int i = temp->n - 1;
while(i>=0 and temp->key[i] > node->key[1]) //to insert middle value to parent in sorted order...
{
temp->key[i+1] = temp->key[i];
i--;
}
temp->key[i+1] = node->key[1]; //To insert at correct address...
temp->n += 1;
Node *Z = getNode();
Z->leaf = false;
Z->parent = temp;
Z->key[0] = node->key[2];
Z->n = 1;
Z->child[0] = getNode();
Z->child[0] = node->child[2];
Z->child[0]->parent = Z;
node->child[2] = NULL;
Z->child[1] = getNode();
Z->child[1] = node->child[3];
Z->child[1]->parent = Z;
node->child[3] = NULL;
temp->child[j+1] = Z;
node->n = 1;
temp->child[j] = node;
return temp;
}
}
if(node->parent != NULL)
node = splitNode(node->parent); //To check whether node->parent is full or not...if full split...
return node;
}
Node* Node :: insert(Node *root , int item)
{
if(root == NULL) //base case to stop recursion...
return getNode(item);
if(root->leaf == true)
{
int i = root->n - 1;
while(i>=0 and root->key[i] > item)
{
root->key[i+1] = root->key[i];
i--;
}
root->key[i+1] = item;
root->n += 1;
if(root->n == 3) //If node is full , split...
root = splitNode(root);
while(root->parent != NULL) //To search for root...
root = root->parent;
return root; //Return root...
}
else
{
int i = root->n-1;
while(i>=0 and root->key[i] > item) //decide which child for next insertion...
i--;
root = insert(root->child[i+1],item);
return root;
}
}
int main()
{
Node ob;
Node *root; //Initail root which is NULL...
root = NULL;
int c;
while(true)
{
cout<<"\n1.Insert \n2.Display graph\n3.Sorted order\n4.exit\n";
cout<<"Enter your choice : ";
cin>>c;
switch(c)
{
case 1:cout<<"Enter an inserting element : ";
int item;
cin>>item;
root = ob.insert(root,item);
break;
case 2:ob.Graph(root);
break;
case 3:if(root == NULL)
cout<<"Empty\n";
else
{
cout<<"Sorted order : ";
ob.Traversal(root);
}
break;
case 4:exit(0);
default:cout<<"Invalid choice\n";
exit(0);
}
}
return 0;
}