/** * Creates a {@link TreeString}. Useful if you need to create one-off * {@link TreeString} without {@link TreeStringBuilder}. Memory consumption * is still about the same to {@code new String(s)}. * * @return null if the parameter is null */ public static TreeString of(final String s) { if (s == null) { return null; } return new TreeString(null, s); }
/** * Inserts a new node between this node and its parent, and returns the * newly inserted node. * <p> * This operation doesn't change the string representation of this node. */ /* package */TreeString split(final String prefix) { assert getLabel().startsWith(prefix); char[] suffix = new char[label.length - prefix.length()]; System.arraycopy(label, prefix.length(), suffix, 0, suffix.length); TreeString middle = new TreeString(parent, prefix); label = suffix; parent = middle; return middle; }
children.put(s, t = new Child(new TreeString(node, s)));
/** * Creates a {@link TreeString}. Useful if you need to create one-off * {@link TreeString} without {@link TreeStringBuilder}. Memory consumption * is still about the same to {@code new String(s)}. * * @return null if the parameter is null */ public static TreeString of(final String s) { if (s == null) { return null; } return new TreeString(null, s); }
/** * Inserts a new node between this node and its parent, and returns the * newly inserted node. * <p> * This operation doesn't change the string representation of this node. */ /* package */TreeString split(final String prefix) { assert getLabel().startsWith(prefix); char[] suffix = new char[label.length - prefix.length()]; System.arraycopy(label, prefix.length(), suffix, 0, suffix.length); TreeString middle = new TreeString(parent, prefix); label = suffix; parent = middle; return middle; }
children.put(s, t = new Child(new TreeString(node, s)));
public void printTree(BSTNode node, int depth) { if (depth > 0) { TreeString treeString = treeStringFromBSTNode(node, depth); for (int i = 0; i < treeString.getLineCount(); ++i) { System.out.println(treeString.getLine(i)); } } } public TreeString treeStringFromString(String string) { return new TreeString(Collections.singletonList(string), string.length(), string.length() / 2); } public TreeString treeStringFromBSTNode(BSTNode node, int depth) { TreeString value = treeStringFromString(String.valueOf(node.getValue())); TreeString left = depth <= 1 || node.getLeft() == null ? null : treeStringFromBSTNode(node.getLeft(), depth - 1); TreeString right = depth <= 1 || node.getRight() == null ? null : treeStringFromBSTNode(node.getRight(), depth - 1); return combineTreeStrings(value, left, right); }