一、查找最大值

// 查找最大值
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. 对于Javaweb初学者的一些坑。#Javaweb

    1.在配置好Tomcat之后 ,编译阶段发现报错 这种对于我个人来说一般有两种情况: ①在编写代码时(比如servlet)发现爆红,一般是maven的依赖没有导入,这个时候在xml文件中导入需要的包的 ...

  2. ROS 机器人技术 - 广播与接收 TF 坐标

    上次我们学习了 TF 的基本概念和如何发布静态的 TF 坐标: ROS 机器人技术 - TF 坐标系统基本概念 ROS 机器人技术 - 静态 TF 坐标帧 这次来总结下如何发布一个自定义的 TF 坐标 ...

  3. 17条嵌入式C语言编程小知识总结

    流水线被指令填满时才能发挥最大效能,即每时钟周期完成一条指令的执行(仅指单周期指令). 如果程序发生跳转,流水线会被清空,这将需要几个时钟才能使流水线再次填满.因此,尽量少的使用跳转指令可以提高程序执 ...

  4. 10-9 重要的内置函数(zip、filter、map、sorted)

    reverse----reversed l = [1,2,3,4,5,6] l.reverse() #不会保留原列表 print(l) l =[1,2,3,4,5,6] l2 = reversed(l ...

  5. PHP end() 函数

    实例 输出数组中的当前元素和最后一个元素的值: <?php$people = array("Peter", "Joe", "Glenn" ...

  6. PHP natcasesort() 函数

    定义和用法 natcasesort() 函数用"自然排序"算法对数组进行排序.键值保留它们原始的键名. 在自然排序算法中,数字 2 小于 数字 10.在计算机排序算法中,10 小于 ...

  7. luogu P1973 [NOI2011]NOI 嘉年华 dp

    LINK:NOI 嘉年华 一道质量非常高的dp题目. 考虑如何求出第一问 容易想到dp. 按照左端点排序/右端点排序状态还是很难描述. 但是我们知道在时间上肯定是一次选一段 所以就可以直接利用时间点来 ...

  8. ios 继承UITableViewController,更改tableview样式

    // 继承UITableViewController,更改tableview样式 - (instancetype)initWithStyle:(UITableViewStyle)style { ret ...

  9. 023_go语言中的通道

    代码演示 package main import "fmt" func main() { messages := make(chan string) go func() { mes ...

  10. SpringCloud系列之服务容错保护Netflix Hystrix

    1. 什么是雪崩效应? 微服务环境,各服务之间是经常相互依赖的,如果某个不可用,很容易引起连锁效应,造成整个系统的不可用,这种现象称为服务雪崩效应. 如图,引用国外网站的图例:https://www. ...