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

java计算二叉树的节点最小值_Java数据结构——二叉树节点的增删改查、获取深度及最大最小值...

一、查找最大值

// 查找最大值

public static Node maxNode() {

Node node = root;

Node maxNode = node;

while (node != null) {

maxNode = node;

node = node.getRichild();

}

return maxNode;

}

二、查找最小值

// 查找最小值

public static Node minNode() {

Node node = root;

Node minNode = node;

while (node != null) {

minNode = node;

node = node.getLechild();

}

return minNode;

}

三、插入节点

// 插入节点

public static boolean insert(Object data, Node parent) {

Node node = new Node(data, null, null);

if (root == null || parent == null) {

root = node;

return true;

} else if (parent.getLechild() != null && parent.getRichild() != null) {

return false;

} else {

if (parent.getLechild() != null) {

parent.setRichild(node);

} else {

parent.setLechild(node);

}

return true;

}

}

四、查找节点

// 查找节点

public static Node find(Node n, Object data) {

if (n != null) {

if (n.getData() == data) {

return n;

} else {

Node res = null;

res = find(n.getLechild(), data);

if (res == null) {

res = find(n.getRichild(), data);

}

return res;

}

} else {

return null;

}

}

五、修改节点

直接调用setData方法即可。

六、删除子节点

// 删除子节点

public static void delete( Node node) {

node.setRichild(null);

node.setLechild(null);

}

七、求深度

// 求深度

//求最长路径

public static int getDepth1(Node node) {

if (node == null) {

return 0;

}

if (node.getLechild() == null && node.getRichild() == null) {

return 1;

}

if (node.getLechild() == null) {

return getDepth1(node.getRichild()) + 1;

}

if (node.getRichild() == null) {

return getDepth1(node.getLechild()) + 1;

} else {

return Math.max(getDepth1(node.getLechild()), getDepth1(node.getRichild())) + 1;

}

}

// 求最小路径

public static int getDepth2(Node node) {

if (node == null) {

return 0;

}

if (node.getLechild() == null && node.getRichild() == null) {

return 1;

}

if (node.getLechild() == null) {

return getDepth2(node.getRichild()) + 1;

}

if (node.getRichild() == null) {

return getDepth2(node.getLechild()) + 1;

} else {

return Math.min(getDepth2(node.getLechild()), getDepth2(node.getRichild())) + 1;

}

}

相关文章:

  • 吴恩达深度学习笔记 2.10~2.18 向量化与python
  • easyui分页查询为什么会有下拉框_Easyui 添加分页组件_EasyUI 教程
  • python-模块,异常,环境管理器
  • 多相机坐标转换_自动驾驶入门之视觉定位坐标转换
  • 蓝书2.2 KMP算法
  • 双下划线一粗一细怎么加_跑跑卡丁车U型弯怎么过技巧教学
  • Java编程语言基础 第三章 季节if用法
  • 基础数据类型转换和深浅拷贝
  • matlab 子图title的位置_MATLAB技巧之绘图篇
  • 管程由哪三部分组成_中药材切片机主要由哪五部分组成呢?
  • socket-详细分析No buffer space available(转载)
  • 怎么保存到桌面_标签打印软件怎么保存标签
  • C++之继承(二)
  • docker mariadb集群_Docker搭建Django+Mariadb环境
  • VueX源码分析(1)
  • 【跃迁之路】【641天】程序员高效学习方法论探索系列(实验阶段398-2018.11.14)...
  • 2017前端实习生面试总结
  • Centos6.8 使用rpm安装mysql5.7
  • Js基础——数据类型之Null和Undefined
  • Python连接Oracle
  • Spring Security中异常上抛机制及对于转型处理的一些感悟
  • 工作踩坑系列——https访问遇到“已阻止载入混合活动内容”
  • 基于web的全景—— Pannellum小试
  • 前端面试总结(at, md)
  • 十年未变!安全,谁之责?(下)
  • Play Store发现SimBad恶意软件,1.5亿Android用户成受害者 ...
  • Spring Batch JSON 支持
  • 交换综合实验一
  • #【QT 5 调试软件后,发布相关:软件生成exe文件 + 文件打包】
  • (26)4.7 字符函数和字符串函数
  • (5)STL算法之复制
  • (aiohttp-asyncio-FFmpeg-Docker-SRS)实现异步摄像头转码服务器
  • (delphi11最新学习资料) Object Pascal 学习笔记---第2章第五节(日期和时间)
  • (Java岗)秋招打卡!一本学历拿下美团、阿里、快手、米哈游offer
  • (pytorch进阶之路)扩散概率模型
  • (非本人原创)我们工作到底是为了什么?​——HP大中华区总裁孙振耀退休感言(r4笔记第60天)...
  • (算法)求1到1亿间的质数或素数
  • .aanva
  • .net websocket 获取http登录的用户_如何解密浏览器的登录密码?获取浏览器内用户信息?...
  • .net 无限分类
  • .NET6 开发一个检查某些状态持续多长时间的类
  • .Net6支持的操作系统版本(.net8已来,你还在用.netframework4.5吗)
  • .NET版Word处理控件Aspose.words功能演示:在ASP.NET MVC中创建MS Word编辑器
  • .NET下的多线程编程—1-线程机制概述
  • .Net小白的大学四年,内含面经
  • @Data注解的作用
  • @private @protected @public
  • [23] GaussianAvatars: Photorealistic Head Avatars with Rigged 3D Gaussians
  • [AAuto]给百宝箱增加娱乐功能
  • [Asp.net MVC]Asp.net MVC5系列——Razor语法
  • [BZOJ1089][SCOI2003]严格n元树(递推+高精度)
  • [C#]winform制作圆形进度条好用的圆环圆形进度条控件和使用方法
  • [Django开源学习 1]django-vue-admin
  • [go] 策略模式
  • [HDOJ4911]Inversion