001 002 package brs; 003 004 /** 005 * Computes a String representation of the binary tree host so that it can be 006 * printed vertically, given a leftmost leading string for the two subtrees. 007 * Called by ToString. 008 * Should be implemented as an anonymous inner class in the call by ToString. 009 * @author Dung X. Nguyen - Copyright 2001 - All rights reserved. 010 * @author Mathias Ricken - Copyright 2008 - All rights reserved. 011 */ 012 public class ToStringHelp<T> implements IVisitor<T,String,String> { 013 /** 014 * Returns "|_[]" to denote an empty tree subtree. 015 * @param host an empty binary (sub)tree. 016 * @param nu not used. 017 * @return String 018 */ 019 public String emptyCase(BiTree<T> host, String... nu) { 020 return "|_ []"; 021 } 022 023 /** 024 * Computes a String representation of the binary tree host so that it 025 * can be printed vertically. 026 * There is no '\n' at the end of the String. 027 * @param host a non-empty binary (sub)tree. 028 * @param leftLead appropriate leftmost leading String to help compute the 029 * String representations of the left and right subtrees. 030 * @return String 031 */ 032 public String nonEmptyCase(BiTree<T> host, String... leftLead) { 033 String ls = host.getLeftSubTree().execute(this, leftLead[0] + "| "); 034 String rs = host.getRightSubTree().execute(this, leftLead[0] + " "); 035 return ("|_ " + host.getRootDat()+ "\n" + 036 leftLead[0] + ls + "\n" + 037 leftLead[0] + rs); 038 } 039 }