數據結構--二叉樹(Java)


數據結構--二叉樹(Java)

博客說明

文章所涉及的資料來自互聯網整理和個人總結,意在於個人學習和經驗匯總,如有什么地方侵權,請聯系本人刪除,謝謝!

樹的常用術語(結合示意圖理解)

image-20200729224821077

  • 節點
  • 根節點
  • 父節點
  • 子節點
  • 葉子節點 (沒有子節點的節點)
  • 節點的權(節點值)
  • 路徑(從root節點找到該節點的路線)
  • 子樹
  • 樹的高度(最大層數)
  • 森林 :多顆子樹構成森林

樹存儲方式優勢

能提高數據存儲,讀取的效率, 比如利用 二叉排序樹(Binary Sort Tree),既可以保證數據的檢索速度,同時也可以保證數據的插入,刪除,修改的速度

二叉樹的概念

  • 每個節點最多只能有兩個子節點的一種形式稱為二叉樹

    image-20200729224615068

  • 二叉樹的子節點分為左節點和右節點

  • 如果該二叉樹的所有葉子節點都在最后一層,並且結點總數= 2^n -1 , n 為層數,則我們稱為滿二叉樹。

    image-20200729224725059

  • 如果該二叉樹的所有葉子節點都在最后一層或者倒數第二層,而且最后一層的葉子節點在左邊連續,倒數第二層的葉子節點在右邊連續,我們稱為完全二叉樹

    image-20200729224756637

遍歷

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

代碼

package cn.guizimo.tree;

/**
 * @author guizimo
 * @date 2020/7/29 8:03 下午
 */
public class TreeDemo {
    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, "林沖");
        HeroNode node6 = new HeroNode(6, "魯智深");

        //創建二叉樹
        root.setLeft(node2);
        root.setRight(node3);
        node2.setLeft(node4);
        node3.setLeft(node5);
        node3.setRight(node6);
        binaryTree.setRoot(root);

        //前序遍歷
//        HeroNode heroNode = binaryTree.preOrderSearch(5);
//        System.out.println(heroNode);
    }


}


/**
 * 二叉樹
 */
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);
            }
        } else {
            System.out.println("二叉樹為空");
        }
    }

    //前序
    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("二叉樹為空");
        }
    }

    //前序查找
    public HeroNode preOrderSearch(int no) {
        if (root != null) {
            return root.preOrderSearch(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 this.root.postOrderSearch(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 getRight() {
        return right;
    }

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

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

    //刪除節點
    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 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);
    }

    //前序遍歷查找
    public HeroNode preOrderSearch(int no) {
        if (this.no == no) {
            return this;
        }
        HeroNode resNode = null;
        //判斷左子樹
        if (this.left != null) {
            resNode = this.left.preOrderSearch(no);
        }
        if (resNode != null) {
            return resNode;
        }
        //判斷右子樹
        if (this.right != null) {
            resNode = this.right.preOrderSearch(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 postOrderSearch(int no) {
        HeroNode resNode = null;
        if (this.left != null) {
            resNode = this.left.postOrderSearch(no);
        }
        if (resNode != null) {
            return resNode;
        }
        if (this.right != null) {
            resNode = this.right.postOrderSearch(no);
        }
        if (resNode != null) {
            return resNode;
        }
        if (this.no == no) {
            return this;
        }
        return resNode;
    }
}

感謝

尚硅谷

萬能的網絡

以及勤勞的自己

關注公眾號: 歸子莫,獲取更多的資料,還有更長的學習計划


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM