private void getAsPennTreebankString(int indent, int numOnLine, StringBuilder sb) { int numSpaces = indent - numOnLine; for (int i = 0; i < numSpaces; i++) { sb.append(" "); } if (isLexical) { sb.append(getSymbolStr()); } else { sb.append("("); sb.append(getSymbolStr()); // If this is a constant instead, then we have each depth in one column. int numNewChars = 1 + getSymbolStr().length(); if (leftChild != null) { // sb.append("\n"); leftChild.getAsPennTreebankString(indent + numNewChars + 1, indent + numNewChars, sb); } if (rightChild != null) { sb.append("\n"); rightChild.getAsPennTreebankString(indent + numNewChars + 1, 0, sb); } sb.append(")"); } }
/** Updates all the start end fields, treating the current node as the root. */ public void updateStartEnd() { ArrayList<IntBinaryTree> leaves = getLeaves(); for (int i = 0; i < leaves.size(); i++) { IntBinaryTree leaf = leaves.get(i); leaf.start = i; leaf.end = i + 1; } postOrderTraversal(new UpdateStartEnd()); }
public void preOrderTraversal(LambdaOne<IntBinaryTree> function) { // Visit this node. function.call(this); // Pre-order traversal of each child. if (leftChild != null) { leftChild.preOrderTraversal(function); } if (rightChild != null) { rightChild.preOrderTraversal(function); } }
@Override public void call(IntBinaryTree node) { if (!node.isLeaf()) { node.start = node.leftChild.start; if (node.rightChild == null) { node.end = node.leftChild.end; } else { node.end = node.rightChild.end; } } }
public void inOrderTraversal(LambdaOne<IntBinaryTree> function) { // In-order traversal of left child. if (leftChild != null) { leftChild.inOrderTraversal(function); } // Visit this node. function.call(this); // In-order traversal of right child. if (rightChild != null) { rightChild.inOrderTraversal(function); } }
private static void addToQueue( LinkedList<IntNaryTree> queue, IntBinaryTree child, Alphabet<String> ntAlphabet) { if (child == null) { return; } String symbolStr = child.getSymbolStr(); if (GrammarConstants.isBinarized(symbolStr)) { addToQueue(queue, child.leftChild, ntAlphabet); addToQueue(queue, child.rightChild, ntAlphabet); } else { queue.add(child.collapseToNary(ntAlphabet)); } }
@Override public void call(IntBinaryTree node) { if (node.isLeaf()) { leaves.add(node); } }