/** * Gives string representation of a tree, parametrized by how to represent each position. It does * a preorder traversal, giving a newline and some indentation before each node, then represents * each node as specified. It can print a tree which is at most 100 levels deep. * * @param t tree to stringify * @param pts how to stringify each position * @throws IndexOutOfBoundsException if the tree is more then 100 levels deep * @return the string representation of t */ public static String stringfor(InspectableTree t, PositionToString pts) { if (spaces == null) { spaces = new byte[200]; // tree can't be more than 100 levels deep for (int i = 0; i < 200; ++i) spaces[i] = (byte) ' '; } // the 4 below was generated entirely at random ByteArrayOutputStream bstr = new ByteArrayOutputStream(t.size() * 4); DataOutputStream ostr = new DataOutputStream(bstr); writeNodeAndChildren(t.root(), pts, ostr, t, 0, 2); return bstr.toString(); }