package main.data.structure;

import static java.lang.System.out;

/**
 * Title : main.data.structure <br>
 * Description :
 *  <p>
 *  <ul> 
 *      <li>
 *  </ul>
 * @author chile
 * @version 1.0
 * @date 2018/4/13 14:38
 */
public class OrdinaryBinaryTree {

    static class Node {
        int value;
        Node left;
        Node right;
        Node(int x) {
            this.value = x;
        }
    }

    public static void main(String[] args) {

        Node[] nodes = new Node[10];
        for (int i = 0; i < nodes.length; i++) {
            nodes[i] = new Node(i);
        }

        /*
         *            0
         *          ╱   ╲
         *         1     6
         *        ╱ ╲   ╱ ╲
         *       2   5 7   8
         *      ╱ ╲         ╲
         *     3   4         9
         */
        // build test binary tree
        nodes[0].left = nodes[1];
        nodes[1].left = nodes[2];
        nodes[2].left = nodes[3];
        nodes[6].left = nodes[7];
        nodes[0].right = nodes[6];
        nodes[1].right = nodes[5];
        nodes[2].right = nodes[4];
        nodes[6].right = nodes[8];
        nodes[8].right = nodes[9];

        // play with it
        Node rootNode = nodes[0];

        recursivePreorderTraversal(rootNode);
        out.println();
        recursiveInorderTraversal(rootNode);
        out.println();
        recursivePostorderTraversal(rootNode);
        out.println();
    }


    private static void recursivePreorderTraversal(Node root) {
        if (null != root) {
            out.print(root.value);
            recursivePreorderTraversal(root.left);
            recursivePreorderTraversal(root.right);
        }
    }

    private static void recursiveInorderTraversal(Node root) {
        if (null != root) {
            recursiveInorderTraversal(root.left);
            out.print(root.value);
            recursiveInorderTraversal(root.right);
        }
    }

    private static void recursivePostorderTraversal(Node root) {
        if (null != root) {
            recursivePreorderTraversal(root.left);
            recursivePreorderTraversal(root.right);
            out.print(root.value);
        }
    }

}

@@ -23,16 +23,84 @@
 cture;%0A%0A
+import org.jetbrains.annotations.NotNull;%0A%0Aimport java.util.Stack;%0A%0A
 import s
@@ -127,16 +127,16 @@
 em.out;%0A
-
 %0A/**%0A * 
@@ -857,16 +857,135 @@
       9%0A
+         *%0A         *   preorder  : 0123456789%0A         *   inorder   : 3241507689%0A         *   postorder : 1234567890%0A
         
@@ -1454,32 +1454,34 @@
  out.println();%0A
+//
         recursiv
@@ -1501,32 +1501,34 @@
 rsal(rootNode);%0A
+//
         out.prin
@@ -1530,24 +1530,26 @@
 .println();%0A
+//
         recu
@@ -1575,32 +1575,94 @@
 rsal(rootNode);%0A
+//        out.println();%0A        preorderTraversal(rootNode);%0A
         out.prin
@@ -2355,32 +2355,32 @@
 al(root.right);%0A
-
             out.
@@ -2415,10 +2415,556 @@
 %0A    %7D%0A%0A
+    private static void preorderTraversal(Node root) %7B%0A        Stack%3CNode%3E s = new Stack%3C%3E();%0A        Node p = root;%0A        while (null != p %7C%7C !s.isEmpty()) %7B%0A            out.print(p.value);%0A            if (null != p.left) %7B%0A                s.push(p);%0A                p = p.left;%0A            %7D else if (!s.isEmpty())%7B%0A                p = s.pop().right;%0A                while (null == p && !s.isEmpty()) %7B%0A                    p = s.pop().right;%0A                %7D%0A            %7D else %7B%0A                p = p.right;%0A            %7D%0A%0A        %7D%0A    %7D%0A%0A
 %7D%0A

data structure: ordinary binary tree