-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtree0.cpp
More file actions
573 lines (506 loc) · 23.9 KB
/
Copy pathtree0.cpp
File metadata and controls
573 lines (506 loc) · 23.9 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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
#include <bits/stdc++.h>
#include <iostream>
//#include "BTree.cpp"
#include "segtree.cpp"
//#include "BST.cpp"
#include "csvAaryan.hpp"
using namespace std;
int main() {
SegmentTree<int> segTree1;
SegmentTree<float> segTree2;
BTree btree(4);
string fname;
while(true){
std::cout<<"1.CREATE\n2.READ/UPDATE/DELETE\n";
int crud_choice; std::cin>>crud_choice;
//Acc to choice, create or read a csv file
//CREATE
if(crud_choice==1){
std::cout<<"Enter Name of Database: ";
string dbname; std::cin>>dbname;
string filename=dbname+".csv";
CSVHandler check(filename,btree);
if(check.filexistchk(filename)==true){
std::cout<<"DB Already Exists!! Use a new name"<<endl;
continue;
}
vector<string> attributes(4);
std::cout<<"Enter Attribute Names:"<<endl;
for(int i=0; i<4; i++){
std::cin>>attributes[i];
}
//Enter these names in top of the file
//int aggregateAttribute; std::cin>>aggregateAttribute;
vector<int> segmentData1;
vector<float> segmentData2;
//Input of data
cout<<"Enter Data according to arrtibute format(ID NAME AGE SALARY)"<<endl;
while(true){
int key; string name; int attribute1; float attribute2;
std::cin>>key>>name>>attribute1>>attribute2;
btree.insert(key,name,attribute1, attribute2);
segmentData1.push_back(attribute1);
segmentData2.push_back(attribute2);
int choice; std::cout<<"Enter 1 to add more, 0 to exit: "; std::cin>>choice;
if(choice==0){
std::cout<<"Data Insersion Complete\n";
break;
}
}
segTree1.construct(segmentData1);
segTree2.construct(segmentData2);
CSVHandler c(filename, btree);
c.saveCSV();
c.exportBTreeToCPP();
fname=filename;
}
//CREATE DONE IN MEMORY. FLUSH THE DATA TO CSV FILES
//READ
if(crud_choice==2){
std::cout<<"Enter Name of Database: ";
string dbname;
std::cin>>dbname;
string filename=dbname+".csv";
CSVHandler check(filename,btree);
if(check.filexistchk(filename)==false){
std::cout<<"DB Doesn't Exists!! Use an existing db"<<endl;
continue;
}
if(btree.isTreeEmpty()==true){
CSVHandler c(filename, btree);
c.loadCSV();
}
if(btree.isTreeEmpty()==true){
std::cout<<"Empty hai"<<endl;
}
btree.printAllRecords();
std::cout<<"TESTING DONEEE"<<endl;
//FETCH THE CSV FILE OF THE DATABASE AND POPULATE THE BTree
//BTree btree(4);
vector<string> attributes(4); //PUT NAMES OF ATTEIBUTES IN THIS. IT'LL BE HELPFUL IN PRINTING
vector<int> segmentdata1;
vector<float>segmentdata2;
segmentdata1=btree.root->ages;
segmentdata2=btree.root->salaries;
//POPULATE THIS VECTOR ALSO
segTree1.construct(segmentdata1);
segTree2.construct(segmentdata2);
//NOW IMPLEMENT ALL THE QUERIES. ASK USER FOR QUERIES AND CHOICE TILL HE WANTS.
//Clean BTree and SegmentTrees. NO DATA FLUSHING REQUIRED AS DATA IS SAME.
std::cout<<"MENU:\n1.SELECT\t2.UPDATE\t3.DELETE"<<endl;
int choice;std::cin>>choice;
//SELECT
if(choice==1){
std::cout<<"1.*\t2. * WHERE AGE"<<"\t3. * WHERE SALARY"<<"\t4. AGGREGATE"<<endl;
int read_choice;std::cin>>read_choice;
if(read_choice==1){
btree.printAllRecords();
}
if(read_choice==2){
// Will do this
// For Name and age
//i will ask for the range of all records, age (upper limit, lower limit), greater than age condition, less than age condition
//call to B tree fucntion for name and age
std::cout << "For Name and Age columns what do you want your conditions to be:" << endl;
std::cout << "1. Greater than" << endl;
std::cout << "2. Less than" << endl;
std::cout << "3. Range" << endl;
std::cout << "4. Equal" << endl;
int read_choice2;
std::cin >> read_choice2;
// 0 represents attribute[0] i.e. Age
if (read_choice2 == 1) {
btree.printGreater(0);
}
if (read_choice2 == 2) {
btree.printLess(0);
}
if (read_choice2 == 3) {
btree.printRange(0);
}
if(read_choice2 == 4){
btree.printEqual(0);
}
}
if(read_choice==3){
// Will do this right
std::cout << "For Name and Salary columns what do you want your conditions to be:" << endl;
std::cout << "1. Greater than" << endl;
std::cout << "2. Less than" << endl;
std::cout << "3. Range" << endl;
std::cout << "4. Equal" << endl;
int read_choice2;
std::cin >> read_choice2;
// 1 represents attribute[1] i.e. Salary
if (read_choice2 == 1) {
btree.printGreater(1);
}
if (read_choice2 == 2) {
btree.printLess(1);
}
if (read_choice2 == 3) {
btree.printRange(1);
}
if(read_choice2 == 4){
btree.printEqual(1);
}
}
if(read_choice==4){
std::cout<<"1.SUM\t2.COUNT\t3.MAX\t4.MIN\n";
std::cout<<"1. AGE"<<"\t 2. SALARY"<<endl;
int aggregateType, attributeNo;std::cin>>aggregateType>>attributeNo;
//Sum
if(aggregateType==1 && attributeNo==1){
//if(attributeNo==1){
//segTree1->sumAll()
//int sum;
//sum = segTree1.sumWithConditions(segTree1.root, 0, segmentdata1.size()-1, 0, segmentdata1.size()-1);
std::cout<<"Sum of Age: "<<segTree1.rangeSum(0,segmentdata1.size()-1)<<endl;
}
else if(aggregateType==1 && attributeNo==2){
//segTree2->sumAll();
// float sum;
// sum = segTree2.sumSalary(segTree1.root, 0, segmentdata1.size()-1, 0, segmentdata1.size()-1);
std::cout<<"Sum of salary: "<<segTree2.rangeSum(0, segmentdata2.size()-1)<<endl;
}
// else{
// std::cout<<"invalid input1"<<endl;
// }
//Count
if(aggregateType==2 && attributeNo==1){
//if(attributeNo==1){
//segTree1->countAll()
// int count;
// count=segTree1.countAge(segTree1.root, 0, segmentdata1.size()-1, 0, segmentdata1.size()-1);
std::cout<<"Count of Age: "<<segTree1.rangeCount(0, segmentdata1.size()-1)<<endl;
}
else if(aggregateType==2 && attributeNo==2){
//segTree2->countAll();
// float count;
// count=segTree2.countAge(segTree2.root, 0, segmentdata2.size()-1, 0, segmentdata2.size()-1);
std::cout<<"Count of salary: "<<segTree2.rangeCount(0,segmentdata2.size()-1);
}
// else{
// std::cout<<"invalid input2"<<endl;
// }
//Max
if(aggregateType==3 && attributeNo==1){
//if(attributeNo==1){
//segTree1->minAll()
// int maxage;
// maxage=segTree1.maxAge(segTree1.root, 0, segmentdata1.size()-1, 0, segmentdata1.size()-1);
std::cout<<"Max Age: "<<segTree1.rangeMax(0, segmentdata1.size()-1);
}
else if(aggregateType==3 && attributeNo==2){
//segTree2->minAll();
// float maxsalary;
// maxsalary=segTree2.maxSalary(segTree2.root, 0, segmentdata2.size()-1, 0, segmentdata2.size()-1);
std::cout<<"Max Salary: "<<segTree2.rangeMax(0, segmentdata2.size()-1);
}
// else{
// std::cout<<"invalid input3"<<endl;
// }
//Min
if(aggregateType==4 && attributeNo==1){
//if(attributeNo==1){
//segTree1->maxAll()
// int minage;
// minage=segTree1.minAge(segTree1.root, 0, segmentdata1.size()-1, 0, segmentdata1.size()-1);
std::cout<<"Min Age: "<<segTree1.rangeMin(0, segmentdata1.size()-1);
}
else if(aggregateType==4 && attributeNo==2){
//segTree2->maxAll();
// float minsalary;
// minsalary=segTree2.minSalary(segTree2.root, 0, segmentdata2.size()-1, 0, segmentdata2.size()-1);
std::cout<<"Min Salary: "<<segTree2.rangeMin(0, segmentdata2.size()-1);
}
// else{
// std::cout<<"invalid input4"<<endl;
// }
}
// else{
// std::cout<<"invalid input5"<<endl;
// }
}
else if(choice==2){
//UPDATE OPERATION
std::cout <<"Do you want to perform 1:-Point update operation\t2:-New entry"<<endl;
int type_of_update;
std::cin >> type_of_update;
if(type_of_update==1){
//point update operation
cout << " Enter the attribute you want to change: \n 1:Age \t2:Salary" << endl;
int updateno;
std::cin >> updateno;
if(updateno==1){
std::cout << " Enter the key number of data you want to update: " << endl;
int key;
std::cin >> key;
std::cout << " Do you want to perform 1:- Increment operation\t2:-Decrement operation\t3:-Set operation "<<endl;
int choice_update;
std::cin >> choice_update;
if(choice_update==1){
//increment operation
std::cout << " Enter the add value: " << endl;
int add_value;
std::cin >> add_value;
//update_add_age
//call segment tree with parameters(add_value, key,(age or salary))
//segTree1.point_add_age(add_value, key);
btree.updateAddAge(key,add_value);
//std::cout<<"Updated Successfully"<<endl;
}
else if(choice_update==2){
//decrement operation
std::cout << " Enter the subtract value: " << endl;
int sub_value;
std::cin >> sub_value;
//update_sub_age
//call segment tree with parameters(sub_value, key,(age or salary))
btree.updateAddAge(key,(-1*sub_value));
std::cout<<"Updated Successfully"<<endl;
}
else if(choice_update==3){
//set operation
std::cout << " Enter the new value: " << endl;
int new_value;
std::cin >> new_value;
//update_set_age
//call segment tree with parameters(new_value, key,(age or salary))
btree.updateAge(key, new_value);
std::cout<<"Updated Successfully"<<endl;
}
else{
std::cout << "invalid input6"<<endl;
}
}
else if(updateno==2){
//salary update operation
std::cout << " Enter the key number of data you want to update: " << endl;
int key;
std::cin >> key;
std::cout << " Do you want to perform 1:- Increment operation\t2:-Decrement operation\t3:-Set operation "<<endl;
int choice_update;
std::cin >> choice_update;
if(choice_update==1){
//increment operation
std::cout << " Enter the add value: " << endl;
float add_value;
std::cin >> add_value;
//update_add_salary
//call segment tree with parameters(add_value, key,(age or salary))
btree.updateAddSalary(key, add_value);
//std::cout<<"Updated Successfully"<<endl;
}
else if(choice_update==2){
//decrement operation
std::cout << " Enter the subtract value: " << endl;
float sub_value;
std::cin >> sub_value;
//update_sub_salary
//call segment tree with parameters(sub_value, key,(age or salary))
//segTree2.point_sub_salary(sub_value, key);
btree.updateAddSalary(key, -1*sub_value);
//std::cout<<"Updated Successfully"<<endl;
}
else if(choice_update==3){
//set operation
std::cout << " Enter the new value: " << endl;
float new_value;
std::cin >> new_value;
//update_set_salary
btree.updateSalary(key, new_value);
//std::cout<<"Updated Successfully"<<endl;
//call segment tree with parameters(new_value, key,(age or salary))
}
else{
std::cout << "invalid input7"<<endl;
}
}
}
if(type_of_update==2){
//new key, name, age, sal
//update btree
//update segtree
cout<<"Enter ID NAME AGE SALARY"<<endl;
int key; string name; int attribute1; float attribute2;
std::cin>>key>>name>>attribute1>>attribute2;
btree.insert(key,name,attribute1, attribute2);
cout<<"Updated Successfully"<<endl;
}
// if(type_of_update==3){
// //range update operation
// cout << " Enter the attribute you want to change: \n 1:Age \t2:Salary" << endl;
// int updateno;
// std::cin >> updateno;
// if(updateno==1){
// //range update operation age
// std::cout << " Enter the range of key numbers of data you want to update (low high): " << endl;
// int low_key,high_key;
// std::cin >> low_key >> high_key;
// std::cout << " Do you want to perform 1:- Increment operation\t2:-Decrement operation\t3:-Set operation "<<endl;
// int choice_update;
// std::cin >> choice_update;
// if(choice_update==1){
// //increment operation
// std::cout << " Enter the add value: " << endl;
// int add_value;
// std::cin >> add_value;
// //range_update_add_age
// //call segment tree with parameters(add_value, key range(low,high),(age or salary))
// segTree1.range_add_age(segTree1.root, low_key, high_key, 0, segmentdata1.size()-1, add_value);
// std::cout<<"Updated Successfully"<<endl;
// }
// else if(choice_update==2){
// //decrement operation
// std::cout << " Enter the subtract value: " << endl;
// int sub_value;
// std::cin >> sub_value;
// //range_update_sub_age
// //call segment tree with parameters(sub_value, key range(low,high),(age or salary))
// segTree1.range_sub_age(segTree1.root, low_key, high_key, 0, segmentdata1.size()-1, sub_value);
// std::cout<<"Updated Successfully"<<endl;
// }
// else if(choice_update==3){
// //set operation
// std::cout << " Enter the new value: " << endl;
// int new_value;
// std::cin >> new_value;
// //range_update_set_age
// //call segment tree with parameters(new_value, key range(low,high),(age or salary))
// segTree1.range_set_age(segTree1.root, low_key, high_key, 0, segmentdata1.size()-1, new_value);
// std::cout<<"Updated Successfully"<<endl;
// }
// else{
// std::cout << "invalid input"<<endl;
// }
// }
// else if(updateno==2){
// //range update operation salary
// std::cout << " Enter the range of key number of data you want to update (low high): " << endl;
// int low_key,high_key;
// std::cin >> low_key >> high_key;
// std::cout << " Do you want to perform 1:- Increment operation\t2:-Decrement operation\t3:-Set operation "<<endl;
// int choice_update;
// std::cin >> choice_update;
// if(choice_update==1){
// //increment operation
// std::cout << " Enter the add value: " << endl;
// float add_value;
// std::cin >> add_value;
// //range_update_add_salary
// //call segment tree with parameters(add_value, key range(low,high),(age or salary))
// segTree2.range_add_salary(segTree2.root, low_key, high_key, 0, segmentdata2.size()-1, add_value);
// std::cout<<"Updated Successfully"<<endl;
// }
// else if(choice_update==2){
// //decrement operation
// std::cout << " Enter the subtract value: " << endl;
// float sub_value;
// std::cin >> sub_value;
// //range_update_sub_salary
// //call segment tree with parameters(sub_value, key range(low,high),(age or salary))
// segTree2.range_sub_salary(segTree2.root, low_key, high_key, 0, segmentdata2.size()-1, sub_value);
// std::cout<<"Updated Successfully"<<endl;
// }
// else if(choice_update==3){
// //set operation
// std::cout << " Enter the new value: " << endl;
// float new_value;
// std::cin >> new_value;
// //range_update_set_salary
// //call segment tree with parameters(new_value, key range(low,high),(age or salary))
// segTree2.range_set_salary(segTree2.root, low_key, high_key, 0, segmentdata2.size()-1, new_value);
// std::cout<<"Updated Successfully"<<endl;
// }
// else{
// std::std::cout << "invalid input"<<endl;
// }
}
else if(choice==3){
std::cout<<"ENTER KEY NUMBER OF DATA YOU WANT TO DELETE: ";
int key; std::cin>>key;
BTreeNode* node=btree.search(key);
if(!node){
std::cout<<"NO SUCH NODE PRESENT"<<endl;
return 0;
}
node->remove(key);
std::cout<<"Deleted Successfully"<<endl;
}
fname=filename;
}
cout<<"Want to query more?Enter 1 to continue, 0 to exit"<<endl;
int contChoice=1;
std::cout<<"Enter your choice: ";
std::cin>>contChoice;
if(contChoice==0){
CSVHandler csvflush(fname, btree);
csvflush.saveCSV();
csvflush.exportBTreeToCPP();
break;
}
}
return 0;
}
// while (true)
// {
// //cout<<"1.NAME \n2."<<attributes[2]<<endl;
// cout<<"Enter attribute you want to change: ";
// int updateno; cin>>updateno;
// cout<<"ENTER KEY NUMBER OF DATA YOU WANT TO UPDATE: ";
// int key; cin>>key;
// cout<<"ENTER THE NEW VALUE: ";
// int value; cin>>value;
// BTreeNode* node=btree.search(key);
// if(!node){
// cout<<"NO SUCH NODE PRESENT"<<endl;
// return 0;
// }
// node->ages[key]=value;
// cout<<"DO YOU WANT TO UPDATE MORE? 1. YES 0. NO: ";
// int choice; cin>>choice;
// if(choice==0){
// break;
// }
// }
//if(crud_choice==4){
// string dbname;
// cin>>dbname;
// if(!dbNames.search(dbname)){
// cout<<"No such Database exists"<<endl;
// return 0;
// }
// BTree btree(4);
// vector<int> segmentdata;
// vector<string> attribute; //PUT NAMES OF ATTRIBUTES IN THIS. IT'LL BE HELPFUL IN PRINTING
// //POPULATE THIS VECTOR ALSO
// SegmentTree* segTree=new SegmentTree(segmentdata);
// while (true)
// {
// cout<<"1.NAME \n2."<<attribute[2]<<endl;
// cout<<"ENTER KEY NUMBER OF DATA YOU WANT TO DELETE: ";
// int key; cin>>key;
// BTreeNode* node=btree.search(key);
// if(!node){
// cout<<"NO SUCH NODE PRESENT"<<endl;
// return 0;
// }
// node->remove(key);
// cout<<"DO YOU WANT TO DELETE MORE? 1. YES 0. NO: ";
// int choice; cin>>choice;
// if(choice==0){
// break;
// }
// }
// }
//UPDATE
//if(crud_choice==3){
// string dbname;
// cin>>dbname;
// if(!dbNames.search(dbname)){
// cout<<"No such Database exists"<<endl;
// return 0;
// }
//FETCH THE CSV FILE OF THE DATABASE AND POPULATE THE BTree
//QUERIES