import java.util.ArrayList; import java.util.List; import java.util.TreeMap; public class DecisionTree { boolean success=true,failure=false; public static void main(String[] args) { Node loan = new Node("Loan"); Node age = new Node("Age"); Node job = new Node("HasJob"); Node house = new Node("OwnHouse"); Node crating = new Node("CreditRating"); Node id = new Node("Id"); Node yes = new Node("yes"); Node no = new Node("no"); house.left = job; house.right = yes; job.left = no; job.right = yes; nicePrint(house); } boolean stoppingCond(){ return success; } boolean createNode(){ return failure; } void classify(Object E, Object F){ } boolean connectDatabase(){ return failure; } Node find_best_split(){ Node bestNode=new Node("top"); return bestNode; } String test_cond(){ String attribute=new String("attrib1"); return attribute; } void TreeGrowth(){ } void Insert(){ } void Delete(){ } public static void nicePrint(Node root) { List< StringPoint > result = getStrings((getWidth(root) + 1) / 2, 0, root); TreeMap< Integer, List< StringPoint > > lines = new TreeMap< >(); for (StringPoint s : result) { if (lines.get(s.y) != null) { lines.get(s.y).add(s); } else { List< StringPoint > l = new ArrayList< >(); l.add(s); lines.put(s.y, l); } } for (List< StringPoint > l : lines.values()) { System.out.println(flatten(l)); } } private static String flatten(List< StringPoint > l) { int x = 0; StringBuilder sb = new StringBuilder(); for (StringPoint s : l) { sb.append(new String(new char[s.x - x]).replace('\0', ' ')); sb.append(s.value); x = sb.length(); } return sb.toString(); } private static int getWidth(Node root) { int width = 0; if (root.left != null) { width += getWidth(root.left); } if (root.right != null) { width += getWidth(root.right); } width += ("" + root.value).length(); return width; } private static List< StringPoint > getStrings(int x, int y, Node root) { List< StringPoint > result = new ArrayList< StringPoint >(); result.add(new StringPoint(x - ("" + root.value).length() / 2, y, "" + root.value)); if (root.left != null) { int width = getWidth(root.left); int i = 0; for (; i < (width + 1) / 2; ++i) result.add(new StringPoint(x - i - 1, y + i + 1, "/")); result.addAll(getStrings(x - i - 1, y + i + 1, root.left)); } if (root.right != null) { int width = getWidth(root.right); int i = 0; for (; i < (width + 1) / 2; ++i) result.add(new StringPoint(x + i + 1, y + i + 1, "\\")); result.addAll(getStrings(x + i + 1, y + i + 1, root.right)); } return result; } static class StringPoint { Integer x; Integer y; String value; StringPoint(int x, int y, String value) { this.x = x; this.y = y; this.value = value; } @Override public String toString() { return "(" + x + "," + y + "," + value + ")"; } } static class Node { Node left; Node right; String value; public Node(String value) { this.value = value; } } }