You could do a very easy HTML output.
The following is linear output.
/**
* Outputs an HTML file representation of a binary tree.
*/
public class PreOrderTraversalBinTree {
private String expression;
public PreOrderTraversalBinTree(String expression) {
this.expression = expression;
System.out.println("<html><head><title>" + expression + "</title></head><body>");
System.out.println("<h2>" + expression + "</h2>");
represent(expression);
System.out.println("</body></html>");
}
/**
* @return the number of consumed characters.
*/
private int represent(String expression) {
if (expression.length() == 0)
return 0;
String node = expression.substring(0, 1);
if ("+*-/".indexOf(node) == -1) {
// Constant:
System.out.println(node);
return 1;
}
// Binary operator:
expression = expression.substring(1);
System.out.println("<table border=\"0\">");
System.out.println("<tr><th colspan=\"2\">" + node + "</th></tr>");
System.out.println("<tr><th>");
int lhs = represent(expression);
expression = expression.substring(lhs);
System.out.println("</th><th>");
int rhs = represent(expression);
System.out.println("</th></tr>");
System.out.println("</table>");
return 1 + lhs + rhs;
}
public static void main(String[] args) {
new PreOrderTraversalBinTree("+*34+/567");
}
}