-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
40 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
src/main/java/quickml/supervised/tree/decisionTree/DecisionTreeVisualizer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package quickml.supervised.tree.decisionTree; | ||
|
||
import quickml.supervised.tree.decisionTree.valueCounters.ClassificationCounter; | ||
import quickml.supervised.tree.nodes.Branch; | ||
import quickml.supervised.tree.nodes.Leaf; | ||
import quickml.supervised.tree.nodes.Node; | ||
|
||
import java.io.PrintStream; | ||
|
||
/** | ||
* Created by ian on 7/20/15. | ||
*/ | ||
public class DecisionTreeVisualizer { | ||
|
||
public static final int INDENT_AMOUNT = 3; | ||
|
||
public void visualize(DecisionTree tree, PrintStream out) { | ||
visualize(tree.root, out, 0); | ||
} | ||
|
||
private void visualize(final Node<ClassificationCounter> node, final PrintStream out, final int depth) { | ||
StringBuilder indentBuilder = new StringBuilder(); | ||
for (int x = 0; x < depth; x++) { | ||
indentBuilder.append(' '); | ||
} | ||
String indent = indentBuilder.toString(); | ||
|
||
if (node instanceof Branch) { | ||
Branch<ClassificationCounter> branch = (Branch<ClassificationCounter>) node; | ||
out.println(indent + branch.toString() + " TRUE:"); | ||
visualize(branch.getTrueChild(), out, depth + INDENT_AMOUNT); | ||
out.println(indent + branch.toString() + " FALSE:"); | ||
visualize(branch.getFalseChild(), out, depth + INDENT_AMOUNT); | ||
} else if (node instanceof Leaf) { | ||
out.println(indent + "LEAF: " + node.toString()); | ||
} | ||
} | ||
|
||
} |