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

【数据结构】用Java实现一棵二叉树

目录

前言

1. 创建MyBinaryTree类

2. 从前序与中序遍历序列构造二叉树

3. 从中序与后序遍历序列构造二叉树

4. 用层序遍历验证二叉树是否构建成功

5. 整体代码(构建二叉树、二叉树的基本功能和测试代码)

6. 测试结果


前言

前面两篇文章已经给出了如何构建二叉树以及如何实现基本功能,感兴趣的友友可以点下面的链接看一看,在这里给出构建二叉树的简单说明以及构建二叉树和实现基本功能实现的代码。

二叉树的构建

二叉树的基本操作

    // 前序遍历void preOrder(TreeNode root);  // 中序遍历void inOrder(TreeNode root);// 后序遍历void postOrder(TreeNode root);// 获取树中节点的个数:遍历思路public static int nodeSize;void size(TreeNode root);// 获取节点的个数:子问题的思路int size2(TreeNode root);//获取叶子节点的个数:遍历思路public static int leafSize = 0;void getLeafNodeCount1(TreeNode root);// 获取叶子节点的个数:子问题int getLeafNodeCount2(TreeNode root);// 获取第K层节点的个数int getKLevelNodeCount(TreeNode root, int k);// 获取二叉树的高度,时间复杂度:O(N)int getHeight(TreeNode root);// 检测值为value的元素是否存在TreeNode find(TreeNode root, char val);//层序遍历void levelOrder(TreeNode root);// 判断一棵树是不是完全二叉树boolean isCompleteTree(TreeNode root);

1. 创建MyBinaryTree类

val存储二叉树节点的值,left为二叉树的左子树,right为二叉树右子树地址。

下面的所有方法都是在MyBinaryTree类中实现的。

public class MyBinaryTree {static class TreeNode {public int val;TreeNode left;//左孩子的引用TreeNode right;//右孩子的引用public TreeNode(int val) {this.val = val;}}}

2. 从前序与中序遍历序列构造二叉树

从前序遍历中获取根节点,中序遍历中根节点前面的是左子树的部分,中序遍历中根节点后面的是右子树部分。使用index下标来遍历前序数组。

1. 从前序遍历结果中获取到树的根节点

2. 在中序遍历结果中确定根节点的位置,按照该位置将中序遍历结果分为两部分

        左半部分是根节点的左子树,递归创建根节点的左子树

        右半部分是根节点的右子树,递归创建根节点的右子树

public TreeNode buildTree1(int[] preorder, int[] inorder) {return buildTreeHelper1(preorder,inorder,0,inorder.length - 1);}int index = 0;private TreeNode buildTreeHelper1(int[] preorder, int[] inorder, int left, int right) {if(index == inorder.length ){return null;}if (left > right){return null;}int pos = find(inorder,preorder);TreeNode root = new TreeNode(preorder[index]);index ++;root.left = buildTreeHelper1(preorder,inorder,left,pos - 1);root.right = buildTreeHelper1(preorder,inorder,pos + 1,right);return root;}private int find(int[] inorder,int[] preorder) {for (int i = 0; i < inorder.length; i++) {if (inorder[i] == preorder[index]){return i;}}return -1;}

3. 从中序与后序遍历序列构造二叉树

先看看同一颗树的前序遍历结果preorder = [3,9,20,15,7]和后序遍历的结果postorder = [9,15,7,20,3],对比一下这两。

发现将后序遍历的结果反转->[3,20,7,15,9],前序遍历是根左右,后序遍历反转后的是根右左,所以我们只需要将后序遍历的结果反转一下,然后向上一题那样构建二叉树,只不过是先构建右子树再构建左子树。

//    从中序与后序遍历序列构造二叉树, 返回这棵树的根节点public TreeNode buildTree2(int[] postorder, int[] inorder) {reverse(postorder);return buildTreeHelper2(postorder,inorder,0,inorder.length - 1);}private void reverse(int[] postorder){for (int i = 0; i < postorder.length/2; i++) {int temp = postorder[i];postorder[i] = postorder[postorder.length - i - 1];postorder[postorder.length - i - 1] = temp;}}private TreeNode buildTreeHelper2(int[] postorder, int[] inorder, int left, int right) {if(index == inorder.length ){return null;}if (left > right){return null;}int pos = find(inorder,postorder);TreeNode root = new TreeNode(postorder[index]);index ++;root.right = buildTreeHelper2(postorder,inorder,pos + 1,right);root.left = buildTreeHelper2(postorder,inorder,left,pos - 1);return root;}

4. 用层序遍历验证二叉树是否构建成功

1. 如果是空树直接返回

2. 层序遍历需要用到队列,定义一个队列,里面放置节点的地址,将根节点如队列

3. 队列非空时,循环进行一下操作:

        a. 队列中当前元素都是在同一层的,依次取出遍历,保存到同一个curList中

        取到一个节点时候,

                >> 保存该节点

                >> 如果该节点左子树存在,将该左子树入队列

                >> 如果该节点右子树存在,将该节点右子树入队列

                 >> 将当前已遍历节点从队列中拿出来

         b. 本层节点遍历结束后,保存到返回的curList中,此时下一层节点已经全部入队列

public void levelOrder(TreeNode root) {List<List<Integer>> list = new ArrayList<>();if (root == null){System.out.println(list);return;}Deque<TreeNode> queue = new LinkedList<>();queue.offer(root);while (!queue.isEmpty()){int size = queue.size();List<Integer> curList = new ArrayList<>();for (int i = 0; i < size; i++) {TreeNode temp = queue.pop();curList.add(temp.val);if (temp.left != null){queue.offer(temp.left);}if (temp.right != null){queue.offer(temp.right);}}list.add(curList);}System.out.println(list);}

5. 整体代码(构建二叉树、二叉树的基本功能和测试代码)

构建的二叉树:

import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;public class MyBinaryTree {static class TreeNode {public int val;TreeNode left;//左孩子的引用TreeNode right;//右孩子的引用public TreeNode(int val) {this.val = val;}}//    从前序与中序遍历序列构造二叉树, 返回这棵树的根节点public TreeNode buildTree1(int[] preorder, int[] inorder) {return buildTreeHelper1(preorder,inorder,0,inorder.length - 1);}int index = 0;private TreeNode buildTreeHelper1(int[] preorder, int[] inorder, int left, int right) {if(index == inorder.length ){return null;}if (left > right){return null;}int pos = find(inorder,preorder);TreeNode root = new TreeNode(preorder[index]);index ++;root.left = buildTreeHelper1(preorder,inorder,left,pos - 1);root.right = buildTreeHelper1(preorder,inorder,pos + 1,right);return root;}private int find(int[] inorder,int[] preorder) {for (int i = 0; i < inorder.length; i++) {if (inorder[i] == preorder[index]){return i;}}return -1;}//    从中序与后序遍历序列构造二叉树, 返回这棵树的根节点public TreeNode buildTree2(int[] postorder, int[] inorder) {reverse(postorder);return buildTreeHelper2(postorder,inorder,0,inorder.length - 1);}private void reverse(int[] postorder){for (int i = 0; i < postorder.length/2; i++) {int temp = postorder[i];postorder[i] = postorder[postorder.length - i - 1];postorder[postorder.length - i - 1] = temp;}}private TreeNode buildTreeHelper2(int[] postorder, int[] inorder, int left, int right) {if(index == inorder.length ){return null;}if (left > right){return null;}int pos = find(inorder,postorder);TreeNode root = new TreeNode(postorder[index]);index ++;root.right = buildTreeHelper2(postorder,inorder,pos + 1,right);root.left = buildTreeHelper2(postorder,inorder,left,pos - 1);return root;}//    层序遍历
//    用数组输出层序遍历结果public void levelOrder(TreeNode root) {List<List<Integer>> list = new ArrayList<>();if (root == null){System.out.println(list);return;}Deque<TreeNode> queue = new LinkedList<>();queue.offer(root);while (!queue.isEmpty()){int size = queue.size();List<Integer> curList = new ArrayList<>();for (int i = 0; i < size; i++) {TreeNode temp = queue.pop();curList.add(temp.val);if (temp.left != null){queue.offer(temp.left);}if (temp.right != null){queue.offer(temp.right);}}list.add(curList);}System.out.println(list);}
//    直接输出结果
//    void levelOrder(TreeNode root) {
//        if (root == null){
//            System.out.println("这是颗空树!!!");
//            return;
//        }
//        Deque<TreeNode> queue = new LinkedList<>();
//        queue.offer(root);
//        while (!queue.isEmpty()){
//            TreeNode cur = queue.pop();
//            System.out.print(cur.val + " ");
//            if (cur.left != null){
//                queue.offer(cur.left);
//            }
//            if (cur.right != null){
//                queue.offer(cur.right);
//            }
//        }
//    }// 前序遍历public 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 + " ");}public static int nodeSize;/*** 获取树中节点的个数:遍历思路*/void size(TreeNode root) {if (root == null){return;}nodeSize ++;size(root.left);size(root.right);}/*** 获取节点的个数:子问题的思路*/int size2(TreeNode root) {if (root == null) return 0;return size2(root.left) + size2(root.right) + 1;}/*获取叶子节点的个数:遍历思路*/public static int leafSize = 0;void getLeafNodeCount1(TreeNode root) {if(root == null){return;}if (root.left == null && root.right == null){leafSize ++;}getLeafNodeCount1(root.left);getLeafNodeCount1(root.right);}/*获取叶子节点的个数:子问题*/int getLeafNodeCount2(TreeNode root) {if (root == null) return 0;if (root.left == null && root.right == null) {return 1;}return getLeafNodeCount2(root.left) + getLeafNodeCount2(root.right);}/*获取第K层节点的个数*/int getKLevelNodeCount(TreeNode root, int k) {if (root == null || k <= 0){return 0;}if (k == 1){return 1;}return getKLevelNodeCount(root.left,k - 1) + getKLevelNodeCount(root.right,k - 1);}/*获取二叉树的高度时间复杂度:O(N)*/int getHeight(TreeNode root) {if (root == null){return 0;}if(root.left == null && root.right == null){return 1;}return 1 + Math.max(getHeight(root.left),getHeight(root.right));}// 检测值为value的元素是否存在TreeNode find(TreeNode root, char val) {if (root == null){return null;}if (root.val == val){return root;}TreeNode node = find(root.left,val);if (node != null){return node;}return find(root.right,val);}// 判断一棵树是不是完全二叉树boolean isCompleteTree(TreeNode root) {Deque<TreeNode> queue = new LinkedList<>();queue.offer(root);boolean isStep1 = true;while (!queue.isEmpty()){TreeNode node = queue.poll();if(isStep1){if(node.left != null && node.right != null){queue.offer(node.left);queue.offer(node.right);} else if (node.left != null) {queue.offer(node.left);isStep1 = false;} else if (node.right != null){return false;}else {isStep1 = false;}}else {if(node.left != null || node.right != null){return false;}}}return true;}public static void main(String[] args) {MyBinaryTree tree = new MyBinaryTree();//用前序遍历和后序遍历构建二叉树int[] pre = {3,9,20,15,7};int[] in = {9,3,15,20,7};TreeNode root = tree.buildTree1(pre,in);System.out.println("前序遍历");tree.preOrder(root);System.out.println();System.out.println("中序遍历");tree.inOrder(root);System.out.println();System.out.println("后序遍历");tree.postOrder(root);System.out.println();System.out.println("层序遍历");tree.levelOrder(root);System.out.println();System.out.println("统计树的节点个数");tree.size(root);System.out.println(nodeSize);System.out.println("统计叶子节点个数");tree.getLeafNodeCount1(root);System.out.println(leafSize);System.out.println("树的高度");System.out.println(tree.getHeight(root));System.out.println("检测树中值为val的元素是否存在 Q  B");
//        System.out.println(tree.find(root,'x').val);if (tree.find(root,'Q') == null){System.out.println("没有找到该元素");}else {System.out.println(tree.find(root,'x').val);}if (tree.find(root,'B') == null){System.out.println("没有找到该元素");}else {System.out.println(tree.find(root,'B').val);}System.out.println("获取第K层节点的个数");System.out.println(tree.getKLevelNodeCount(root,3));System.out.println("判断一棵树是不是完全二叉树");System.out.println(tree.isCompleteTree(root));}
}

6. 测试结果

相关文章:

【数据结构】用Java实现一棵二叉树

目录 前言 1. 创建MyBinaryTree类 2. 从前序与中序遍历序列构造二叉树 3. 从中序与后序遍历序列构造二叉树 4. 用层序遍历验证二叉树是否构建成功 5. 整体代码&#xff08;构建二叉树、二叉树的基本功能和测试代码&#xff09; 6. 测试结果 前言 前面两篇文章已经给出了…...

【面试】面试官问的几率较大的网络安全面试题

文章目录防范常见的 Web 攻击1、什么是SQL注入攻击2、什么是XSS攻击3、什么是CSRF攻击4、什么是文件上传漏洞5、DDos 攻击重要协议分布图1、arp协议的工作原理ARP协议工作原理&#xff1a;2、什么是RARP&#xff1f;工作原理3、dns是什么&#xff1f;dns的工作原理4、rip协议是…...

[Python] 循环语句

循环语句就是在符合条件的情况下&#xff0c;重复执行一个代码段 1.while循环 while语句可用于在条件为真时反复执行代码块 语法格式 while 条件语句:执行语句 当条件语句为真(True)时&#xff0c;就会执行while循环下的语句 示例 实现1到100 的累加并输出求和结果 …...

计算机网络考试复习——第一章 1.5 1.6

1.5 计算机网络的类别 1.5.1计算机网络的定义&#xff1a; 系统集合&#xff0c;连接起来&#xff0c;协议工作&#xff0c;资源共享 计算机网络主要是由一些通用的、可编程的硬件互连而成的&#xff0c;而这些硬件并非专门用来实现某一特定目的&#xff08;例如&#xff0…...

3.29 最小生成树算法

最小生成树概念 参考&#xff1a;什么是最小生成树&#xff1f; Minimum Spanning Tree 何为生成树&#xff1f; 生成树是指一个联通图的极小的连通子图&#xff0c;它包含了图中的所有n个顶点&#xff0c;并只有n-1条边&#xff08;构成一棵树&#xff09; 生成树的一些性…...

计算机科班与培训开发编程的区别在哪里?

科班、培训班、科班培训班的模式都培养了很多编程技术人员进入IT行业&#xff0c;有的成为某个技术领域的专家&#xff0c;有的成为领导层&#xff0c;有的一直在默默无闻的敲代码等待35岁的到来。不管那种方式入行&#xff0c;这些类似的情况都存在&#xff0c;并且未来还会一…...

idea设置常用自设置快捷键及坐标

<!--mybatis 依赖--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.5</version> </dependency…...

Vue 3.0 实例方法

#$watch 参数&#xff1a;{string | Function} source{Function | Object} callback{Object} [options] {boolean} deep{boolean} immediate{string} flush返回&#xff1a;{Function} unwatch用法&#xff1a; 侦听组件实例上的响应式 property 或函数计算结果的变化。回调函数…...

日撸 Java 三百行day1-10

文章目录说明day1 环境搭建1.1 开发环境1.2 package import 和 println1.3 编写HelloWorld.javaday2 基本算术操作2.1 加、减、乘、除、整除、取余.day3 基本if 语句3.1 if条件分支语句3.2 代码day4 闰年的计算4.1 思路整理&#xff1a;何为闰年&#xff1f;4.2 核心代码day5 基…...

Ubuntu Instant-ngp 训练自有数据集

1. 运行环境配置 conda create -n instant-ngp python3.10 conda activate instant-ngp pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple2. COLMAP稀疏重建生成transform.json colmap 环境配置参考文档&#xff1b; 终端定位在instant-ngp/da…...

k8s集群只一台节点,重启节点后命名空间找不到了

定位 如果您的Kubernetes集群只有一台节点&#xff0c;并且在重启节点之前您创建了一些命名空间和资源&#xff0c;那么在节点重启后&#xff0c;这些命名空间和资源可能会丢失。这是因为在Kubernetes中&#xff0c;资源和命名空间通常是存储在etcd中的。当节点重启时&#xf…...

MarkDown示例

这里写自定义目录标题欢迎使用Markdown编辑器新的改变功能快捷键合理的创建标题&#xff0c;有助于目录的生成如何改变文本的样式插入链接与图片如何插入一段漂亮的代码片生成一个适合你的列表创建一个表格设定内容居中、居左、居右SmartyPants创建一个自定义列表如何创建一个注…...

spring cloud 雪崩效应

什么是雪崩效应 雪崩就是塌方。在山坡上的积雪&#xff0c;如果积雪的内聚力小于重力或其他力量&#xff0c;则积雪便向下滑动&#xff0c;从而逐渐引起积雪的崩塌。 在微服务架构中&#xff0c;服务之间通常存在级联调用。比如&#xff0c;服务A调用服务B&#xff0c;而服…...

Python 自动化指南(繁琐工作自动化)第二版:三、函数

原文&#xff1a;https://automatetheboringstuff.com/2e/chapter3/ 您已经熟悉了前几章中的print()、input()和len()函数。Python 提供了几个这样的内置函数&#xff0c;但是您也可以编写自己的函数。函数就像一个程序中的一个小程序。 为了更好地理解函数是如何工作的&#…...

c++多线程 1

https://www.runoob.com/cplusplus/cpp-multithreading.html 两种类型的多任务处理&#xff1a;基于进程和基于线程。 基于进程的多任务处理是程序的并发执行。 基于线程的多任务处理是同一程序的片段的并发执行。 线程 c11以后有了 标准库 1 函数 2 类成员函数 3 lambda函…...

STM32F103制作FlashDriver

文章目录前言芯片内存定义实现过程FlashDriver生成段定义擦除函数写入函数编译后的map手动测试HexView提取指定地址内容并重映射总结前言 在汽车行业控制器软件刷新流程中&#xff0c;一般会将Flash驱动单独进行刷写&#xff0c;目的是防止程序中一直存在Flash驱动的话&#x…...

springboot树形结构接口, 懒加载实现

数据库关系有父子id的, 作为菜单栏展示时需要用前端需要用到懒加载, 所谓懒加载就是接口有一个标志位isLeaf, 前端请求后通过该字段判断该节点是否还有子节点数据 创建数据库表 t_company_info结构有id和parentId标识, 用来表示父子关系 /*Navicat Premium Data TransferSourc…...

java企业级信息系统开发学习笔记02初探spring——利用组件注解符精简spring配置文件

文章目录一、学习目标二、打开01的项目三、利用组件注解符精简spring配置文件&#xff08;一&#xff09;创建新包&#xff0c;复制四个类&#xff08;二&#xff09;修改杀龙任务类&#xff08;三&#xff09;修改救美任务类&#xff08;四&#xff09;修改勇敢骑士类&#xf…...

用Python发送电子邮件?这也太丝滑了吧(21)

小朋友们好&#xff0c;大朋友们好&#xff01; 我是猫妹&#xff0c;一名爱上Python编程的小学生。 欢迎和猫妹一起&#xff0c;趣味学Python。 今日主题 猫爸赚钱养家&#xff0c;细想起来真的不容易啊&#xff01; 起早贪黑&#xff0c;都是6点早起做早饭&#xff0c;送…...

分类预测 | MATLAB实现CNN-GRU-Attention多输入分类预测

分类预测 | MATLAB实现CNN-GRU-Attention多输入分类预测 目录分类预测 | MATLAB实现CNN-GRU-Attention多输入分类预测分类效果模型描述程序设计参考资料分类效果 模型描述 Matlab实现CNN-GRU-Attention多变量分类预测 1.data为数据集&#xff0c;格式为excel&#xff0c;12个输…...

golang循环变量捕获问题​​

在 Go 语言中&#xff0c;当在循环中启动协程&#xff08;goroutine&#xff09;时&#xff0c;如果在协程闭包中直接引用循环变量&#xff0c;可能会遇到一个常见的陷阱 - ​​循环变量捕获问题​​。让我详细解释一下&#xff1a; 问题背景 看这个代码片段&#xff1a; fo…...

汽车生产虚拟实训中的技能提升与生产优化​

在制造业蓬勃发展的大背景下&#xff0c;虚拟教学实训宛如一颗璀璨的新星&#xff0c;正发挥着不可或缺且日益凸显的关键作用&#xff0c;源源不断地为企业的稳健前行与创新发展注入磅礴强大的动力。就以汽车制造企业这一极具代表性的行业主体为例&#xff0c;汽车生产线上各类…...

Python爬虫(二):爬虫完整流程

爬虫完整流程详解&#xff08;7大核心步骤实战技巧&#xff09; 一、爬虫完整工作流程 以下是爬虫开发的完整流程&#xff0c;我将结合具体技术点和实战经验展开说明&#xff1a; 1. 目标分析与前期准备 网站技术分析&#xff1a; 使用浏览器开发者工具&#xff08;F12&…...

Robots.txt 文件

什么是robots.txt&#xff1f; robots.txt 是一个位于网站根目录下的文本文件&#xff08;如&#xff1a;https://example.com/robots.txt&#xff09;&#xff0c;它用于指导网络爬虫&#xff08;如搜索引擎的蜘蛛程序&#xff09;如何抓取该网站的内容。这个文件遵循 Robots…...

【决胜公务员考试】求职OMG——见面课测验1

2025最新版&#xff01;&#xff01;&#xff01;6.8截至答题&#xff0c;大家注意呀&#xff01; 博主码字不易点个关注吧,祝期末顺利~~ 1.单选题(2分) 下列说法错误的是:&#xff08; B &#xff09; A.选调生属于公务员系统 B.公务员属于事业编 C.选调生有基层锻炼的要求 D…...

C++.OpenGL (10/64)基础光照(Basic Lighting)

基础光照(Basic Lighting) 冯氏光照模型(Phong Lighting Model) #mermaid-svg-GLdskXwWINxNGHso {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-GLdskXwWINxNGHso .error-icon{fill:#552222;}#mermaid-svg-GLd…...

IoT/HCIP实验-3/LiteOS操作系统内核实验(任务、内存、信号量、CMSIS..)

文章目录 概述HelloWorld 工程C/C配置编译器主配置Makefile脚本烧录器主配置运行结果程序调用栈 任务管理实验实验结果osal 系统适配层osal_task_create 其他实验实验源码内存管理实验互斥锁实验信号量实验 CMISIS接口实验还是得JlINKCMSIS 简介LiteOS->CMSIS任务间消息交互…...

实现弹窗随键盘上移居中

实现弹窗随键盘上移的核心思路 在Android中&#xff0c;可以通过监听键盘的显示和隐藏事件&#xff0c;动态调整弹窗的位置。关键点在于获取键盘高度&#xff0c;并计算剩余屏幕空间以重新定位弹窗。 // 在Activity或Fragment中设置键盘监听 val rootView findViewById<V…...

python执行测试用例,allure报乱码且未成功生成报告

allure执行测试用例时显示乱码&#xff1a;‘allure’ &#xfffd;&#xfffd;&#xfffd;&#xfffd;&#xfffd;ڲ&#xfffd;&#xfffd;&#xfffd;&#xfffd;ⲿ&#xfffd;&#xfffd;&#xfffd;Ҳ&#xfffd;&#xfffd;&#xfffd;ǿ&#xfffd;&am…...

CVE-2020-17519源码分析与漏洞复现(Flink 任意文件读取)

漏洞概览 漏洞名称&#xff1a;Apache Flink REST API 任意文件读取漏洞CVE编号&#xff1a;CVE-2020-17519CVSS评分&#xff1a;7.5影响版本&#xff1a;Apache Flink 1.11.0、1.11.1、1.11.2修复版本&#xff1a;≥ 1.11.3 或 ≥ 1.12.0漏洞类型&#xff1a;路径遍历&#x…...