-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDecisionTree1.java
More file actions
224 lines (170 loc) · 6.21 KB
/
DecisionTree1.java
File metadata and controls
224 lines (170 loc) · 6.21 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
import java.io.*;
class DecisionTree {
private class BinTree {
/* FIELDS */
private int nodeID;
private String questOrAns = null;
private BinTree yesBranch = null;
private BinTree noBranch = null;
/* CONSTRUCTOR */
public BinTree(int newNodeID, String newQuestAns) {
nodeID = newNodeID;
questOrAns = newQuestAns;
}
}
/* OTHER FIELDS */
static BufferedReader keyboardInput = new BufferedReader(new InputStreamReader(System.in));
BinTree rootNode = null;
/* Default Constructor */
public DecisionTree() {
}
/* CREATE ROOT NODE */
public void createRoot(int newNodeID, String newQuestAns) {
rootNode = new BinTree(newNodeID,newQuestAns);
System.out.println("Created root node " + newNodeID);
}
/* ADD YES NODE */
public void addYesNode(int existingNodeID, int newNodeID, String newQuestAns) {
// If no root node do nothing
if (rootNode == null) {
System.out.println("ERROR: No root node!");
return;
}
// Search tree
if (searchTreeAndAddYesNode(rootNode,existingNodeID,newNodeID,newQuestAns)) {
System.out.println("Added node " + newNodeID +
" onto \"yes\" branch of node " + existingNodeID);
}
else System.out.println("Node " + existingNodeID + " not found");
}
/* SEARCH TREE AND ADD YES NODE */
private boolean searchTreeAndAddYesNode(BinTree currentNode,
int existingNodeID, int newNodeID, String newQuestAns) {
if (currentNode.nodeID == existingNodeID) {
// Found node
if (currentNode.yesBranch == null) currentNode.yesBranch = new
BinTree(newNodeID,newQuestAns);
else {
System.out.println("WARNING: Overwriting previous node " +
"(id = " + currentNode.yesBranch.nodeID +
") linked to yes branch of node " +
existingNodeID);
currentNode.yesBranch = new BinTree(newNodeID,newQuestAns);
}
return(true);
}
else {
// Try yes branch if it exists
if (currentNode.yesBranch != null) {
if (searchTreeAndAddYesNode(currentNode.yesBranch,
existingNodeID,newNodeID,newQuestAns)) {
return(true);
}
else {
// Try no branch if it exists
if (currentNode.noBranch != null) {
return(searchTreeAndAddYesNode(currentNode.noBranch,
existingNodeID,newNodeID,newQuestAns));
}
else return(false); // Not found here
}
}
return(false); // Not found here
}
}
/* ADD NO NODE */
public void addNoNode(int existingNodeID, int newNodeID, String newQuestAns) {
// If no root node do nothing
if (rootNode == null) {
System.out.println("ERROR: No root node!");
return;
}
// Search tree
if (searchTreeAndAddNoNode(rootNode,existingNodeID,newNodeID,newQuestAns)) {
System.out.println("Added node " + newNodeID +
" onto \"no\" branch of node " + existingNodeID);
}
else System.out.println("Node " + existingNodeID + " not found");
}
/* SEARCH TREE AND ADD NO NODE */
private boolean searchTreeAndAddNoNode(BinTree currentNode,
int existingNodeID, int newNodeID, String newQuestAns) {
if (currentNode.nodeID == existingNodeID) {
// Found node
if (currentNode.noBranch == null) currentNode.noBranch = new
BinTree(newNodeID,newQuestAns);
else {
System.out.println("WARNING: Overwriting previous node " +
"(id = " + currentNode.noBranch.nodeID +
") linked to yes branch of node " +
existingNodeID);
currentNode.noBranch = new BinTree(newNodeID,newQuestAns);
}
return(true);
}
else {
// Try yes branch if it exists
if (currentNode.yesBranch != null) {
if (searchTreeAndAddNoNode(currentNode.yesBranch,
existingNodeID,newNodeID,newQuestAns)) {
return(true);
}
else {
// Try no branch if it exists
if (currentNode.noBranch != null) {
return(searchTreeAndAddNoNode(currentNode.noBranch,
existingNodeID,newNodeID,newQuestAns));
}
else return(false); // Not found here
}
}
else return(false); // Not found here
}
}
public void queryBinTree() throws IOException {
queryBinTree(rootNode);
}
private void queryBinTree(BinTree currentNode) throws IOException {
// Test for leaf node (answer) and missing branches
if (currentNode.yesBranch==null) {
if (currentNode.noBranch==null) System.out.println(currentNode.questOrAns);
else System.out.println("Error: Missing \"Yes\" branch at \"" +
currentNode.questOrAns + "\" question");
return;
}
if (currentNode.noBranch==null) {
System.out.println("Error: Missing \"No\" branch at \"" +
currentNode.questOrAns + "\" question");
return;
}
// Question
askQuestion(currentNode);
}
private void askQuestion(BinTree currentNode) throws IOException {
System.out.println(currentNode.questOrAns + " (enter \"Yes\" or \"No\")");
String answer = keyboardInput.readLine();
if (answer.equals("Yes")) queryBinTree(currentNode.yesBranch);
else {
if (answer.equals("No")) queryBinTree(currentNode.noBranch);
else {
System.out.println("ERROR: Must answer \"Yes\" or \"No\"");
askQuestion(currentNode);
}
}
}
/* OUTPUT BIN TREE */
public void outputBinTree() {
outputBinTree("1",rootNode);
}
private void outputBinTree(String tag, BinTree currentNode) {
// Check for empty node
if (currentNode == null) return;
// Output
System.out.println("[" + tag + "] nodeID = " + currentNode.nodeID +
", question/answer = " + currentNode.questOrAns);
// Go down yes branch
outputBinTree(tag + ".1",currentNode.yesBranch);
// Go down no branch
outputBinTree(tag + ".2",currentNode.noBranch);
}
}