二叉樹的遍歷使用Java實現

import java.util.Stack;

/**
 * Created by ***** on 2017/8/21.
 */
public class BinaryTreeSeek {
    public static void main(String[] agrs){
        BinarySortTree b = new BinarySortTree();
        b.val = 5;
        b.left_child = new BinarySortTree();
        b.right_child = new BinarySortTree();
        b.left_child.val = 3;
        b.right_child.val = 7;
        b.left_child.left_child = new BinarySortTree();
        b.left_child.right_child = new BinarySortTree();
        b.left_child.left_child.val = 2;
        b.left_child.right_child.val = 4;
        b.right_child.left_child = new BinarySortTree();
        b.right_child.right_child = new BinarySortTree();
        b.right_child.left_child.val = 6;
        b.right_child.right_child.val = 8;

//        PreOrder(b);
//        System.out.println();
//        PreOrder2(b);
//        System.out.println();
//        InOrder(b);
//        System.out.println();
//        InOrder2(b);
//        System.out.println();
//        PostOrder(b);
//        System.out.println();
        PostOrder2(b);
    }

    /**
     * 二叉樹遞歸前序遍歷
     * @param tree
     */
    public static void PreOrder(BinarySortTree tree){
        if (tree != null){
            System.out.print(tree.val+" ");
            PreOrder(tree.left_child);
            PreOrder(tree.right_child);
        }else {
            return;
        }
    }

    /**
     * 二叉樹前序遍歷
     * @param tree
     */
    public static void PreOrder2(BinarySortTree tree){
        Stack<BinarySortTree> stack = new Stack<>();
        while(tree != null || !stack.empty()){
            if (tree != null){
                stack.push(tree);
                System.out.print(tree.val+" ");
                tree = tree.left_child;
            }else {
                tree = stack.pop();
                tree = tree.right_child;
            }

        }
    }


    /**
     * 二叉樹遞歸中序遍歷
     * @param tree
     */
    public static void InOrder(BinarySortTree tree){
        if (tree != null){
            InOrder(tree.left_child);
            System.out.print(tree.val+" ");
            InOrder(tree.right_child);
        }else {
            return;
        }
    }

    /**
     * 二叉樹中序遍歷
     * @param tree
     */
    public static void InOrder2(BinarySortTree tree){
        Stack<BinarySortTree> stack = new Stack<>();
        while(tree != null || !stack.empty()){
            if (tree != null){
                stack.push(tree);
                tree = tree.left_child;
            }else {
                tree = stack.pop();
                System.out.print(tree.val+" ");
                tree = tree.right_child;
            }

        }
    }

    /**
     * 二叉樹遞歸後序遍歷
     * @param tree
     */
    public static void PostOrder(BinarySortTree tree){
        if (tree != null){
            PostOrder(tree.left_child);
            PostOrder(tree.right_child);
            System.out.print(tree.val+" ");
        }else {
            return;
        }
    }

    /**
     * 二叉樹後序遍歷 需要記錄上一次彈出的節點
     * @param tree
     */
    public static void PostOrder2(BinarySortTree tree){
        Stack<BinarySortTree> stack = new Stack<>();
        BinarySortTree lastview = null;
        while(tree != null || !stack.empty()){
            if (tree != null){
                stack.push(tree);
                tree = tree.left_child;
            }else {
                tree = stack.pop();
                if (tree.right_child == null || tree.right_child == lastview){
                    //如果無右節點或者該節點的右節點已經訪問過
                    System.out.print(tree.val+" ");
                    lastview = tree;
                    tree = null;
                }else {
                    stack.push(tree);//重新入棧有右孩子的改節點
                    tree = tree.right_child;
                }

            }

        }
    }
}

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