Draw Binary Search Tree Java
Here's a very versatile tree printer. Not the best looking, but it handles a lot of cases. Feel free to add slashes if you can figure that out.
package com.tomac120.NodePrinter; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; /** * Created by elijah on 6/28/16. */ public class NodePrinter{ final private List<List<PrintableNodePosition>> nodesByRow; int maxColumnsLeft = 0; int maxColumnsRight = 0; int maxTitleLength = 0; String sep = " "; int depth = 0; public NodePrinter(PrintableNode rootNode, int chars_per_node){ this.setDepth(rootNode,1); nodesByRow = new ArrayList<>(depth); this.addNode(rootNode._getPrintableNodeInfo(),0,0); for (int i = 0;i<chars_per_node;i++){ //sep += " "; } } private void setDepth(PrintableNode info, int depth){ if (depth > this.depth){ this.depth = depth; } if (info._getLeftChild() != null){ this.setDepth(info._getLeftChild(),depth+1); } if (info._getRightChild() != null){ this.setDepth(info._getRightChild(),depth+1); } } private void addNode(PrintableNodeInfo node, int level, int position){ if (position < 0 && -position > maxColumnsLeft){ maxColumnsLeft = -position; } if (position > 0 && position > maxColumnsRight){ maxColumnsRight = position; } if (node.getTitleLength() > maxTitleLength){ maxTitleLength = node.getTitleLength(); } List<PrintableNodePosition> row = this.getRow(level); row.add(new PrintableNodePosition(node, level, position)); level++; int depthToUse = Math.min(depth,6); int levelToUse = Math.min(level,6); int offset = depthToUse - levelToUse-1; offset = (int)(Math.pow(offset,Math.log(depthToUse)*1.4)); offset = Math.max(offset,3); PrintableNodeInfo leftChild = node.getLeftChildInfo(); PrintableNodeInfo rightChild = node.getRightChildInfo(); if (leftChild != null){ this.addNode(leftChild,level,position-offset); } if (rightChild != null){ this.addNode(rightChild,level,position+offset); } } private List<PrintableNodePosition> getRow(int row){ if (row > nodesByRow.size() - 1){ nodesByRow.add(new LinkedList<>()); } return nodesByRow.get(row); } public void print(){ int max_chars = this.maxColumnsLeft+maxColumnsRight+1; int level = 0; String node_format = "%-"+this.maxTitleLength+"s"; for (List<PrintableNodePosition> pos_arr : this.nodesByRow){ String[] chars = this.getCharactersArray(pos_arr,max_chars); String line = ""; int empty_chars = 0; for (int i=0;i<chars.length+1;i++){ String value_i = i < chars.length ? chars[i]:null; if (chars.length + 1 == i || value_i != null){ if (empty_chars > 0) { System.out.print(String.format("%-" + empty_chars + "s", " ")); } if (value_i != null){ System.out.print(String.format(node_format,value_i)); empty_chars = -1; } else{ empty_chars = 0; } } else { empty_chars++; } } System.out.print("\n"); int depthToUse = Math.min(6,depth); int line_offset = depthToUse - level; line_offset *= 0.5; line_offset = Math.max(0,line_offset); for (int i=0;i<line_offset;i++){ System.out.println(""); } level++; } } private String[] getCharactersArray(List<PrintableNodePosition> nodes, int max_chars){ String[] positions = new String[max_chars+1]; for (PrintableNodePosition a : nodes){ int pos_i = maxColumnsLeft + a.column; String title_i = a.nodeInfo.getTitleFormatted(this.maxTitleLength); positions[pos_i] = title_i; } return positions; } }
NodeInfo class
package com.tomac120.NodePrinter; /** * Created by elijah on 6/28/16. */ public class PrintableNodeInfo { public enum CLI_PRINT_COLOR { RESET("\u001B[0m"), BLACK("\u001B[30m"), RED("\u001B[31m"), GREEN("\u001B[32m"), YELLOW("\u001B[33m"), BLUE("\u001B[34m"), PURPLE("\u001B[35m"), CYAN("\u001B[36m"), WHITE("\u001B[37m"); final String value; CLI_PRINT_COLOR(String value){ this.value = value; } @Override public String toString() { return value; } } private final String title; private final PrintableNode leftChild; private final PrintableNode rightChild; private final CLI_PRINT_COLOR textColor; public PrintableNodeInfo(String title, PrintableNode leftChild, PrintableNode rightChild){ this(title,leftChild,rightChild,CLI_PRINT_COLOR.BLACK); } public PrintableNodeInfo(String title, PrintableNode leftChild, PrintableNode righthild, CLI_PRINT_COLOR textColor){ this.title = title; this.leftChild = leftChild; this.rightChild = righthild; this.textColor = textColor; } public String getTitle(){ return title; } public CLI_PRINT_COLOR getTextColor(){ return textColor; } public String getTitleFormatted(int max_chars){ return this.textColor+title+CLI_PRINT_COLOR.RESET; /* String title = this.title.length() > max_chars ? this.title.substring(0,max_chars+1):this.title; boolean left = true; while(title.length() < max_chars){ if (left){ title = " "+title; } else { title = title + " "; } } return this.textColor+title+CLI_PRINT_COLOR.RESET;*/ } public int getTitleLength(){ return title.length(); } public PrintableNodeInfo getLeftChildInfo(){ if (leftChild == null){ return null; } return leftChild._getPrintableNodeInfo(); } public PrintableNodeInfo getRightChildInfo(){ if (rightChild == null){ return null; } return rightChild._getPrintableNodeInfo(); } }
NodePosition class
package com.tomac120.NodePrinter; /** * Created by elijah on 6/28/16. */ public class PrintableNodePosition implements Comparable<PrintableNodePosition> { public final int row; public final int column; public final PrintableNodeInfo nodeInfo; public PrintableNodePosition(PrintableNodeInfo nodeInfo, int row, int column){ this.row = row; this.column = column; this.nodeInfo = nodeInfo; } @Override public int compareTo(PrintableNodePosition o) { return Integer.compare(this.column,o.column); } }
And, finally, Node Interface
package com.tomac120.NodePrinter; /** * Created by elijah on 6/28/16. */ public interface PrintableNode { PrintableNodeInfo _getPrintableNodeInfo(); PrintableNode _getLeftChild(); PrintableNode _getRightChild(); }
Source: https://stackoverflow.com/questions/4965335/how-to-print-binary-tree-diagram-in-java
0 Response to "Draw Binary Search Tree Java"
Post a Comment