package binaryTree.visitor; import binaryTree.*; /** * Prints the binary tree host vertically. * @author Dung X. Nguyen */ public class VerticalPrintHelper implements IVisitor { /** @SBGen Singleton Variable */ public final static VerticalPrintHelper Singleton = new VerticalPrintHelper (); /** @SBGen Constructor */ private VerticalPrintHelper() { } /** * Prints [] on System.out and stays on the same line of output. * @param host * @param input an Integer indicating the level of the node. * @return null */ public Object emptyCase(BiTree host, Object input) { int level = ((Integer)input).intValue (); printSpaces (level); System.out.print ("[]"); return null; } /** * Prints the host tree vertically on System.out and stays on the same line of output as the last printed tree node. * @param host * @param input an Integer indicating the level of the node. * @return null */ public Object nonEmptyCase(BiTree host, Object input) { int level = ((Integer)input).intValue (); printSpaces (level); System.out.println (host.getRootDat()); host.getLeftSubTree().execute(this, new Integer (++level)); System.out.println (); host.getRightSubTree().execute(this, new Integer (level)); return (null); } /** * Prints n spaces. */ private void printSpaces (int n) { for (int i = 0; i < n; i++) { System.out.print(' '); } } }