package me.wayne.ds;

import java.util.Stack;

public class Traverse {

    public static void preOrder(BitTree tree) {
        if (tree == null) return;

        System.out.printf(tree.value + " ");

        if (tree.left != null) {
            preOrder(tree.left);
        }
        if (tree.right != null) {
            preOrder(tree.right);
        }
    }

    public static void inOrder(BitTree tree) {
        if (tree == null) return;

        if (tree.left != null) {
            inOrder(tree.left);
        }
        System.out.printf(tree.value + " ");
        if (tree.right != null) {
            inOrder(tree.right);
        }
    }

    public static void postOrder(BitTree tree) {
        if (tree == null) return;

        if (tree.left != null) {
            postOrder(tree.left);
        }
        if (tree.right != null) {
            postOrder(tree.right);
        }
        System.out.printf(tree.value + " ");
    }

    public static void nonRecursionPreOrder(BitTree tree) {
        Stack<BitTree> stack = new Stack<BitTree>();
        while (!stack.isEmpty() || tree != null) {
            if (tree != null) {
                System.out.printf(tree.value + " ");
                stack.push(tree);
                tree = tree.left;
            } else {
                tree = stack.pop();
                tree = tree.right;
            }
        }
    }

    public static void nonRecursionInOrder(BitTree tree) {
        Stack<BitTree> stack = new Stack<BitTree>();
        while (!stack.isEmpty() || tree != null) {
            if (tree != null) {
                stack.push(tree);
                tree = tree.left;
            } else {
                tree = stack.pop();
                System.out.printf(tree.value + " ");
                tree = tree.right;
            }
        }
    }
}

@@ -40,23 +40,16 @@
 Stack;%0A%0A
-public 
 class Tr
@@ -58,31 +58,24 @@
 erse %7B%0A%0A    
-public 
 static void 
@@ -150,33 +150,32 @@
 System.out.print
-f
 (tree.value + %22 
@@ -336,39 +336,32 @@
    %7D%0A    %7D%0A%0A    
-public 
 static void inOr
@@ -506,33 +506,32 @@
 System.out.print
-f
 (tree.value + %22 
@@ -614,39 +614,32 @@
    %7D%0A    %7D%0A%0A    
-public 
 static void post
@@ -867,33 +867,32 @@
 System.out.print
-f
 (tree.value + %22 
@@ -898,39 +898,32 @@
  %22);%0A    %7D%0A%0A    
-public 
 static void nonR
@@ -988,39 +988,32 @@
 ack = new Stack%3C
-BitTree
 %3E();%0A        whi
@@ -1108,33 +1108,32 @@
 System.out.print
-f
 (tree.value + %22 
@@ -1335,15 +1335,8 @@
     
-public 
 stat
@@ -1420,23 +1420,16 @@
 w Stack%3C
-BitTree
 %3E();%0A   
@@ -1665,17 +1665,16 @@
 ut.print
-f
 (tree.va

clean code.