二叉樹前序中序後序遍歷及節點的查找

代碼展示

package demo5;

public class BinaryTree {

    TreeNode root;

    //設置根節點
    public void setRoot(TreeNode root){
        this.root = root;
    }

    //獲取根節點
    public TreeNode getRoot(){
        return root;
    }

    public void frontShow() {
    	if(root!=null){
             root.frontShow();
        }
    }

    public void midShow() {
         if(root!=null){
             root.midShow();
        }
    }

    public void afterShow() {
        if(root!=null){
             root.afterShow();
        }
    }


    public TreeNode preSearch(int i) {
        return root.preSearch(i);
    }

    public TreeNode midSearch(int i) {
        return root.midSearch(i);
    }

    public TreeNode afterSearch(int i) {
        return root.afterSearch(i);
    }
}
package demo5;

public class TreeNode {
    //節點的權
    int value;
    //左兒子
    TreeNode leftNode;
    //右兒子
    TreeNode rightNode;

    public TreeNode(int value){
        this.value = value;
    }

    //設置左兒子
    public void setLeftNode(TreeNode leftNode) {
        this.leftNode = leftNode;
    }

    //設置右兒子
    public void setRightNode(TreeNode rightNode) {
        this.rightNode = rightNode;
    }

    //前序遍歷
    public void frontShow() {
        //先遍歷當前節點的內容
        System.out.print(value+" ");
        //左節點
        if(leftNode!=null){
            leftNode.frontShow();
        }
        //右節點
        if(rightNode!=null){
            rightNode.frontShow();
        }
    }

    //中序遍歷
    public void midShow() {
        //左節點
        if(leftNode!=null){
            leftNode.midShow();
        }
        //當前節點
        System.out.print(value+" ");
        //右節點
        if(rightNode!=null){
            rightNode.midShow();
        }
    }

    //後序遍歷
    public void afterShow() {
        //左節點
        if(leftNode!=null){
            leftNode.afterShow();
        }
        //右節點
        if(rightNode!=null){
            rightNode.afterShow();
        }
        //當前節點
        System.out.print(value+" ");
    }

    //前序查找
    public TreeNode preSearch(int i) {
        TreeNode target = null;
        //對比當前節點的值
        if(this.value == i){
            return this;
        //當前節點的值不是要查找的節點
        }else{
            //查找左兒子
            if(leftNode!=null){
                //有可能可以查到,也可能查不到,查不到的話,target還是一個null
                target = leftNode.preSearch(i);
            }
            //如果不爲空,說明在左兒子中已經找到
            if(target!=null){
                return target;
            }
            //查找右兒子
            if(rightNode!=null){
                target = rightNode.preSearch(i);
            }
        }
        return target;
    }

    //中序查找
    public TreeNode midSearch(int i) {
        TreeNode target = null;
        //查找左兒子
        if(leftNode!=null){
            //有可能可以查到,也可能查不到,查不到的話,target還是一個null
            target = leftNode.midSearch(i);
        }
        //如果不爲空,說明在左兒子中已經找到
        if(target!=null){
            return target;
        }
        //對比當前節點的值
        if(this.value==i){
            return this;
        }
        //查找右兒子
        if(rightNode!=null){
            target = rightNode.midSearch(i);
        }
        return target;
    }

    //後序查找
    public TreeNode afterSearch(int i) {
        TreeNode target = null;
        //查找左兒子
        if(leftNode!=null){
            //有可能查到,有可能查不到,查不到的話,target還是一個null
            target = leftNode.afterSearch(i);
        }
        //如果不爲null,說明在左兒子中已經找到
        if(target!=null){
            return target;
        }
        //查找右兒子
        if(rightNode!=null){
            target = rightNode.afterSearch(i);
        }
        //如果target不爲空,說明在右兒子中已經找到
        if(target!=null){
            return target;
        }
        //對比當前節點的值
        if(this.value==i){
            return this;
        }
        return null;
    }
}
package demo5;

public class TestBinaryTree {

    public static void main(String[] args) {
        //創建一棵樹
        BinaryTree binaryTree = new BinaryTree();
        //創建一個根節點
        TreeNode root = new TreeNode(1);
        //把根節點賦給樹
        binaryTree.setRoot(root);
        //創建一個左節點
        TreeNode rootL = new TreeNode(2);
        //把新創建的節點設置爲根節點的子節點
        root.setLeftNode(rootL);
        //創建一個右節點
        TreeNode rootR = new TreeNode(3);
        //把新創建的節點設置爲根節點的子節點
        root.setRightNode(rootR);
        //爲第二層的左節點創建兩個子節點
        rootL.setLeftNode(new TreeNode(4));
        rootL.setRightNode(new TreeNode(5));
        //爲第二層的右節點創建兩個子節點
        rootR.setLeftNode(new TreeNode(6));
        rootR.setRightNode(new TreeNode(7));
        //前序遍歷樹
        System.out.print("前序遍歷:");
        binaryTree.frontShow();
        System.out.println();
        //中序遍歷樹
        System.out.print("中序遍歷:");
        binaryTree.midShow();
        System.out.println();
        //後序遍歷樹
        System.out.print("後序遍歷:");
        binaryTree.afterShow();
        System.out.println();
        //前序查找
        System.out.println("========================");
        System.out.println("前序查找:");
        TreeNode res1 = binaryTree.preSearch(3);
        System.out.println(res1);
        //中序查找
        System.out.println("中序查找:");
        TreeNode res2 = binaryTree.midSearch(5);
        System.out.println(res2);
        //後序查找
        System.out.println("後序查找:");
        TreeNode res3 = binaryTree.afterSearch(3);
        System.out.println(res3);
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章