-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
409 lines (273 loc) · 11.7 KB
/
app.js
File metadata and controls
409 lines (273 loc) · 11.7 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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
const { json } = require("body-parser");
const { stdin, stdout } = require("process");
const fs=require('fs').promises;
const readline=require('readline');
const { parse } = require("path");
const r1=readline.createInterface({
input:process.stdin,
output: process.stdout
})
// taking record data
function askQuestion(query) {
return new Promise((resolve) => r1.question(query, resolve));
}
// function to retrieve the record data for specific id from the file if it exist
async function get_record(id,page_number){
let data=await fs.readFile(`${page_number}.txt`);
data=data.toString();
data.split(0,data.length-1);
data=data.split('\n');
data.forEach((rec)=>{
rec=rec.split(',');
if(rec.length===3){
rec[0]=parseInt(rec[0]);
if(rec[0]===id){
console.log(`and his id is ${rec[0]}, his name is ${rec[1]}, his age is ${rec[2]}`)
}
}
})
}
// if overflow ocur in node and keys become 3
function split(i,parent){ // i is the ind of the full child and n is the parent
let full_node=parent.childs[i];
let median=full_node.keys[1];
parent.count++;
let left_node=new node(full_node.leaf);
left_node.keys.push(full_node.keys[2]);
left_node.count=1;
for (let j=2;j<full_node.childs.length;j++){
left_node.childs.push(full_node.childs[j]);
}
full_node.count=1;
full_node.keys.splice(1,2);
full_node.childs.splice(2);
parent.keys.push(median);
parent.keys.sort((a,b)=>a-b)
parent.childs.splice(i+1,0,left_node);
}
// node of the b tree
class node {
constructor(leaf){
this.leaf=leaf;
this.count=0;
this.keys=[]; // each key will be an object contain key value and page number {key , page_number}
this.childs=[];
}
}
// b tree class with insert method and handle if root's count become 3
class b_tree{
constructor(){
this.root=null;
}
insert(element,page){
let key_obj={value:element,page_number:page};
// if the b tree dosn't contain any node
if (!this.root){
let nod=new node(1);
nod.keys.push(key_obj);
nod.count=1;
this.root=nod;
}
// if the root is the only node in the tree and contain 1 key or 2
else if (this.root.leaf){
if (this.root.count===1){
this.root.keys.push(key_obj);
}
else if (this.root.count===2){
this.root.keys.push(key_obj);
}
this.root.keys.sort((a,b)=>a.value-b.value);
this.root.count+=1;
}
// if the root is not a leaf node
else {
function recur(current){
if (current.childs[0]&¤t.childs[0].leaf&&element<current.keys[0].value){
current.childs[0].keys.push(key_obj);
current.childs[0].count++;
current.childs[0].keys.sort((a, b) => {
return a.value - b.value;
});
if (current.childs[0].count===3){
split(0,current);
}
return ;
}
if (current.count===1&¤t.childs[1]&¤t.childs[1].leaf&&element>current.keys[0].value){
current.childs[1].keys.push(key_obj);
current.childs[1].count++;
current.childs[1].keys.sort((a, b) => {
return a.value - b.value;
});
if (current.childs[1].count===3){
split(1,current);
}
return ;
}
if (current.count==2&¤t.childs[1]&¤t.childs[1].leaf&&element<current.keys[1].value){
current.childs[1].keys.push(key_obj);
current.childs[1].count++;
current.childs[1].keys.sort((a, b) => {
return a.value - b.value;
});
if (current.childs[1].count==3){
split(1,current);
}
return ;
}
if (current.childs[2]&¤t.childs[2].leaf&&element>current.keys[1].value){
current.childs[2].keys.push(key_obj);
current.childs[2].count++;
current.childs[2].keys.sort((a, b) => {
return a .value- b.value;
});
if (current.childs[2].count==3){
split(2,current);
}
return ;
}
if (current.childs[0]&&element<current.keys[0].value){
recur(current.childs[0]);
}
else if ((current.childs[1]&&element>current.keys[0].value&¤t.count==1)||(current.childs[1]&¤t.count==2&&element>current.keys[0].value&&element<current.keys[1].value)){
recur(current.childs[1]);
}
else if (current.childs[2]&¤t.count==2&&element>current.keys[1].value){
recur(current.childs[2]);
}
// recursively after insertion check overflowed nodes
for (let i=0;i<3;i++){
if (current.childs[i]&¤t.childs[i].count==3){
split(i,current);
}
}
}
recur(this.root);
}
// check after insertion if the root is full and count =3
if (this.root.count===3){
let new_root=new node(0);
new_root.count=1;
new_root.keys.push(this.root.keys[1]);
let left_node=new node(Number(!this.root.childs[2]&&!this.root.childs[3]));
left_node.keys.push(this.root.keys[2]);
left_node.count=1;
this.root.count=1;
for (let j=2;j<this.root.childs.length;j++){ // if the root has more than three childs now
left_node.childs.push(this.root.childs[j]);
}
this.root.keys.splice(1,2);
this.root.childs.splice(2,2);
let old_root=this.root;
this.root=new_root;
this.root.childs.push(old_root);
this.root.childs.push(left_node);
}
}
}
let btree=new b_tree();
async function construct_tree(){
if (!btree.root){
let x=await fs.readFile('node.txt');
x=x.toString();
x=x.slice(0,x.length-1);
x=x.split(',').map(Number);
for(var i=0;i<(x.length);i+=2){
btree.insert(x[i],x[i+1]);
}
}
}
construct_tree();
// insert operation (id,name,age)
async function handle_input() {
try {
const id = await ((askQuestion("Enter your ID: ")));
let id_=parseInt(id,10);
const name = await askQuestion("What is your name? ");
const age = await askQuestion("What is your age? ");
const insertion_data = `${id_},${name},${age}\n`;
// Read current_page.json
let data = await fs.readFile('current_page.json', 'utf8');
const obj = JSON.parse(data);
let page = obj["current_bage"], cnt = obj["current_count"];
if (cnt === 3) {
// Create a new file
const newFile = `${page + 1}.txt`;
await fs.writeFile(newFile, insertion_data);
obj["current_bage"]++;
obj["current_count"] = 1;
} else {
// Append to the existing file
const oldFile = `${page}.txt`;
await fs.appendFile(oldFile, insertion_data);
obj["current_count"]++;
}
// Update the btree node file
let node=`${id_},${obj.current_bage},`;
await fs.appendFile('node.txt',node);
btree.insert(id_,obj.current_bage);
// Write back the updated current_page.json
const json_data = JSON.stringify(obj, null, 2);
await fs.writeFile('current_page.json', json_data);
console.log("insertion completed successfully.");
} catch (err) {
console.error("An error occurred:", err);
}
}
// search for specific id
async function handle_search(id){
let location=0;
function recur(node){
if (!node){
return ;
}
for (let i=0;i<node.keys.length;i++){
if (node.keys[i].value===id){
location=node.keys[i].page_number+1;
return ;
}
}
let ops1=0,ops2=0,ops3=0,ops4=0,ops5=0;
if (node.count===1&&id>node.keys[0].value){
ops1=recur(node.childs[1]);
}
else if (node.count===1&&id<node.keys[0].value){
ops2=recur(node.childs[0]);
}
else if (node.count===2&&id<node.keys[0].value){
ops3=recur(node.childs[0]);
}
else if (node.count===2&&id>node.keys[0].value&&id<node.keys[1].value){
ops4=recur(node.childs[1]);
}
else if (node.count===2&&id>node.keys[1].value&&node.childs[2]){
ops5=recur(node.childs[2]);
}
}
recur(btree.root);
if (location){
console.log("this employee exist ");
await get_record(id,location-1);
}
else {
console.log("not exist");
}
}
(async () => {
for (let j = 0; ; j++) {
let query=await askQuestion("enter your query {insert,search,exit}");
if (query==="insert"){
console.log("enter the data",'\n');
await handle_input();
}
else if (query==="search") {
x=await askQuestion("enter the id ");
x=parseInt(x,10);
await handle_search(x);
}
else if (query==="exit") {
break;
}
}
r1.close();
})();