package binaryTree.visitor; import binaryTree.*; /** * Computes a String representation of the binary tree host so that it can be printed vertically, * given a leftmost leading string for the two subtrees. * Called by ToString. * Can be implemented as an anonymous inner class in the call by ToString. * @author Dung X. Nguyen - Copyright 2000 - All rights reserved. */ public class ToStringHelp implements IVisitor { public static final ToStringHelp Singleton = new ToStringHelp (); private ToStringHelp () { } /** * Returns "|_[]" to denote an empty tree subtree. * @param host an empty binary (sub)tree. * @param input not used. * @return "|_[]". */ public Object emptyCase(BiTree host, Object input) { return "|_ []"; } /** * Computes a String representation of the binary tree host so that it can be printed vertically. * There is no '\n' at the end of the String. * @param host a non-empty binary (sub)tree. * @param input appropriate leftmost leading String to help compute the String representations * of the left and right subtrees. * @return ("|_" + root + "\n" + * input + left subtree + "\n" + * input + right subtree) */ public Object nonEmptyCase(BiTree host, Object input) { String ls = (String)host.getLeftSubTree().execute(this, input + "| "); String rs = (String) host.getRightSubTree().execute(this, input + " "); return ("|_ " + host.getRootDat()+ "\n" + input + ls + "\n" + input + rs); } }