一、查找最大值

// 查找最大值
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;
}
}

Java数据结构——二叉树节点的增删改查、获取深度及最大最小值的更多相关文章

  1. ZooKeeper客户端 zkCli.sh 节点的增删改查

    zkCli.sh 在 bin 目录下的  zkCli.sh  就是ZooKeeper客户端 ./zkCli.sh -timeout 5000  -server 127.0.0.1:2181  客户端与 ...

  2. Zookeeper入门(六)之zkCli.sh对节点的增删改查

    参考地址为:https://www.cnblogs.com/sherrykid/p/5813148.html 1.连接 在 bin 目录下的  zkCli.sh  就是ZooKeeper客户端 ./z ...

  3. java对xml文件做增删改查------摘录

    java对xml文件做增删改查 package com.wss; import java.io.File;import java.util.ArrayList;import java.util.Lis ...

  4. 使用java对sql server进行增删改查

    import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import ...

  5. Java API实现Hadoop文件系统增删改查

    Java API实现Hadoop文件系统增删改查 Hadoop文件系统可以通过shell命令hadoop fs -xx进行操作,同时也提供了Java编程接口 maven配置 <project x ...

  6. HTML DOM(二):节点的增删改查

    上一篇讲述了DOM的基本知识,从其得知,在DOM眼中,HTML的每个成分都可以看作是节点(文档节点.元素节点.文本节点.属性节点.注释节点,其中,属性节点是属于元素节点),本篇的内容就是通过DOM对这 ...

  7. HTML DOM节点的增删改查

    上篇博客中,我们已经初步接触了DOM基础,可是我们学习是为了可以更好地应用,今天我们就来看看DOM节点的增删改查. 无论在哪里,我们想要操作一个东西,总是应该先去获得它.那么我们怎么获得呢? HTML ...

  8. JavaScript---Dom树详解,节点查找方式(直接(id,class,tag),间接(父子,兄弟)),节点操作(增删改查,赋值节点,替换节点,),节点属性操作(增删改查),节点文本的操作(增删改查),事件

    JavaScript---Dom树详解,节点查找方式(直接(id,class,tag),间接(父子,兄弟)),节点操作(增删改查,赋值节点,替换节点,),节点属性操作(增删改查),节点文本的操作(增删 ...

  9. 9 HTML DOM事件监听&版本兼容&元素(节点)增删改查

    事件监听: 语法:element.addEventListener(event, function, useCapture); event:事件的类型,触发什么事件,注意不需要on作为前缀,比如cli ...

随机推荐

  1. 21天速成python电子书pdf|超级厉害的书内附百度云链接可直接下载|

    python的学习书籍小编看过很多,但是这本<21天学通python>真的是堪称极品!本书的作者团队成员为一线开发工程师.资深编程专家或专业培训师,在编程开发方面有着丰富的经验,并已出版过 ...

  2. 线程_multiprocessing实现文件夹copy器

    import multiprocessing import os import time import random def copy_file(queue,file_name,source_fold ...

  3. PHP cal_to_jd() 函数

    ------------恢复内容开始------------ 实例 把 2007 年 6 月 20 日(格利高里历法)转换为儒略日计数: <?php$d=cal_to_jd(CAL_GREGOR ...

  4. PHP filesize() 函数

    定义和用法 filesize() 函数返回指定文件的大小. 如果成功,该函数返回文件大小的字节数.如果失败,则返回 FALSE. 语法 filesize(filename) 参数 描述 filenam ...

  5. SpringBoot学习之整合Mybatis

    本博客使用IDEA开发工具,通过Maven构建SpringBoot项目,初始化项目添加的依赖有:spring-boot-starter-jdbc.spring-boot-starter-web.mys ...

  6. ASP.NET MVC+LayUI视频上传

    前言: 前段时间在使用APS.NET MVC+LayUI做视频上传功能的时,发现当上传一些内存比较大的视频就会提示上传失败,后来通过查阅相关资料发现.NET MVC框架为考虑安全问题,在运行时对请求的 ...

  7. java多线程之锁 -- 偏向锁、轻量级锁、自旋锁、重量级锁

    转载至:https://blog.csdn.net/zqz_zqz/article/details/70233767 之前做过一个测试,详情见这篇文章<多线程 +1操作的几种实现方式,及效率对比 ...

  8. 032_go语言中的定时器

    代码演示 package main import "fmt" import "time" func main() { timer1 := time.NewTim ...

  9. Spark Streaming——Spark第一代实时计算引擎

    虽然SparkStreaming已经停止更新,Spark的重点也放到了 Structured Streaming ,但由于Spark版本过低或者其他技术选型问题,可能还是会选择SparkStreami ...

  10. C++/C socket编程

    目录 socket()函数 何为socket Internet套接字 流格式套接字SOCK_STREAM 数据报格式套接字SOCK_DGRAM TCP/IP协议族 创建套接字 加载套接字库 Windo ...