二叉樹迭代/非迭代前/中/後序排列

package Binary;

import java.util.LinkedList;

public class BinaryTree {
    private TreeNode root = null;

    public BinaryTree() {
        root = new TreeNode(1, "A");
    }

    /**
     * 創建二叉樹 
     *        A     ;
     *     B     C   ;
     *   D   E  F  G ;
     * 
     * 
     */
    public void createBinaryTree() {
        TreeNode nodeB = new TreeNode(2, "B");
        TreeNode nodeC = new TreeNode(3, "C");
        TreeNode nodeD = new TreeNode(4, "D");
        TreeNode nodeE = new TreeNode(5, "E");
        TreeNode nodeF = new TreeNode(6, "F");
        TreeNode nodeG = new TreeNode(7, "G");

        root.leftChild = nodeB;
        root.rightChild = nodeC;
        nodeB.leftChild = nodeD;
        nodeB.rightChild = nodeE;
        nodeC.leftChild = nodeF;
        nodeC.rightChild = nodeG;

    }

    /**
     * 獲得二叉樹的深度
     * 
     * @author my
     *
     */
    public int getHeight() {
        return getHeight(root);
    }

    private int getHeight(TreeNode node) {
        if (node == null) {
            return 0;
        } else {
            int i = getHeight(node.leftChild);
            int j = getHeight(node.rightChild);
            return (i < j) ? j + 1 : i + 1;
        }
    }

    /**
     * 獲取二叉樹的節點數
     * 
     */
    public int getSize() {
        return getSize(root);
    }

    private int getSize(TreeNode node) {
        if (node == null) {
            return 0;
        } else {
            return 1 + getSize(node.leftChild) + getSize(node.rightChild);
        }
    }

    /**
     * 迭代方式獲取 --前序遍歷
     */
    public void preOrder(TreeNode node) {
        if (node == null) {
            return;
        } else {
            System.out.println("preOrder data :" + node.data);
            if (node.leftChild != null) {
                preOrder(node.leftChild);
            }
            if (node.rightChild != null) {
                preOrder(node.rightChild);
            }
        }
    }

    /**
     * 非迭代方式獲取 --前序遍歷
     */
    public void preNonRecOrder(TreeNode node) {
        LinkedList<TreeNode> linkNode = new LinkedList<TreeNode>();
        if (node == null) {
            return;
        } else {
            linkNode.push(node);
        }
        while(!linkNode.isEmpty()) {
            TreeNode tn = linkNode.pop();
            System.out.println("preNonRecOrder : "+tn.data);
            if (tn.rightChild != null) {
                linkNode.push(tn.rightChild);
            }
            if (tn.leftChild != null) {
                linkNode.push(tn.leftChild);
            }
        }

    }

    /**
     * 迭代方式獲取 --後序遍歷
     */
    public void postOrder(TreeNode node) {
        if (node == null) {
            return;
        } else {
            if (node.leftChild != null) {
                postOrder(node.leftChild);
            }
            if (node.rightChild != null) {
                postOrder(node.rightChild);
            }
            System.out.println("postOrder data :" + node.data);
        }
    }
    /**
     * 非迭代方式獲取 --後序遍歷DEBFGCA
     *        A     ;
     *     B     C   ;
     *   D   E  F  G ;
     */
    public void postNonRecOrder(TreeNode node) {
        LinkedList<TreeNode> linkNode = new LinkedList<TreeNode>();
        if (node == null) {
            return;
        } else {
            linkNode.push(node);
            if (node.rightChild != null) {
                linkNode.push(node.rightChild);
            }
            if (node.leftChild != null) {
                linkNode.push(node.leftChild);
            }
            node.visited=1;
        }
        while(!linkNode.isEmpty()) {
            TreeNode tn = linkNode.pop();
            if(tn.rightChild == null && tn.leftChild == null) {
                tn.visited = 1;
                System.out.println("midNonRecOrder : "+tn.data);
                continue;
            }
            if(tn.visited != 1) {
                linkNode.push(tn);
                if (tn.rightChild != null) {
                    linkNode.push(tn.rightChild);
                }
                if (tn.leftChild != null) {
                    linkNode.push(tn.leftChild);
                }
                tn.visited = 1;
            }else {
                System.out.println("midNonRecOrder : "+tn.data);
            }
        }

    }
    /**
     *        A     ;
     *     B     C   ;
     *   D   E  F  G ;
     * 
     * 迭代方式獲取 --中序遍歷
     */
    public void midOrder(TreeNode node) {
        if (node == null) {
            return;
        } else {
            if (node.leftChild != null) {
                midOrder(node.leftChild);
            }
            System.out.println("midOrder data :" + node.data);
            if (node.rightChild != null) {
                midOrder(node.rightChild);
            }
        }
    }
    /**
     * 非迭代方式獲取 --中序遍歷   DEBAFGC
     *        A     ;
     *     B     C   ;
     *   D   E  F  G ;
     */
    public void midNonRecOrder(TreeNode node) {
        LinkedList<TreeNode> linkNode = new LinkedList<TreeNode>();
        if (node == null) {
            return;
        } else {
            if (node.rightChild != null) {
                linkNode.push(node.rightChild);
            }
            linkNode.push(node);
            node.visited=1;
            if (node.leftChild != null) {
                linkNode.push(node.leftChild);
            }
        }
        while(!linkNode.isEmpty()) {
            TreeNode tn = linkNode.pop();
            if(tn.rightChild == null && tn.leftChild == null) {
                tn.visited = 1;
                System.out.println("midNonRecOrder : "+tn.data);
                continue;
            }
            if(tn.visited != 1) {
                if (tn.rightChild != null) {
                    linkNode.push(tn.rightChild);
                }
                linkNode.push(tn);
                if (tn.leftChild != null) {
                    linkNode.push(tn.leftChild);
                }
                tn.visited = 1;
            }else {
                System.out.println("midNonRecOrder : "+tn.data);
            }
        }

    }

    /**
     * main 方法 測試結果
     */
    public static void main(String[] args) {
        BinaryTree bt = new BinaryTree();
        bt.createBinaryTree();
//      System.out.println("BinaryTree 深度:" + bt.getHeight());
//      System.out.println("BinaryTree 節點數:" + bt.getSize());
//      bt.preOrder(bt.root);
//      bt.midOrder(bt.root);
//      bt.postOrder(bt.root);
//      bt.preNonRecOrder(bt.root);
//      bt.midNonRecOrder(bt.root);
        bt.postNonRecOrder(bt.root);


    }

    /**
     * 二叉樹的元素 :結點 具有左右結點和父結點;
     * 
     * @author my
     *
     */
    class TreeNode {
        private int index;
        private String data;
        private TreeNode leftChild;
        private TreeNode rightChild;
        private int visited;

        public TreeNode(int index, String data) {
            super();
            this.index = index;
            this.data = data;
            this.visited = 0;
            this.leftChild = null;
            this.rightChild = null;
        }

        public int getVisited() {
            return visited;
        }

        public void setVisited(int visited) {
            this.visited = visited;
        }

        public int getIndex() {
            return index;
        }

        public void setIndex(int index) {
            this.index = index;
        }

        public String getData() {
            return data;
        }

        public void setData(String data) {
            this.data = data;
        }

    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章