public void postOrderTraversal(LambdaOne<IntBinaryTree> function) { // Post-order traversal of each child. if (leftChild != null) { leftChild.postOrderTraversal(function); } if (rightChild != null) { rightChild.postOrderTraversal(function); } // Visit this node. function.call(this); }
/** 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()); }
/** Gets the leaves of this tree. */ public ArrayList<IntBinaryTree> getLeaves() { LeafCollector leafCollector = new LeafCollector(); postOrderTraversal(leafCollector); return leafCollector.leaves; }