/** * 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; }
/** * Calls {@link TreeString#dedup(Map)} recursively. */ private void dedup(final Map<String, char[]> table) { node.dedup(table); for (Child child : children.values()) { child.dedup(table); } } }
/** * Returns the full string representation. */ @Override public String toString() { char[][] tokens = new char[depth()][]; int i = tokens.length; int sz = 0; for (TreeString p = this; p != null; p = p.parent) { tokens[--i] = p.label; sz += p.label.length; } StringBuilder buf = new StringBuilder(sz); for (char[] token : tokens) { buf.append(token); } return buf.toString(); }
/** * Inserts a new node between this node and its parent, and returns that * node. Newly inserted 'middle' node will have this node as its sole * child. */ private Child split(final String prefix) { String suffix = node.getLabel().substring(prefix.length()); Child middle = new Child(node.split(prefix)); middle.makeWritable(); middle.children.put(suffix, this); return middle; }
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); }
/** * 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); }
public static String toString(final TreeString t) { return t == null ? null : t.toString(); }
children.put(s, t = new Child(new TreeString(node, s)));
/** * Inserts a new node between this node and its parent, and returns that * node. Newly inserted 'middle' node will have this node as its sole * child. */ private Child split(final String prefix) { String suffix = node.getLabel().substring(prefix.length()); Child middle = new Child(node.split(prefix)); middle.makeWritable(); middle.children.put(suffix, this); return middle; }
public boolean isBlank() { return StringUtils.isBlank(toString()); }
/** * Interns {@link #label} */ /* package */void dedup(final Map<String, char[]> table) { String l = getLabel(); char[] v = table.get(l); if (v != null) { label = v; } else { table.put(l, label); } }
/** * 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; }
/** * 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); }
/** * Interns a {@link TreeString} created elsewhere. */ public TreeString intern(final TreeString s) { if (s==null) return null; return root.intern(s.toString()).node; }
/** * Calls {@link TreeString#dedup(Map)} recursively. */ private void dedup(final Map<String, char[]> table) { node.dedup(table); for (Child child : children.values()) { child.dedup(table); } } }
/** * Returns the full string representation. */ @Override public String toString() { char[][] tokens = new char[depth()][]; int i = tokens.length; int sz = 0; for (TreeString p = this; p != null; p = p.parent) { tokens[--i] = p.label; sz += p.label.length; } StringBuilder buf = new StringBuilder(sz); for (char[] token : tokens) { buf.append(token); } return buf.toString(); }