forked from ob-algdati-ws17/blatt-7-aufgabe-1-team123
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.cpp
108 lines (100 loc) · 3.04 KB
/
Main.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
#include <iostream>
#include <vector>
#include <fstream>
#include <sstream>
#include "AVLTree.h"
using namespace std;
template<typename T>
ostream &operator<<(ostream &out, const vector<T> &v);
int main(int argc, const char *argv[]) {
auto treeToPng = [](AVLTree *tree) {
ofstream myfile;
myfile.open("tree.dot");
myfile << *tree;
myfile.close();
system("dot -Tpng tree.dot -o tree.png");
};
int val;
AVLTree *tree = new AVLTree();
for (int i = 1; i < argc; i++) {
std::istringstream iss(argv[i]);
iss >> val;
tree->insert(val);
}
char action = '\0';
treeToPng(tree);
while (action != 'q') {
switch (action) {
case '\0': // dummy
break;
case 's':
cout << "Search: ";
cin >> val;
cout << (tree->search(val) ? "" : "not ") << "found" << endl;
break;
case 'd':
cout << "Delete: ";
cin >> val;
tree->remove(val);
break;
case 'i':
cout << "Insert: ";
cin >> val;
tree->insert(val);
break;
case 't':
char order;
cout << "p(o)storder, (i)norder, p(r)eorder? ";
cin >> order;
vector<int> *vec;
switch (order) {
case 'o':
vec = tree->postorder();
if (vec == nullptr)
cout << "emoty tree" << endl;
else
cout << *vec << endl;
break;
case 'i':
vec = tree->inorder();
if (vec == nullptr)
cout << "emoty tree" << endl;
else
cout << *vec << endl;
break;
case 'r':
vec = tree->preorder();
if (vec == nullptr)
cout << "emoty tree" << endl;
else
cout << *vec << endl;
break;
default:
cout << "Order unknown" << endl;
break;
}
break;
default:
cout << "Unknown command: " << action << endl;
break;
}
if (action == 'i' || action == 'd')
treeToPng(tree);
cout << "(s)earch, (d)elete, (i)nsert, (t)raverse, (q)uit: ";
cin >> action;
}
cout << "Cheerio!" << endl;
return 0;
}
template<typename T>
ostream &operator<<(ostream &out, const vector<T> &v) {
out << "[";
size_t last = v.size() - 1;
for (size_t i = 0; i < v.size(); ++i) {
out << v[i];
if (i != last)
out << ", ";
}
out << "]";
return out;
}