1. 程式人生 > 其它 >03 idea中建立maven

03 idea中建立maven

二叉樹的前序中序後序遍歷

前序遍歷:先輸出父節點,再遍歷左子樹和右子樹
中序遍歷:先遍歷左子樹,再輸出父節點,遍歷右子樹
後續遍歷:先遍歷左子樹,再遍歷右子樹,最後輸出父節點
看輸出父節點的順序,就可以確定是前序中序還是後序
1.建立二叉樹
2.前序遍歷
	2.1.先輸出當前節點(初始時候是root節點)
	2.2.如果左子節點不為空,則遞迴繼續前序遍歷
	2.3.如果右子節點不為空,則遞迴繼續前序遍歷
3.中序遍歷
	3.1.如果左子節點不為空,則遞迴繼續中序遍歷
	3.2.輸出當前節點
	3.3.如果當前節點的右子節點不為空,則遞迴繼續中序序遍歷
4.後序遍歷
	4.1.如果左子節點不為空,則遞迴繼續後序遍歷
	4.2.如果當前節點的右子節點不為空,則遞迴繼續後序遍歷
	4.3.輸出當前節點

程式碼實現

public class BinaryTreeDemo {
    public static void main(String[] args) {
      //建立二叉樹
        BinaryTree binaryTree = new BinaryTree();
        //建立需要的節點
        HeroNode root = new HeroNode(1, "宋江");
        HeroNode node2 = new HeroNode(2, "吳用");
        HeroNode node3 = new HeroNode(3, "盧俊義");
        HeroNode node4 = new HeroNode(4, "林沖");
        HeroNode node5 = new HeroNode(5, "關勝");
        //我們先手動建立該二叉樹,後面我們學習遞迴的方式建立二叉樹
        root.setLeft(node2);
        root.setRight(node3);
        node3.setRight(node4);
        node3.setLeft(node5);

        binaryTree.setRoot(root);
        System.out.println("前序遍歷");
        binaryTree.preOrder();

        System.out.println("中序遍歷");
        binaryTree.infixOrder();

        System.out.println("後序遍歷");
        binaryTree.postOrder();
    }
}

//定義二叉樹
class BinaryTree{
    private HeroNode root;
    public void setRoot(HeroNode root){
        this.root=root;
    }

    //前序遍歷
    public void preOrder(){
        if (this.root!=null){
            this.root.PreOrder();
        }else {
            System.out.println("二叉樹為空,無法遍歷");
        }
    }

    //中序遍歷
    public void  infixOrder(){
        if (this.root!=null){
            this.root.infixOrder();
        }else {
            System.out.println("二叉樹為空,無法遍歷");
        }
    }

    //後序遍歷
    public void  postOrder(){
        if (this.root!=null){
            this.root.postOrder();
        }else {
            System.out.println("二叉樹為空,無法遍歷");
        }
    }
}

//建立節點
class HeroNode{
    private int no;
    private String name;
    private HeroNode left;
    private HeroNode right;

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public HeroNode getLeft() {
        return left;
    }

    public void setLeft(HeroNode left) {
        this.left = left;
    }

    public HeroNode getRight() {
        return right;
    }

    public void setRight(HeroNode right) {
        this.right = right;
    }

    public HeroNode(int no, String name) {
        this.no = no;
        this.name = name;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                '}';
    }

    //編寫前序遍歷的方法
    public void PreOrder(){
        System.out.println(this);//先輸出父節點
        //遞歸向左子樹前序遍歷
        if (this.left!=null){
            this.left.PreOrder();
        }
        //遞歸向右子樹前序遍歷
        if (this.right!=null){
            this.right.PreOrder();
        }
    }
    //中序遍歷
    public void infixOrder(){
        //向左子樹中序遍歷
        if (this.left!=null){
            this.left.infixOrder();
        }
        //輸出父節點
        System.out.println(this);
        //遞歸向右子樹遍歷
        if (this.right!=null){
            this.right.infixOrder();
        }
    }
    //後序遍歷
    public void postOrder(){
        if (this.left!=null){
            this.left.postOrder();
        }
        if (this.right!=null){
            this.right.postOrder();
        }
        System.out.println(this);
    }
}