5.1_二叉樹

【樹的基本概念】測試

 

【二叉樹】this

任何一個節點的子節點的數量不超過2。spa

二叉樹的子節點分爲左節點和右節點。code

【滿二叉樹】blog

全部的葉子節點在最後一層。遞歸

並且節點的總數爲2^n-1。get

【徹底二叉樹】class

全部葉子節點都在最後一層或者倒數第二層,且最後一層的葉子節點在左邊連續,倒數第二節的葉子節點在右邊連續。二叉樹

 

【鏈式存儲的二叉樹】遍歷

【樹的遍歷】

[ 前序遍歷 ]

根——左——右

[ 中序遍歷 ]

左——根——右

[ 後序遍歷 ]

左——右——根

【節點類TreeNode】

package com.tree.demo1;

/**
 * 二叉樹節點
 */
public class TreeNode {

    //節點的權
    private int value;

    //左兒子
    private TreeNode leftNode;

    //右兒子
    private TreeNode rightNode;


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

    /**
     * 前序遍歷
     */
    public void frontShow(){
        //1.先遍歷當前節點的內容
        System.out.println(value);
        //2.再遍歷左節點
        if(null!=leftNode){
            leftNode.frontShow();
        }
        //3.再遍歷右節點
        if(null!=rightNode){
            rightNode.frontShow();
        }
    }

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

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

    /**
     * 前序查找
     */
    public TreeNode frontSearch(int num){
        TreeNode target =null;
        if(this.value==num){
            return this;
        }else{
            if(null!=leftNode){
                target = leftNode.frontSearch(num);
            }
            if(null!=target){
                return target;
            }
            if(null!=rightNode){
                target = rightNode.frontSearch(num);
            }
            if(null!=target){
                return target;
            }
        }
        return target;
    }

    /**
     * 刪除某個節點
     */
    public void delete(int num){
        TreeNode parent =this;
        //判斷左兒子
        if(null!=parent.leftNode && num==parent.leftNode.getValue()){
            parent.setLeftNode(null);
            return;
        }
        //判斷右兒子
        if(null!=parent.rightNode && num==parent.rightNode.getValue()){
            parent.setRightNode(null);
            return;
        }
        //遞歸檢查並刪除左兒子
        if(leftNode!=null){
            parent=leftNode;
            parent.delete(num);
        }
        //遞歸檢查並刪除右兒子
        if(rightNode!=null){
            parent=rightNode;
            parent.delete(num);
        }
    }

    public int getValue() {
        return value;
    }

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

    public TreeNode getLeftNode() {
        return leftNode;
    }

    public void setLeftNode(TreeNode leftNode) {
        this.leftNode = leftNode;
    }

    public TreeNode getRightNode() {
        return rightNode;
    }

    public void setRightNode(TreeNode rightNode) {
        this.rightNode = rightNode;
    }
}

【二叉樹類 BinaryTree】

package com.tree.demo1;

/**
 * 二叉樹
 */
public class BinaryTree {

    //根節點
    private TreeNode root;

    /**
     * 前序遍歷
     */
    public void frontShow(){
        if (null!=root) {
            root.frontShow();
        }
    }

    /**
     * 中序遍歷
     */
    public void midShow(){
        root.midShow();
    }

    /**
     * 後序遍歷
     */
    public void afterShow(){
        root.afterShow();
    }

    /**
     * 前序查找
     */
    public TreeNode frontSearch(int num){
        return root.frontSearch(num);
    }

    /**
     * 刪除一個子樹
     */
    public void delete(int num){
        root.delete(num);
    }

    public TreeNode getRoot() {
        return root;
    }

    public void setRoot(TreeNode root) {
        this.root = root;
    }
}

【測試類】

package com.tree.demo1;

/**
 * 二叉樹測試類
 */
public class BinaryTreeTest {

    public static void main(String[] args) {
        //建立一棵樹
        BinaryTree tree = new BinaryTree();
        //建立一個根節點
        TreeNode rootNode = new TreeNode(1);

        //把根節點賦給樹
        tree.setRoot(rootNode);
        //建立一個左節點
        TreeNode rootL = new TreeNode(2);
        rootNode.setLeftNode(rootL);
        //建立一個右節點
        TreeNode rootR = new TreeNode(3);
        rootNode.setRightNode(rootR);

        //爲第二層的左節點建立兩個子節點
        rootL.setLeftNode(new TreeNode(4));
        rootL.setRightNode(new TreeNode(5));
        //爲第二層的右節點建立兩個子節點
        rootR.setLeftNode(new TreeNode(6));
        rootR.setRightNode(new TreeNode(7));

        //遍歷樹-前序遍歷
        tree.frontShow();
        System.out.println("==============================");

        //遍歷樹-中序遍歷
        tree.midShow();
        System.out.println("==============================");

        //遍歷樹-後序遍歷
        tree.afterShow();
        System.out.println("==============================");

        System.out.println(tree.frontSearch(5).getValue());

        System.out.println("==============================");
        tree.delete(2);
        tree.frontShow();

    }
}
相關文章
相關標籤/搜索