-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmenu.cpp
654 lines (557 loc) · 16.2 KB
/
menu.cpp
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
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
#include "menu.h"
using namespace std;
int ask(const char* msgs[], int n)
{
int choice = -1;
string err = "";
while (choice < 0 || choice >= n) {
cout << err << endl;
err = "Wrong input";
for (int i = 0; i < n; i++) cout << msgs[i] << endl;
cout << "Enter option: ";
cin >> choice;
}
if(!choice) choice -= 20;
return choice;
}
void clean(BinarySearchTree<int>* bst1, BinarySearchTree<double>* bst2, BinarySearchTree<Complex>* bst3, BinarySearchTree<string>* bst4, BinarySearchTree<char>* bst5, BinarySearchTree<Student>* bst6)
{
if(bst1) { delete bst1; }
if(bst2) { delete bst2; }
if(bst3) { delete bst3; }
if(bst4) { delete bst4; }
if(bst5) { delete bst5; }
if(bst6) { delete bst6; }
}
void menu()
{
const char* MAIN[] = {
"0. Quit",
"1. Create BinarySearchTree",
"2. BinarySearchTree operations",
"3. Run tests",
"4. Run 100",
"5. Run 10^5",
"6. Run 10^7"
};
int main_size = 7;
const char* CREATE[] = {
"0. Quit",
"1. Integers",
"2. Floats",
"3. Complex numbers",
"4. Strings",
"5. Symbols",
"6. Students"
};
int create_size = 7;
const char* TREE_OPERATIONS[] = {
"0. Quit",
"1. Insert",
"2. Remove",
"3. Search",
"4. Traverse",
"5. Get substree",
"6. Merge"
};
int tree_operations_size = 7;
BinarySearchTree<int>* bst1 = 0;
BinarySearchTree<double>* bst2 = 0;
BinarySearchTree<Complex>* bst3 = 0;
BinarySearchTree<string>* bst4 = 0;
BinarySearchTree<char>* bst5 = 0;
BinarySearchTree<Student>* bst6 = 0;
auto get_len = []()
{
int len;
cout << "Enter number of elements: \n";
cin >> len;
while(len <= 0) {
cout << "Inappropriate number of elements.\n";
cout << "Enter number of elements: \n";
cin >> len;
}
return len;
};
auto get_int_array = [](int len)
{
cout << "Enter ints: \n";
int* a = new int[len];
for(int i = 0 ; i < len; i++) cin >> a[i];
return a;
};
auto get_double_array = [](int len)
{
cout << "Enter elements: \n";
double* ar = new double[len];
for(int i = 0 ; i < len; i++) cin >> ar[i];
return ar;
};
auto get_Complex_array = [](int len)
{
cout << "Enter elements: \n";
Complex* arr = new Complex[len];
double Re;
double Im;
for(int i = 0 ; i < len; i++) {
cout << "Enter Re: ";
cin >> Re;
cout << "Enter Im: ";
cin >> Im;
arr[i] = Complex(Re, Im);
}
return arr;
};
auto get_string_array = [](int len)
{
cout << "Enter strings: \n";
string* array = new string[len];
for(int i = 0 ; i < len; i++) cin >> array[i];
return array;
};
auto get_char_array = [](int len)
{
cout << "Enter chars: \n";
char* array = new char[len];
for(int i = 0 ; i < len; i++) cin >> array[i];
return array;
};
auto get_Student_array = [](int len)
{
cout << "Enter Students: \n";
Student* array = new Student[len];
int id;
string firstName;
string middleName;
string lastName;
for(int i = 0 ; i < len; i++) {
cout << "ID: ";
cin >> id;
cout << "First name: ";
cin >> firstName;
cout << "Middle name: ";
cin >> middleName;
cout << "Last name: ";
cin >> lastName;
array[i] = Student(id, firstName, middleName, lastName);
}
return array;
};
auto big_Tree_Test = [](int len, BinarySearchTree<int>* bst1)
{
bst1 = new BinarySearchTree<int>();
clock_t t1 = clock();
int tmp;
for (int i = 0; i < len; i++) {
tmp = rand();
bst1->Insert(tmp);
}
clock_t t2 = clock();
cout << "Insertion took: " << difftime(t2, t1) / CLOCKS_PER_SEC << '\n';
cout << "Merge will took nearly double the insertion time.\n";
t1 = clock();
bst1->Search(rand());
t2 = clock();
cout << "Search took: " << difftime(t2, t1) / CLOCKS_PER_SEC << '\n';
t1 = clock();
bst1->ToString("RNL");
t2 = clock();
cout << "Traverse took: " << difftime(t2, t1) / CLOCKS_PER_SEC << '\n';
t1 = clock();
bst1->GetSubtree(tmp);
t2 = clock();
cout << "GetSubtree took: " << difftime(t2, t1) / CLOCKS_PER_SEC << '\n';
};
int option = 0;
while(option >= 0) {
switch(option) {
case 0:
option = ask(MAIN, main_size);
break;
case 1:
clean(bst1, bst2, bst3, bst4, bst5, bst6);
bst1 = 0;
bst2 = 0;
bst3 = 0;
bst4 = 0;
bst5 = 0;
bst6 = 0;
option = ask(CREATE, create_size) + main_size - 1;
break;
case 2:
option = ask(TREE_OPERATIONS, tree_operations_size) + main_size + create_size - 2;
break;
case 3: //Run tests
unit_tests_ArraySequence::Run();
unit_tests_DynamicArray::Run();
unit_tests_LinkedList::Run();
unit_tests_ListSequence::Run();
unit_tests_BinaryHeap::Run();
unit_tests_PriorityQueue::Run();
unit_tests_BinarySearchTree::Run();
option = 0;
break;
case 4:
{
int len = 100;
clean(bst1, bst2, bst3, bst4, bst5, bst6);
bst1 = 0;
bst2 = 0;
bst3 = 0;
bst4 = 0;
bst5 = 0;
bst6 = 0;
bst1 = new BinarySearchTree<int>();
big_Tree_Test(len, bst1);
option = 0;
}
break;
case 5:
{
int len = 100000;
clean(bst1, bst2, bst3, bst4, bst5, bst6);
bst1 = 0;
bst2 = 0;
bst3 = 0;
bst4 = 0;
bst5 = 0;
bst6 = 0;
bst1 = new BinarySearchTree<int>();
big_Tree_Test(len, bst1);
option = 0;
}
break;
case 6:
{
int len = 10000000;
clean(bst1, bst2, bst3, bst4, bst5, bst6);
bst1 = 0;
bst2 = 0;
bst3 = 0;
bst4 = 0;
bst5 = 0;
bst6 = 0;
bst1 = new BinarySearchTree<int>();
big_Tree_Test(len, bst1);
option = 0;
}
break;
case 7: //Create int bst
{
int len = get_len();
int* a = get_int_array(len);
Stack<int>* stack1 = new Stack<int>(a, len, 1);
bst1 = new BinarySearchTree<int>(stack1);
delete stack1;
delete [] a;
option = 0;
}
break;
case 8: //Create double bst
{
int len = get_len();
double* ar = get_double_array(len);
Stack<double>* stack2 = new Stack<double>(ar, len, 1);
bst2 = new BinarySearchTree<double>(stack2);
delete stack2;
delete [] ar;
option = 0;
}
break;
case 9: //Create Complex bst
{
int len = get_len();
Complex* arr = get_Complex_array(len);
Stack<Complex>* stack3 = new Stack<Complex>(arr, len, 1);
bst3 = new BinarySearchTree<Complex>(stack3);
delete stack3;
delete [] arr;
option = 0;
}
break;
case 10: //Create string bst
{
int len = get_len();
string* array = get_string_array(len);
Stack<string>* stack4 = new Stack<string>(array, len, 1);
bst4 = new BinarySearchTree<string>(stack4);
delete stack4;
delete [] array;
option = 0;
}
break;
case 11: //Create char bst
{
int len = get_len();
char* array = get_char_array(len);
Stack<char>* stack5 = new Stack<char>(array, len, 1);
bst5 = new BinarySearchTree<char>(stack5);
delete stack5;
delete [] array;
option = 0;
}
break;
case 12: //Create Student bst
{
int len = get_len();
Student* array = get_Student_array(len);
Stack<Student>* stack6 = new Stack<Student>(array, len, 1);
bst6 = new BinarySearchTree<Student>(stack6);
delete stack6;
delete [] array;
option = 0;
}
break;
case 13: //Insert
{
if(bst1) {
int n;
cout << "Enter int: \n";
cin >> n;
bst1->Insert(n);
} else if(bst2) {
double f;
cout << "Enter flaot: \n";
cin >> f;
bst2->Insert(f);
} else if(bst3) {
double Re;
double Im;
cout << "Enter Re: ";
cin >> Re;
cout << "Enter Im: ";
cin >> Im;
bst3->Insert(Complex(Re, Im));
} else if(bst4) {
string str;
cout << "Enter string: \n";
cin >> str;
bst4->Insert(str);
} else if(bst5) {
char c;
cout << "Enter char: \n";
cin >> c;
bst5->Insert(c);
} else if(bst6) {
int id;
string firstName;
string middleName;
string lastName;
cout << "ID: ";
cin >> id;
cout << "First name: ";
cin >> firstName;
cout << "Middle name: ";
cin >> middleName;
cout << "Last name: ";
cin >> lastName;
bst6->Insert(Student(id, firstName, middleName, lastName));
} else {
std::cout << "No stack created\n";
}
option = 0;
}
break;
case 14: //Remove
{
if(bst1) {
int n;
cout << "Enter int: \n";
cin >> n;
if(bst1->IsEmpty()) { cout << "BinarySearchTree is empty\n"; } else { bst1->Remove(n); }
} else if(bst2) {
double f;
cout << "Enter flaot: \n";
cin >> f;
if(bst2->IsEmpty()) { cout << "BinarySearchTree is empty\n"; } else { bst2->Remove(f); }
} else if(bst3) {
double Re;
double Im;
cout << "Enter Re: ";
cin >> Re;
cout << "Enter Im: ";
cin >> Im;
if(bst3->IsEmpty()) { cout << "BinarySearchTree is empty\n"; } else { bst3->Remove(Complex(Re, Im)); }
} else if(bst4) {
string str;
cout << "Enter string: \n";
cin >> str;
if(bst4->IsEmpty()) { cout << "BinarySearchTree is empty\n"; } else { bst4->Remove(str); }
} else if(bst5) {
char c;
cout << "Enter char: \n";
cin >> c;
if(bst5->IsEmpty()) { cout << "BinarySearchTree is empty\n"; } else { bst5->Remove(c); }
} else if(bst6) {
int id;
string firstName;
string middleName;
string lastName;
cout << "ID: ";
cin >> id;
cout << "First name: ";
cin >> firstName;
cout << "Middle name: ";
cin >> middleName;
cout << "Last name: ";
cin >> lastName;
if(bst6->IsEmpty()) { cout << "BinarySearchTree is empty\n"; } else { bst6->Remove(Student(id, firstName, middleName, lastName)); }
} else {
std::cout << "No bst created\n";
}
option = 0;
}
break;
case 15: //Search
{
if(bst1) {
int n;
cout << "Enter int: \n";
cin >> n;
if(bst1->IsEmpty()) { cout << "BinarySearchTree is empty\n"; } else { cout << "Result: " << bst1->Search(n) << endl; }
} else if(bst2) {
double f;
cout << "Enter flaot: \n";
cin >> f;
if(bst2->IsEmpty()) { cout << "BinarySearchTree is empty\n"; } else { cout << "Result: " << bst2->Search(f) << endl; }
} else if(bst3) {
double Re;
double Im;
cout << "Enter Re: ";
cin >> Re;
cout << "Enter Im: ";
cin >> Im;
if(bst3->IsEmpty()) { cout << "BinarySearchTree is empty\n"; } else { cout << "Result: " << bst3->Search(Complex(Re, Im)) << endl; }
} else if(bst4) {
string str;
cout << "Enter string: \n";
cin >> str;
if(bst4->IsEmpty()) { cout << "BinarySearchTree is empty\n"; } else { cout << "Result: " << bst4->Search(str) << endl; }
} else if(bst5) {
char c;
cout << "Enter char: \n";
cin >> c;
if(bst5->IsEmpty()) { cout << "BinarySearchTree is empty\n"; } else { cout << "Result: " << bst5->Search(c) << endl; }
} else if(bst6) {
int id;
string firstName;
string middleName;
string lastName;
cout << "ID: ";
cin >> id;
cout << "First name: ";
cin >> firstName;
cout << "Middle name: ";
cin >> middleName;
cout << "Last name: ";
cin >> lastName;
if(bst6->IsEmpty()) { cout << "BinarySearchTree is empty\n"; } else { cout << "Result: " << bst6->Search(Student(id, firstName, middleName, lastName)) << endl; }
} else {
std::cout << "No bst created\n";
}
option = 0;
}
break;
case 16: //Traverse
{
if(bst1) {
cout << bst1->ToString() << endl;
} else if(bst2) {
cout << bst2->ToString() << endl;
} else if(bst3) {
// cout << bst3->ToString() << endl;
} else if(bst4) {
// cout << bst4->ToString() << endl;
} else if(bst5) {
cout << bst5->ToString() << endl;
} else if(bst6) {
// cout << bst6->ToString() << endl;
} else {
std::cout << "No bst created\n";
}
option = 0;
}
break;
case 17: //Get subtree
{
if(bst1) {
cout << bst1->ToString() << endl;
} else if(bst2) {
cout << bst2->ToString() << endl;
} else if(bst3) {
// cout << bst3->ToString() << endl;
} else if(bst4) {
// cout << bst4->ToString() << endl;
} else if(bst5) {
cout << bst5->ToString() << endl;
} else if(bst6) {
// cout << bst6->ToString() << endl;
} else {
std::cout << "No bst created\n";
}
option = 0;
}
break;
case 18: //Merge
{
int len = get_len();
if(bst1) {
int* a = get_int_array(len);
Stack<int>* stack1 = new Stack<int>(a, len, 1);
BinarySearchTree<int>* bst = new BinarySearchTree<int>(stack1);
bst1->Merge(bst);
delete stack1;
delete bst;
delete [] a;
} else if(bst2) {
double* ar = get_double_array(len);
Stack<double>* stack2 = new Stack<double>(ar, len, 1);
BinarySearchTree<double>* bst = new BinarySearchTree<double>(stack2);
bst2->Merge(bst);
delete stack2;
delete bst;
delete [] ar;
} else if(bst3) {
Complex* arr = get_Complex_array(len);
Stack<Complex>* stack3 = new Stack<Complex>(arr, len, 1);
BinarySearchTree<Complex>* bst = new BinarySearchTree<Complex>(stack3);
bst3->Merge(bst);
delete stack3;
delete bst;
delete [] arr;
} else if(bst4) {
string* array = get_string_array(len);
Stack<string>* stack4 = new Stack<string>(array, len, 1);
BinarySearchTree<string>* bst = new BinarySearchTree<string>(stack4);
bst4->Merge(bst);
delete stack4;
delete bst;
delete [] array;
} else if(bst5) {
char* array = get_char_array(len);
Stack<char>* stack5 = new Stack<char>(array, len, 1);
BinarySearchTree<char>* bst = new BinarySearchTree<char>(stack5);
bst5->Merge(bst);
delete stack5;
delete bst;
delete [] array;
} else if(bst6) {
Student* array = get_Student_array(len);
Stack<Student>* stack6 = new Stack<Student>(array, len, 1);
BinarySearchTree<Student>* bst = new BinarySearchTree<Student>(stack6);
bst6->Merge(bst);
delete stack6;
delete bst;
delete [] array;
} else {
std::cout << "No bst created\n";
}
option = 0;
}
break;
default:
option = 0;
}
}
clean(bst1, bst2, bst3, bst4, bst5, bst6);
}