private void populateInOrder(TreeNode<T> currentNode, final LinkedList<TreeNode<T>> stack) { List<TreeNode<T>> children = currentNode.getChildren(); if (children.size() == 0) { stack.addLast(currentNode); } else if (children.size() == 1) { stack.addLast(currentNode); populateInOrder(children.get(0), stack); } else if (children.size() == 2) { populateInOrder(children.get(0), stack); stack.addLast(currentNode); populateInOrder(children.get(1), stack); } else { throw new IllegalArgumentException( "Can't create an in-order iterator for a node with " + children.size() + " children."); } }
public Iterator<TreeNode<T>> getInOrderIter() { final LinkedList<TreeNode<T>> stack = new LinkedList<TreeNode<T>>(); populateInOrder(this, stack); return stack.iterator(); }