Pages

InOrder Tree Traversal

Steps for traverseInOrder :  

  1. Create Empty Stack
  2. Push root on Stack
  3. Repeat while stack not empty
    1. push left tree on stack  :  set current = current.left && Push current on stack and  
    2. pop stack and visit the node
    3. push right tree in stack : current = current.right;                                                       stack.push(current);


public void traverseInOrderWithoutRecursion() {

        Stack<Node> stack = new Stack<Node>();

        Node current = root;

        stack.push(root);

        while(! stack.isEmpty()) {

            while(current.left != null) {

                current = current.left;             
                stack.push(current);             
            }

            current = stack.pop();
            visit(current.value);
            if(current.right != null) {
                current = current.right;             
                stack.push(current);
            }
        }
    }