forked from itrobertson/DecisionTree
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreeNode.java
More file actions
145 lines (126 loc) · 4.67 KB
/
TreeNode.java
File metadata and controls
145 lines (126 loc) · 4.67 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
package decisiontree;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;
public class TreeNode<T>
{
//edgeValue is Edge descriptor i.e. Yes, No, Full, Some
private T edgeValue;
//Outcome has a value when node contains an answer (Yes or No).
private T outcome;
//attributeName is the name of each attribute in the data sample.
private String attributeName;
private PrintWriter writer;
private List<TreeNode> branches = new ArrayList<>();
private TreeNode branch;
public TreeNode(String attributeName) {
this.attributeName=attributeName;
}
public TreeNode(String leaf, T outcome){
//this is a leaf Node
attributeName=leaf;
this.outcome=outcome;
}
public TreeNode findValue(String attributeName,T attributeValue){
for(TreeNode node : branches){
//System.out.println("NodeName: "+this.getAttributeName()+" ExampleName: "+attributeName+" NodeValue: "+node.getValue()+" ExampleValue: "+attributeValue+" Node: "+node.getOutcome());
if(this.attributeName.equals(attributeName) && node.getValue().equals(attributeValue)){
//System.out.println("NodeName: "+this.getAttributeName()+" ExampleName: "+attributeName+" NodeValue: "+node.getValue()+" ExampleValue: "+attributeValue);
return node;
}
}
return null;//Value could not be found so return null
// throw new RuntimeException("Could not find value: " + attributeValue);
}
public TreeNode predict(Example e){
String attributeName = this.getAttributeName();
String eAttribute = e.getAttribute(attributeName);
T attributeValue = (T) e.getValue(attributeName);
// System.out.println("Node: "+attributeName+" Value: " +" Example: "+eAttribute+" Value:");
TreeNode tn = findValue(eAttribute,attributeValue);
if(tn==null){
return null;
}
//if outcome is not null it is a leaf node
if(tn.getOutcome()!=null){
return tn;
}else{//outcome is null and tn is not null so continue down tree deeper
return tn.predict(e);
}
// throw new RuntimeException("No match for value: " + attributeValue);
}
public boolean containsValue(T attributeValue){
for(TreeNode node : branches){
//System.out.println("treenode: "+node.getValue()+" new: "+attributeValue);
return node.getValue().equals(attributeValue);
}
return false;
}
public TreeNode getNext(int index){
branch=getBranches().get(index);
return branch != null ? branch : getNext(index++);
}
/*public TreeNode(T newValue, DecisionTree<T> branch)
{
value = newValue;
this.branch=branch;
branches.add(branch);
}*/
public TreeNode addBranch(T branchValue, TreeNode<T> child){
branches.add(child);
int childIndex = branches.indexOf(child);
branches.get(childIndex).setValue(branchValue);
return this;
}
public void setValue(T newValue){
edgeValue=newValue;
}
//public DecisionTree getBranch(){
// return branch;
// }
public String getAttributeName(){
return attributeName;
}
public T getValue(){
return edgeValue;
}
public T getOutcome(){
return outcome;
}
public List<TreeNode> getBranches(){
return branches;
}
public void setPrintWriter(PrintWriter pr){
writer=pr;
}
public void printTree(TreeNode<T> node){
String newline = System.getProperty("line.separator");
LinkedList<TreeNode> queue = new LinkedList<TreeNode>();
//String result = "";
//OPTIONAL: if statement, - omit leaf parent nodes from printing
if(node.getOutcome()==null){
writer.write((newline+newline)+"Parent: "+node.getAttributeName()+" [Edge: "+node.getValue()+"]\n \r"+newline);
writer.write(" [ "+newline);
//writer.write(result);
for(TreeNode d : node.branches){
// result+="\t";
if(d.getOutcome()!=null){
writer.write("\tLeaf: "+d.getValue()+" => "+d.getOutcome()+newline);
//writer.write(result);
}else{
writer.write("\tAttributeNode:" +d.getAttributeName()+" (Edge=>"+d.getValue()+"). "+newline);
queue.addLast(d);
}
}
writer.write(" ]\n"+newline);
for(TreeNode n : queue){
printTree(n);
}
//writer.write(result);
writer.flush();
}//end optional
//return result;
}
}