-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecisionTreeMain.cpp
More file actions
172 lines (131 loc) · 3.88 KB
/
decisionTreeMain.cpp
File metadata and controls
172 lines (131 loc) · 3.88 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
#include "decisionTree.h"
using namespace d_tree;
int main(){
const string menu = "-------------------------------------------------------------------\n\t\t\t\tMENU\n-------------------------------------------------------------------\n1. Read decision tree from file\n2. Insert a labeled node childNode, attached to a node fatherName using an edge edgeLabel\n3. Delete a node\n4. Edit a node\n5. Print current decision tree\n6. Print variables in current tree\n7. Start prediction by inserting values one at a time\n8. Start prediction by inserting all values from start\n0. Exit\n\nChoose ---> ";
// Defining interface variables (ch = menu | l1,l2,la = fatherLabel,childLabel,edgeLabel | ld = delete | le1,le2 = edit | file_name = name of .txt file used | lp = preditionLabel)
choice ch;
Label l1, l2, la, ld, le1, le2, lp;
string file_name;
// Shows Menu
cout << endl << menu;
cin >> ch;
// Initialize empty tree
Tree tree = d_tree::createEmpty();
while(ch != 0){
switch(ch){
case 1:
// Reading tree from file
cout << "\nInsert file name (.txt file requested): ";
cin >> file_name;
tree = readFromFile(file_name);
if(!isEmpty(tree))
cout << "\nSuccess\n";
else
cout << "\nFailure\n";
cout << "\n";
break;
case 2:
// Insert a labeled node called childNode attached to a father node called fatherNode, by using a labeled edge called edgeLabel
cout << "\nInsert fatherLabel name (if tree is empty, insert $#$#$): ";
cin >> l1;
cout << "\nInsert childLabel to be attached to " << l1 << ": ";
cin >> l2;
if(!isEmpty(tree))
{
cout << "\nInsert label to be assigned to the edgeInserisci l'etichetta da assegnare all'arco: ";
cin >> la;
}
else
la = "----";
if(!cin)
{
cout << "Failure in inserting data\n";
break;
}
removeBlanksAndLower(la);
removeBlanksAndLower(l1);
removeBlanksAndLower(l2);
if(addElem(l1, l2, la, tree) == FAIL)
cout << "\nFailure! (inserting error)\n";
else
cout << "\nSuccess!\n";
cout << "\n";
break;
case 3:
// Deleting a node
cout << "\nInsert name of node to be deleted: ";
cin >> ld;
if(!cin)
{
cout << "Error in inserting data\n";
break;
}
removeBlanksAndLower(ld);
if(deleteElem(ld, tree) == FAIL)
cout << "\nFailure! (deleting error)\n";
else
cout << "\nSuccess!\n";
cout << "\n";
break;
case 4:
// Editing node
cout << "\nInsert node name to be edited: ";
cin >> le1;
cout << "\nInsert new label to be assigned: ";
cin >> le2;
if(!cin)
{
cout << "Error in inserting data\n";
break;
}
removeBlanksAndLower(le1);
removeBlanksAndLower(le2);
if(editElem(le1, le2, tree) == FAIL)
cout << "\nFailure! (editing error)\n";
else
cout << "\nSuccess!\n";
cout << "\n";
break;
case 5:
// Printing decision tree
cout << "\n";
printTree(tree);
cout << "\n";
break;
case 6:
// Printing variables only
printVariables(tree);
cout << "\n";
break;
case 7:
// Predict by inserting one value at a time
lp = compareMono(tree);
if(lp == emptyLabel)
cout << "\nFailure! (There's no valid path)\n";
else
{
lp = normalizeValue(lp);
cout << "\nFinal prediction is: " << lp << "\n";
}
cout << "\n";
break;
case 8:
// Predict by inserting all values
lp = compareMultiMain(tree);
if(lp == emptyLabel)
cout << "\nFailure! (There's no valid path)\n";
else
{
lp = normalizeValue(lp);
cout << "\nFinal prediction is: " << lp << "\n";
}
cout << "\n";
break;
default:
cout << "\nInvalid character!\n\n";
}
cout << menu;
cin >> ch;
}
return 0;
}