【数据结构】——二叉树详解

1、概念

一颗二叉树是结点的一个有限集合,该集合或是为空、或是由一个根结点加上两颗别称为左子树右子树的二叉树组成的:

  1. 二叉树不存在度大于二的结点
  2. 二叉树的子树由左右之分,次序不能颠倒,因此二叉树是有序树。

 下图中都是二叉树:

有两种特殊的二叉树:

  1. 满二叉树: 一棵二叉树,如果每层的结点数都达到最大值,则这棵二叉树就是满二叉树
  2. 完全二叉树:对于深度为K的,有n个结点的二叉树,当且仅当其每一个结点都与深度为K的满二叉树中编号从0至n-1的结点一一对应时称之为完全二叉树

 2、二叉树的性质

  1. 如果规定根结点的层数为1,则一颗非空二叉树的第n层上最多有2^(n-1)  (n > 0)个结点。
  2. 如果规定只有根结点的二叉树的深度为1,则深度为K的二叉树的最大结点数是2k-1 (k>=0)。
  3. 对一棵二叉树, 如果其叶结点个数为 n0, 度为2的非叶结点个数为 n2,则有n0=n2+1。
  4. 具有n个结点的完全二叉树的深度k为 上取整。
  5. 对于具有n个结点的完全二叉树,如果按照从上至下从左至右的顺序对所有节点从0开始编号,则对于序号为i的结点有:

 例题:

3、二叉树的遍历

public class BinaryTree {
    static class TreeNode {
        public char val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(char val) {
            this.val = val;
        }
    }

    //这棵树的根结点
    public TreeNode root;


    public void createTree() {
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');
        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        E.right = H;
        this.root = A;
    }

    // 前序遍历
    void preOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        System.out.print(root.val + " ");
        preOrder(root.left);
        preOrder(root.right);
    }

    // 中序遍历
    void inOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        inOrder(root.left);
        System.out.print(root.val + " ");
        inOrder(root.right);
    }

    //后序遍历
    void postOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.val + " ");
    }
}

4、二叉树的基本操作

public class BinaryTree {
    static class TreeNode {
        public char val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(char val) {
            this.val = val;
        }
    }

    //这棵树的根结点
    public TreeNode root;


    public void createTree() {
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');
        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        E.right = H;
        this.root = A;
    }
    //获取树中结点的个数  遍历思路 nodeSize++
    public static int nodeSize;
    public void size2(TreeNode root){
        if (root == null){
            return;
        }
        nodeSize++;
        size2(root.left);
        size2(root.right);
    }
    //子问题思路  获取树中结点的个数
    int size(TreeNode root){
        if (root == null){
            return 0;
        }
        return size(root.left) + size(root.right) + 1;
    }
    //获取叶子结点的个数 子问题思路
    int getLeafNodeCount1(TreeNode root){
        if (root == null){
            return 0;
        }
        if (root.left == null && root.right == null){
            return 1;
        }
        return getLeafNodeCount1(root.left) + getLeafNodeCount1(root.right);
    }
    //遍历思路
    public static int leafSize;
    void getLeafNodeCount2(TreeNode root){
        if (root == null){
            return;
        }
        if (root.left == null && root.right == null){
            leafSize++;
        }
        getLeafNodeCount2(root.left);
        getLeafNodeCount2(root.right);
    }
    // 获取第K层节点的个数
    int getKLevelNodeCount(TreeNode root,int k){
        if(root == null){
            return 0;
        }
        if (k == 1){
            return 1;
        }
        return getKLevelNodeCount(root.left,k-1) + getKLevelNodeCount(root.right,k-1);
    }
    // 获取二叉树的高度
    int getHeight(TreeNode root){
        if (root == null){
            return 0;
        }
        //int leftHeight = getHeight(root.left);
        //int rightHeight = getHeight(root.right);
        return getHeight(root.left) > getHeight(root.right) ?
                getHeight(root.left) + 1 : getHeight(root.right) + 1;
    }
    // 检测值为value的元素是否存在
    TreeNode find(TreeNode root, int val){
        if (root == null){
            return null;
        }
        if (root.val == val){
            return root;
        }
        TreeNode ret1 = find(root.left,val);
        if (ret1 != null){
            return ret1;
        }
        TreeNode ret2 = find(root.right,val);
        if (ret2 != null){
            return ret2;
        }
        return null;
    }
    //层序遍历
    void levelOrder(TreeNode root){
        if (root == null){
            return;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()){
            TreeNode cur = queue.poll();
            System.out.print(cur.val+" ");
            if (cur.left != null){
                queue.offer(cur.left);
            }
            if (cur.right != null){
                queue.offer(cur.right);
            }
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值