/** * Assembles some number of elements from the top of the global stack into a new Tree, and * replaces those elements with the new Tree. * * <p><b>Caution:</b> The arguments must be consecutive integers 1..N, in any order, but with no * gaps; for example, makeTree(2,4,1,5) would cause problems (3 was omitted). * * @param rootIndex Which stack element (counting from 1) to use as the root of the new Tree. * @param childIndices Which stack elements to use as the children of the root. */ void makeTree(int rootIndex, int... childIndices) { // Get root from stack Tree<Token> root = getStackItem(rootIndex); // Get other trees from stack and add them as children of root for (int i = 0; i < childIndices.length; i++) { root.addChild(getStackItem(childIndices[i])); } // Pop root and all children from stack for (int i = 0; i <= childIndices.length; i++) { stack.pop(); } // Put the root back on the stack stack.push(root); }