当前位置: 首页 > article >正文

Java:数据结构-二叉树

1.二叉树的概念:

二叉树是一种数据结构,其中每个节点最多有两个子节点,称为左子节点和右子节点。二叉树可以用于各种算法和数据操作

注:子树之间不能有交集,否则就不是树形结构,只能有一个父树。

2.二叉树的基本属性:

  • 节点 (Node): 二叉树中的每个元素称为节点。每个节点包含数据,以及指向其左子节点和右子节点的指针。

  • 根节点 (Root): 二叉树的顶点节点,通常称为根节点。一个二叉树只有一个根节点。

  • 子节点 (Child Node): 根节点下面的节点称为子节点,一个节点可以有左子节点和右子节点。

  • 叶节点 (Leaf Node): 没有子节点的节点称为叶节点。

  • 父节点 (Parent Node): 具有子节点的节点称为其子节点的父节点。

  • 深度 (Depth): 指从根节点到某个节点所经过的边的数量。根节点的深度为 0。

  • 高度 (Height): 从某个节点到叶节点的最长路径的边数。叶节点的高度为 0。

  • 层 (Level): 树的层数由根节点开始计算,根节点是第 1 层,它的子节点是第 2 层,依此类推。

3.两种特殊的二叉树

1.满二叉树

如果每层的结点数都达到最大值,则这棵二叉树就是满二叉树

2.完全二叉树

 

完全二叉树是效率很高的数据结构,有n 个结点的二叉树,当且仅当其每一个结点都与深度为K的满二叉树中编号从0至n-1的结点一一对应时称之为完 比特就业课 全二叉树。

4.二叉树的性质:

5.简单的创建一个二叉树

public class BinaryTree {
    static class TreeNode{
        public TreeNode left;
        public TreeNode right;
        public int val;
        public TreeNode(int val){
            this.val=val;
        }
    }
    public TreeNode creatTree(){
        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;
        return A;
    }
}

6.二叉树的遍历

1.前序遍历

先遍历根,再遍历左子树,最后遍历右子树 

public void preOrder(TreeNode root){
        if(root==null){
            return;
        }
        System.out.println(root.val+" ");
        preOrder(root.left);
        preOrder(root.right);
    }
public List<Character> preOrder1(TreeNode root){
        List<Character> list=new ArrayList<>();
        if(root==null){
            return list;
        }
        list.add(root.val);
        List<Character> treeLeft=preOrder1(root.left);
        list.addAll(treeLeft);
        List<Character> treeRight=preOrder1(root.right);
        list.addAll(treeRight);
        return list;
    }

2.中序遍历

先遍历左子树,再遍历根,最后遍历右子树 

public void inOrder(TreeNode root){
        if(root==null){
            return;
        }
        preOrder(root.left);
        System.out.println(root.val+" ");
        preOrder(root.right);
    }

3.后序遍历 

先遍历左子树,再遍历右子树,最后遍历根

public void postOrder(TreeNode root){
        if(root==null) {
            return;
        }
        preOrder(root.left);
        preOrder(root.right);
        System.out.println(root.val+" ");
    }

4.层次遍历

7.二叉树的基本操作:

1.获取树中节点的个数

 public static int size(TreeNode root){
        if(root==null){
            return -1;
        }
        usedSize++;
        size(root.left);
        size(root.right);
        return usedSize;
    }
public int size1(TreeNode root){
        if(root==null){
            return -1;
        }
        return size(root.left)+size(root.right)+1;
    }

2.获取叶子节点的个数

整棵树的叶子等于左树的叶子加上右树的叶子

public void getLeafNodeCount(TreeNode root){
        if (root==null){
            return;
        }
        if(root.left==null && root.right==null){
            leftCount++;
        }
        getLeafNodeCount(root.left);
        getLeafNodeCount(root.right);

    }
public int getLeafNodeCount1(TreeNode root){
        if (root==null){
            return -1;
        }
        if(root.left==null && root.right==null){
            return 1;
        }
        return getLeafNodeCount1(root.left)+getLeafNodeCount1(root.right);
    }

 3.获取第K层节点的个数

public int getLLeaveNodeCount(TreeNode root,int k){
        if(root==null){
            return -1;
        }
        if(k==1){
            return 1;
        }
        return getLLeaveNodeCount(root.left,k-1)+getLLeaveNodeCount(root.right,k-1);
    }

4.获取二叉树的高度

 public int getHeight(TreeNode root){
        if(root==null){
            return -1;
        }
        
        return Math.max(getHeight(root.left),getHeight(root.right))+1;

    }

5.检测值为value的元素是否存在 

public TreeNode find(TreeNode root, char val){
        if(root==null){
            return null;
        }
        if(root.val==val){
            return root;
        }
        TreeNode left=find(root.left,val);
        if(left!=null){
            return left;
        }
        TreeNode right=find(root.right,val);
        if(right!=null){
            return right;
        }
        return null;
    }

6.层序遍历 

public void levelOrder(TreeNode root){
        if(root==null){
            return;
        }
        Queue<TreeNode> queue=new LinkedList<>();
        queue.offer(root);
        while (queue.isEmpty()){
            TreeNode cur=queue.poll();
            if(cur.left!=null){
                queue.offer(cur.left);
            }
            if(cur.right!=null){
                queue.offer(cur.right);
            }
            
        }
        System.out.println();
    }

7. 判断一个二叉树是不是完全二叉树

public boolean isCompleteTree(TreeNode root){
        if(root==null){
            return true;
        }
        Queue<TreeNode> queue=new LinkedList<>();
        queue.offer(root);
        while (queue.isEmpty()){
            TreeNode cur=queue.poll();
            if(cur.left!=null){
                queue.offer(cur.left);
                queue.offer(cur.right);
            }else {
                break;
            }
        }
        while (!queue.isEmpty()){
            TreeNode pek= queue.peek();
            if(pek!=null){
                return false;
            }
            queue.poll();
        }
        return true;
    }

希望能对大家有所帮助!!!!!!!!


http://www.kler.cn/news/365307.html

相关文章:

  • 【ArcGIS Pro实操第8期】绘制WRF三层嵌套区域
  • 利用移动式三维扫描技术创建考古文物的彩色纹理网格【上海沪敖3D】
  • OpenCV与AI深度学习 | 实战 | OpenCV中更稳更快的找圆方法--EdgeDrawing使用演示(详细步骤 + 代码)
  • 【进阶OpenCV】 (19)-- Dlib库 --人脸表情识别
  • webpack面试笔记(一)
  • 使用 v-html 指令渲染的标签, 标签内绑定的 click 事件不生效
  • pta-java-6-1 jmu-Java-04面向对象进阶-01-接口-匿名内部类ActionListener
  • SpringBoot实现mysql多数据源配置(Springboot+Mybatis)
  • 模拟信号采集显示器+GPS同步信号发生器制作全过程(焊接、问题、代码、电路)
  • Java调用大模型 - Spring AI 初体验
  • [ 钓鱼实战系列-基础篇-8 ] 一篇文章教会你选择适合的钓鱼项目并设计钓鱼页面
  • 富格林:曝光阻止欺诈套路攻略
  • 利用移动式三维扫描技术创建考古文物的彩色纹理网格【上海沪敖3D】
  • Java基础第二天(实训学习整理资料(二))
  • 【纯血鸿蒙】HarmonyOS Emulator(实操亲测版)
  • java 17天 TreeSet以及Collections
  • 昇思MindSpore进阶教程--安装常见问题(上)
  • 炸了!改进Transformer!Transformer-BiGRU多变量回归预测(Matlab)
  • 机器学习与神经网络的当下与未来
  • LoadBalancer 类型的 Service工作期间,kube-proxy做了什么?
  • ctfshow(262,264)--反序列化漏洞--字符串逃逸
  • LeetCode Hot 100:图论
  • 昇思MindSpore进阶教程--三方硬件对接
  • Windchill性能优化篇之分页查询
  • 操作系统笔记(二)进程,系统调用,I/O设备
  • 使用LangGraph构建多Agent系统架构!