package binaryTree.visitor; import binaryTree.*; /** * Prints the binary tree host vertically. * @author Alan L. Cox * @author Dung X. Nguyen * @since 04/05/01 */ public class VerticalPrintHelper implements IAlgo { public final static VerticalPrintHelper Singleton = new VerticalPrintHelper(); 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.getRootData()); level++; 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(' '); } } }