forked from itrobertson/DecisionTree
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDecisionTree.java
More file actions
95 lines (73 loc) · 2.48 KB
/
DecisionTree.java
File metadata and controls
95 lines (73 loc) · 2.48 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
package decisiontree;
import java.io.PrintWriter;
import java.util.*;
public class DecisionTree<E> {
private PrintWriter writerTree;
private TreeNode<E> root;
//@result used for printing DecisionTree
private String result = "";
public DecisionTree(){
//init
}
public DecisionTree(String attributeName){
root = new TreeNode<>(attributeName);
}
public DecisionTree(String leafNode, E value){
root = new TreeNode<>(leafNode,value);
}
/**
*
* @return E and String getters are used by TreeNode
* as Branches data structure is of type <DecisionTree>
*
*/
public String getAttributeName(){
return root.getAttributeName();
}
public E getValue(){
return root.getValue();
}
public E getOutcome(){
return root.getOutcome();
}
public void setValue(E edgeValue){
root.setValue(edgeValue);
}
public void addBranch(E value, DecisionTree<E> tree)
{
root.addBranch(value,tree.getTree());
}
public TreeNode<E> getTree(){
return root;
}
public E predict(Example e) {
//System.out.println("E: "+e.accessData()+" "+e.getGoal()+" Predict: "+root.predict(e).getOutcome());
E outcome = (E) root.predict(e);
if(outcome!=null){
return (E) root.predict(e).getOutcome();
}else{
return null;
}
}
/**
*
* getBranches() can be called from outside DecisionTree in order to
* initiate recursion, as well as internally for recursion.
*/
public void getBranches(PrintWriter writerTree){
this.writerTree=writerTree;
//System.out.println(root.printTree(writerTree));
getBranches(root);
}
public void getBranches(TreeNode branchSet){
writerTree.println();
writerTree.write("///////////////////////////////////////START NEW TREE");
writerTree.println();
root.setPrintWriter(writerTree);
root.printTree(root);
}
@Override
public String toString(){
return root.getAttributeName();
}
}