JAVA六種遍歷二叉樹代碼

先根遍歷,中根遍歷,後根遍歷 二叉樹並且 循環+遞歸兩種方式

//二叉樹節點class
public class BinaryTreeNode {
    private int value;
    public BinaryTreeNode leftNode;
    public BinaryTreeNode rightNode;

    public void setValue(int v){
        this.value = v;
    }
    public int getValue(){
        return this.value;
    }

}

//創建二叉樹
public class BinaryTree {
    public static BinaryTreeNode treeRoot;
    static{
        treeRoot = new BinaryTreeNode();
        treeRoot.setValue(1);

        treeRoot.leftNode = new BinaryTreeNode();
        treeRoot.leftNode.setValue(2);

        treeRoot.rightNode = new BinaryTreeNode();
        treeRoot.rightNode.setValue(3);

        treeRoot.leftNode.leftNode = new BinaryTreeNode();
        treeRoot.leftNode.leftNode.setValue(4);
        treeRoot.leftNode.rightNode = new BinaryTreeNode();
        treeRoot.leftNode.rightNode.setValue(5);

        treeRoot.rightNode.leftNode = new BinaryTreeNode();
        treeRoot.rightNode.leftNode.setValue(6);
        treeRoot.rightNode.rightNode = new BinaryTreeNode();
        treeRoot.rightNode.rightNode.setValue(7);
    }
}

//創建帶訪問次數控制code的節點包裝類
//此類爲 後根遍歷 循環方法提供幫助, 與別的遍歷方法無關 
package Chapter2;

public class BinaryTreeNodeCode{
    private BinaryTreeNode binaryTreeNode;
    private int code;

    public BinaryTreeNodeCode( BinaryTreeNode binaryTreeNode,int code){
        this.binaryTreeNode = binaryTreeNode;
        this.code = code;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public BinaryTreeNode getBinaryTreeNode() {
        return binaryTreeNode;
    }


}


//開始進行遍歷操作
import java.util.ArrayList;
import java.util.LinkedList;

public class BinaryTreeOprate {
    public static void printTreeFirstRoot(BinaryTreeNode treeRoot){
        if(treeRoot == null){
            return;
        }
        System.out.println(treeRoot.getValue());
        printTreeFirstRoot(treeRoot.leftNode);
        printTreeFirstRoot(treeRoot.rightNode);
    }
    public static void printTreeSecoundRoot(BinaryTreeNode treeRoot){
        if(treeRoot == null){
            return;
        }
        printTreeSecoundRoot(treeRoot.leftNode);
        System.out.println(treeRoot.getValue());
        printTreeSecoundRoot(treeRoot.rightNode);
    }
    public static void printTreeThiredRoot(BinaryTreeNode treeRoot){
        if(treeRoot == null){
            return;
        }
        printTreeThiredRoot(treeRoot.leftNode);
        printTreeThiredRoot(treeRoot.rightNode);
        System.out.println(treeRoot.getValue());
    }
    public static void printTreeFirstRootFor(BinaryTreeNode treeRoot){
        if(treeRoot == null){
            return;
        }
        LinkedList<BinaryTreeNode> treeNodeStack = new LinkedList<>();
        treeNodeStack.push(treeRoot);
        while(!treeNodeStack.isEmpty()){
            BinaryTreeNode treeNode = treeNodeStack.getLast();
            treeNodeStack.removeLast();
            System.out.println(treeNode.getValue());
            if(treeNode.rightNode != null){
                treeNodeStack.addLast(treeNode.rightNode);
            }
            if(treeNode.leftNode != null){
                treeNodeStack.addLast(treeNode.leftNode);
            }


        }
    }
    public static void printTreeSecoundRootFor(BinaryTreeNode treeRoot){
        if(treeRoot == null){
            return;
        }
        LinkedList<BinaryTreeNode> treeNodeStack = new LinkedList<>();
        treeNodeStack.addLast(treeRoot);
        while(!treeNodeStack.isEmpty()){
            if(treeNodeStack.getLast().leftNode != null){
                treeNodeStack.addLast(treeNodeStack.getLast().leftNode);
            }else{
                BinaryTreeNode treeNode = treeNodeStack.getLast();
                System.out.println(treeNode.getValue());
                if(treeNode.rightNode != null){
                    treeNodeStack.addLast(treeNode.rightNode);
                }
            }

        }
    }
    /*
     * 規定訪問一個節點 所做事情代表的 號碼
     * 1 入棧(入棧之後,此節點代碼爲 1
     * 2 訪問左右子節點(如果代碼是1, 則下一步進行 訪問左右子節點,刷代碼爲2)
     * 3 彈出並且輸出 (如果代碼是2 則下一步該進行彈出棧,並且輸出值)
     */
    public static void printTreeThiredRootFor(BinaryTreeNode treeRoot) throws Exception{
        if(treeRoot == null){
            return;
        }
        LinkedList<BinaryTreeNodeCode> treeNodeStack = new LinkedList<>();
        treeNodeStack.addLast(new BinaryTreeNodeCode(treeRoot, 1));
        while(!treeNodeStack.isEmpty()){
            //如果棧頂元素的code == 1
            //把棧頂元素的code set成2
            //把棧頂元素的right節點入棧(!=null的話)並設置code=1
            //把棧頂元素的left節點入棧 (!=null的話)並設計code=1

            if(treeNodeStack.getLast().getCode() == 1){
                //獲取棧頂的元素
                BinaryTreeNodeCode treeNodeCode = treeNodeStack.getLast();
                //因爲這是第二次訪問,所以置爲2
                treeNodeCode.setCode(2);
                //將棧頂的元素左右子樹入棧,並且設置code=1
                if(treeNodeCode.getBinaryTreeNode().rightNode != null){
                    treeNodeStack.addLast(new BinaryTreeNodeCode(treeNodeCode.getBinaryTreeNode().rightNode, 1));
                }
                if(treeNodeCode.getBinaryTreeNode().leftNode != null){
                    treeNodeStack.addLast(new BinaryTreeNodeCode(treeNodeCode.getBinaryTreeNode().leftNode, 1));
                }

            //如果code == 2
            //則將棧頂元素的值輸出並且彈出
            }else if(treeNodeStack.getLast().getCode() == 2){
                System.out.println(treeNodeStack.getLast().getBinaryTreeNode().getValue());
                treeNodeStack.removeLast();
            }else{
                throw new Exception("出現code != 1  && != 2");
            }

        }
    }
    public static void main(String[] args) throws Exception {
        BinaryTreeNode treeRoot = BinaryTree.treeRoot;
        System.out.println("======先根遍歷===遞歸=====");
        printTreeFirstRoot(treeRoot);
        System.out.println("======先根遍歷===循環=====");
        printTreeFirstRootFor(treeRoot);
        System.out.println("======中根遍歷===遞歸=====");
        printTreeSecoundRoot(treeRoot);
        System.out.println("======中根遍歷===循環=====");
        printTreeSecoundRoot(treeRoot);
        System.out.println("======後根遍歷===遞歸=====");
        printTreeThiredRoot(treeRoot);
        System.out.println("======後根遍歷===循環=====");
        printTreeThiredRootFor(treeRoot);
    }
}

/////////運行結果////////////////
/*
======先根遍歷===遞歸=====
1
2
4
5
3
6
7
======先根遍歷===循環=====
1
2
4
5
3
6
7
======中根遍歷===遞歸=====
4
2
5
1
6
3
7
======中根遍歷===循環=====
4
2
5
1
6
3
7
======後根遍歷===遞歸=====
4
5
2
6
7
3
1
======後根遍歷===循環=====
4
5
2
6
7
3
1

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