二叉樹

滿二叉樹:所有葉子節(jié)點都在最后一層,而且節(jié)點的總數(shù)為:2^n-1 n是樹的高度
完全二叉樹:若設(shè)二叉樹的高度為h,除第 h 層外,其它各層 (1~h-1) 的結(jié)點數(shù)都達(dá)到最大個數(shù),第 h 層從右向左連續(xù)缺若干結(jié)點,這就是完全二叉樹。
前序遍歷:父節(jié)點-左節(jié)點-右節(jié)點
中序遍歷:左節(jié)點-父節(jié)點-右節(jié)點
后序遍歷:左節(jié)點-右節(jié)點-父節(jié)點

/**
 * 二叉樹
 * @author zhongjiahong
 *
 */
public class BinaryTree {

    TreeNode root;

    //設(shè)置根節(jié)點
    public void setRoot(TreeNode root){
        this.root = root;
    }
    //獲取根節(jié)點
    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 frontSearch(int i ){
        return root.frontSearch(i);
    }
    //刪除子樹
    public void delete(int i){
        if(root.value==i){
            root=null;
        }else{
            root.delete(i);
        }
    }
}
/**
 * 二叉樹節(jié)點
 * @author zhongjiahong
 *
 */
public class TreeNode {

    //節(jié)點的權(quán)
    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(){
        //先遍歷當(dāng)前節(jié)點的內(nèi)容
        System.out.println(value);
        //左節(jié)點
        if(leftNode!=null){
            leftNode.frontShow();
        }
        //右節(jié)點
        if(rightNode!=null){
            rightNode.frontShow();
        }
    }

    //中序遍歷
    public void midShow(){
        if(leftNode!=null){
            leftNode.midShow();
        }
        System.out.println(value);
        if(rightNode!=null){
            rightNode.midShow();
        }
    }
    //后序遍歷
    public void afterShow() {
        if(leftNode!=null){
            leftNode.afterShow();
        }
        if(rightNode!=null){
            rightNode.afterShow();
        }
        System.out.println(value);
    }

    //用前序查找
    public TreeNode frontSearch(int i){
        TreeNode target = null;
        //對比當(dāng)前節(jié)點的值
        if(this.value==i){
            return this;
        //當(dāng)前節(jié)點的值不是要查找的節(jié)點
        }else{
            //查找左兒子
            if(leftNode!=null){
                //有可能可以查到,也可能查不到,查不到的話,target還是一個null
                target = leftNode.frontSearch(i);
            }
            //如果不為空,說明在左兒子中已經(jīng)找到
            if(target!=null){
                return target;
            }
            //查找右兒子
            if(rightNode!=null){
                target=rightNode.frontSearch(i);
            }
        }
        return target;
    }
    //刪除子樹
    public void delete(int i) {
        TreeNode parent = this;
        //判斷左兒子
        if(parent.leftNode!=null&&parent.leftNode.value==i){
            parent.leftNode=null;
            return;
        }
        //判斷右兒子
        if(parent.rightNode!=null&&parent.rightNode.value==i){
            parent.rightNode=null;
            return;
        }

        //遞歸檢查并刪除左兒子
        parent=leftNode;
        if(parent!=null){
            parent.delete(i);
        }

        //遞歸檢查并刪除右兒子
        parent=rightNode;
        if(parent!=null){
            parent.delete(i);
        }
    }
}

順序存儲的二叉樹通常只考慮完全二叉樹

第n個元素的左子節(jié)點是:2n+1
第n個元素的右子節(jié)點是:2
n+2
第n個元素的父節(jié)點是:(n-1)/2

把任何數(shù)組看做一棵順序存儲的二叉樹

/**
 * 順序存儲的二叉樹遍歷
 * @author zhongjiahong
 *
 */
public class ArrayBinaryTree {

    int[] data;

    public ArrayBinaryTree(int[] data){
        this.data=data;
    }

    public void frontShow(){
        frontShow(0);
    }

    //前序遍歷
    public void frontShow(int index){
        if(data==null||data.length==0){
            return;
        }
        //先遍歷當(dāng)前節(jié)點的內(nèi)容
        System.out.println(data[index]);
        //2*index+1:處理左子樹
        if(2*index+1<data.length){
            frontShow(2*index+1);
        }
        //2*index+2:處理右子樹
        if(2*index+2<data.length){
            frontShow(2*index+2);
        }
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。