private void populatePreOrder(TreeNode<T> currentNode, final LinkedList<TreeNode<T>> stack) { List<TreeNode<T>> children = currentNode.getChildren(); stack.addLast(currentNode); for (TreeNode<T> childNode : children) { populatePreOrder(childNode, stack); } }
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."); } }