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)); } }
public IntNaryTree collapseToNary(Alphabet<String> ntAlphabet) { Alphabet<String> alphabet = isLexical ? this.alphabet : ntAlphabet; // Reset the symbol id according to the new alphabet. int symbol = alphabet.lookupIndex(getSymbolLabel()); ArrayList<IntNaryTree> children = null; if (!isLeaf()) { assert (leftChild != null); LinkedList<IntNaryTree> queue = new LinkedList<IntNaryTree>(); addToQueue(queue, leftChild, ntAlphabet); addToQueue(queue, rightChild, ntAlphabet); children = new ArrayList<IntNaryTree>(queue); } return new IntNaryTree(symbol, start, end, children, isLexical, alphabet); }