數據結構之二叉樹

二叉樹

1.爲什麼需要樹這種數據結構

  • 數組存儲方式的分析�優點:通過下標方式訪問元素,速度快。對於有序數組,還可使用二分查找提高檢索速度。�缺點:如果要檢索具體某個值,或者插入值(按一定順序)會整體移動,效率較低
  • 鏈式存儲方式的分析�優點:在一定程度上對數組存儲方式有優化(比如:插入一個數值節點,只需要將插入節點,鏈接到鏈表中即可, 刪除效率也很好)。�缺點:在進行檢索時,效率仍然較低,比如(檢索某個值,需要從頭節點開始遍歷
  • 樹存儲方式的分析�能提高數據存儲,讀取的效率, 比如利用 二叉排序樹(Binary Sort Tree),既可以保證數據的檢索速度,同時也可以保證數據的插入,刪除,修改的速度。

2.樹示意圖

3.二叉樹的概念

  • 樹有很多種,每個節點最多只能有兩個子節點的一種形式稱爲二叉樹。
  • 二叉樹的子節點分爲左節點和右節點。
  • 如果該二叉樹的所有葉子節點都在最後一層,並且結點總數= 2^n -1 , n 爲層數,則我們稱爲滿二叉樹。
  • 如果該二叉樹的所有葉子節點都在最後一層或者倒數第二層,而且最後一層的葉子節點在左邊連續,倒數第二層的葉子節點在右邊連續,我們稱爲完全二叉樹。

3.1二叉樹遍歷

  • 前序遍歷: 先輸出父節點,再遍歷左子樹和右子樹

  • 中序遍歷: 先遍歷左子樹,再輸出父節點,再遍歷右子樹

  • 後序遍歷: 先遍歷左子樹,再遍歷右子樹,最後輸出父節點

  • 小結: 看輸出父節點的順序,就確定是前序,中序還是後序

3.2代碼實現

package cn.smallmartial.tree;

/**
 * @Author smallmartial
 * @Date 2019/6/15
 * @Email [email protected]
 */
public class BinaryTreeDemo {
    public static void main(String[] args) {
        //創建一個二叉樹
        BinaryTree binaryTree = new BinaryTree();
        HeroNode root = new HeroNode(1, "doodou");
        HeroNode heroNode2 = new HeroNode(2, "smallmartial");
        HeroNode heroNode3 = new HeroNode(3, "張三");
        HeroNode heroNode4 = new HeroNode(4, "李四");

        //先手動創建二叉樹
        root.setLeft(heroNode2);
        root.setRiht(heroNode3);
        heroNode3.setRiht(heroNode4);
        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.proOrder();
        }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 HeroNode(int no, String name) {
        this.no = no;
        this.name = name;
    }

    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 getRiht() {
        return right;
    }

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

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", left=" + left +
                ", riht=" + right +
                '}';
    }
    //前序遍歷
    public void proOrder(){
        System.out.println(this);
        //遞歸向左子樹前序遍歷
        if (this.left != null){
            this.left.proOrder();
        }
        //遞歸向右子樹前序遍歷
        if (this.right != null){
            this.right.proOrder();
        }
        //中序遍歷
    }

    //中序遍歷
    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);
    }

}

運行結果

3.3二叉樹-查找指定節點

  • 要求:
    請編寫前序查找,中序查找和後序查找的方法。
    並分別使用三種查找方式,查找 heroNO = 5 的節點
    並分析各種查找方式,分別比較了多少次

  • 代碼

    package cn.smallmartial.tree;
    
    /**
     * @Author smallmartial
     * @Date 2019/6/15
     * @Email [email protected]
     */
    public class BinaryTreeDemo {
        public static void main(String[] args) {
            //創建一個二叉樹
            BinaryTree binaryTree = new BinaryTree();
            HeroNode root = new HeroNode(1, "doodou");
            HeroNode heroNode2 = new HeroNode(2, "smallmartial");
            HeroNode heroNode3 = new HeroNode(3, "張三");
            HeroNode heroNode4 = new HeroNode(4, "李四");
    
            //先手動創建二叉樹
            root.setLeft(heroNode2);
            root.setRiht(heroNode3);
            heroNode3.setRiht(heroNode4);
            binaryTree.setRoot(root);
    
    //
    //        System.out.println("前序遍歷");
    //        binaryTree.preOrder();
    //
    //        System.out.println("中序遍歷");
    //        binaryTree.infixOrder();
    //
    //        System.out.println("後續序遍歷");
    //        binaryTree.postOrder();
    
            //前序遍歷
            System.out.println("前序遍歷方式");
            HeroNode resNode = binaryTree.preOrderSerch(4);
            if (resNode != null){
                System.out.println("找到了信息爲no="+resNode.getNo()+"name="+resNode.getName());
            }else {
                System.out.println("沒有找到 no ="+5);
            }
        }
    }
    //創建二叉樹
    class BinaryTree{
    
        private HeroNode root;
    
        public void setRoot(HeroNode root){
            this.root = root;
        }
        //前序遍歷
        public void preOrder(){
            if (this.root != null){
                this.root.proOrder();
            }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("二叉樹爲空,無法遍歷");
            }
        }
        //前序遍歷查找
        public HeroNode preOrderSerch(int no){
            if (root != null){
                return root.proOrderserch(no);
            }else {
                return null;
            }
        }
    
        //中序遍歷
        public HeroNode infixOrderSearch(int no){
            if (root != null){
                return root.infixOrderSearch(no);
            }else {
                return null;
            }
        }
        //後續遍歷
        public HeroNode postOrderSearch(int no ){
            if (root != null){
                return root.postOrderSerach(no);
            }else {
                return null;
            }
        }
    
    }
    
    class HeroNode{
        private int no;
        private String name;
        private HeroNode left;
        private HeroNode right;
    
        public HeroNode(int no, String name) {
            this.no = no;
            this.name = name;
        }
    
        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 getRiht() {
            return right;
        }
    
        public void setRiht(HeroNode riht) {
            this.right = riht;
        }
    
        @Override
        public String toString() {
            return "HeroNode{" +
                    "no=" + no +
                    ", name='" + name + '\'' +
                    ", left=" + left +
                    ", riht=" + right +
                    '}';
        }
        //前序遍歷
        public void proOrder(){
            System.out.println(this);
            //遞歸向左子樹前序遍歷
            if (this.left != null){
                this.left.proOrder();
            }
            //遞歸向右子樹前序遍歷
            if (this.right != null){
                this.right.proOrder();
            }
            //中序遍歷
        }
    
        //中序遍歷
        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);
        }
    
        //前序遍歷查找
        public HeroNode proOrderserch(int no){
            if (this.no == no){
                return this;
            }
    
            HeroNode resNode = null;
            if (this.left != null){
                resNode = this.left.proOrderserch(no);
            }
    
            if (resNode != null){
                return resNode;
            }
    
            if (this.right != null){
                resNode = this.right.proOrderserch(no);
            }
            return resNode;
        }
    
        //中序遍歷查找
        public  HeroNode infixOrderSearch(int no){
    
            HeroNode resNode = null;
            if (this.left != null){
                resNode = this.left.infixOrderSearch(no);
            }
    
            if (resNode != null){
                return resNode;
            }
    
            if (this.no == no){
                return this;
            }
    
            if (this.right != null){
                resNode = this.right.infixOrderSearch(no);
            }
            return resNode;
        }
    
        //後序遍歷
        public HeroNode postOrderSerach(int no ){
            HeroNode resNode = null;
            if (this.left != null){
                resNode = this.left.infixOrderSearch(no);
            }
    
            if (resNode != null){
                return resNode;
            }
            if (this.right != null){
                resNode = this.right.infixOrderSearch(no);
            }
    
            if (this.no == no){
                return this;
            }
            return resNode;
    
        }
    
    }
    
    

3.4二叉樹的刪除

  • 如果刪除的節點是葉子節點,則刪除該節點
    如果刪除的節點是非葉子節點,則刪除該子樹.
    測試,刪除掉 5號葉子節點 和 3號子樹.

  • 代碼

    package cn.smallmartial.tree;
    
    /**
     * @Author smallmartial
     * @Date 2019/6/15
     * @Email [email protected]
     */
    public class BinaryTreeDemo {
        public static void main(String[] args) {
            //創建一個二叉樹
            BinaryTree binaryTree = new BinaryTree();
            HeroNode root = new HeroNode(1, "doodou");
            HeroNode heroNode2 = new HeroNode(2, "smallmartial");
            HeroNode heroNode3 = new HeroNode(3, "張三");
            HeroNode heroNode4 = new HeroNode(4, "李四");
    
            //先手動創建二叉樹
            root.setLeft(heroNode2);
            root.setRiht(heroNode3);
            heroNode3.setRiht(heroNode4);
            binaryTree.setRoot(root);
    
    //
    //        System.out.println("前序遍歷");
    //        binaryTree.preOrder();
    //
    //        System.out.println("中序遍歷");
    //        binaryTree.infixOrder();
    //
    //        System.out.println("後續序遍歷");
    //        binaryTree.postOrder();
    
    //        //前序遍歷
    //        System.out.println("前序遍歷方式");
    //        HeroNode resNode = binaryTree.preOrderSerch(4);
    //        if (resNode != null){
    //            System.out.println("找到了信息爲no="+resNode.getNo()+"name="+resNode.getName());
    //        }else {
    //            System.out.println("沒有找到 no ="+5);
    //        }
    
            //測試刪除
    
            System.out.println("刪除前");
            binaryTree.preOrder();
            binaryTree.delNode(4);
            System.out.println("刪除後");
            binaryTree.preOrder();
        }
    }
    //創建二叉樹
    class BinaryTree{
    
        private HeroNode root;
    
        public void setRoot(HeroNode root){
            this.root = root;
        }
    
        public void delNode(int no){
            if (root != null){
                if (root.getNo() == no){
                    root = null;
                }else {
                    root.delNode(no);
                }
            }
        }
        //前序遍歷
        public void preOrder(){
            if (this.root != null){
                this.root.proOrder();
            }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("二叉樹爲空,無法遍歷");
            }
        }
        //前序遍歷查找
        public HeroNode preOrderSerch(int no){
            if (root != null){
                return root.proOrderserch(no);
            }else {
                return null;
            }
        }
    
        //中序遍歷
        public HeroNode infixOrderSearch(int no){
            if (root != null){
                return root.infixOrderSearch(no);
            }else {
                return null;
            }
        }
        //後續遍歷
        public HeroNode postOrderSearch(int no ){
            if (root != null){
                return root.postOrderSerach(no);
            }else {
                return null;
            }
        }
    
    }
    
    class HeroNode{
        private int no;
        private String name;
        private HeroNode left;
        private HeroNode right;
    
        public HeroNode(int no, String name) {
            this.no = no;
            this.name = name;
        }
    
        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 getRiht() {
            return right;
        }
    
        public void setRiht(HeroNode riht) {
            this.right = riht;
        }
    
        @Override
        public String toString() {
            return "HeroNode{" +
                    "no=" + no +
                    ", name='" + name + '\'' +
                    ", left=" + left +
                    ", riht=" + right +
                    '}';
        }
    
        //遞歸刪除節點
        public void delNode(int no){
    
            if (this.left != null &&this.left.no == no){
                this.left = null;
                return;
            }
    
            if (this.right != null && this.right.no == no){
                this.right = null;
                return;
            }
            //向左子樹遞歸刪除
            if (this.left != null){
                this.left.delNode(no);
            }
    
            //向右遞歸刪除
            if (this.right != null){
                this.right.delNode(no);
            }
    
    
        }
        //前序遍歷
        public void proOrder(){
            System.out.println(this);
            //遞歸向左子樹前序遍歷
            if (this.left != null){
                this.left.proOrder();
            }
            //遞歸向右子樹前序遍歷
            if (this.right != null){
                this.right.proOrder();
            }
            //中序遍歷
        }
    
        //中序遍歷
        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);
        }
    
        //前序遍歷查找
        public HeroNode proOrderserch(int no){
            if (this.no == no){
                return this;
            }
    
            HeroNode resNode = null;
            if (this.left != null){
                resNode = this.left.proOrderserch(no);
            }
    
            if (resNode != null){
                return resNode;
            }
    
            if (this.right != null){
                resNode = this.right.proOrderserch(no);
            }
            return resNode;
        }
    
        //中序遍歷查找
        public  HeroNode infixOrderSearch(int no){
    
            HeroNode resNode = null;
            if (this.left != null){
                resNode = this.left.infixOrderSearch(no);
            }
    
            if (resNode != null){
                return resNode;
            }
    
            if (this.no == no){
                return this;
            }
    
            if (this.right != null){
                resNode = this.right.infixOrderSearch(no);
            }
            return resNode;
        }
    
        //後序遍歷
        public HeroNode postOrderSerach(int no ){
            HeroNode resNode = null;
            if (this.left != null){
                resNode = this.left.infixOrderSearch(no);
            }
    
            if (resNode != null){
    
                return resNode;
            }
            if (this.right != null){
                resNode = this.right.infixOrderSearch(no);
            }
    
            if (this.no == no){
                return this;
            }
            return resNode;
    
        }
    
    }
    
    
  • 運行結果

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