Java 树结构实际应用 三(二叉排序树)
package com.lin.binarysorttree_0314;
public class BinarySortTreeTest {
public static void main(String[] args) {
int[] arr = {7, 3, 10, 12, 5, 1, 9};
BinarySortTree binarySortTree = new BinarySortTree();
for (int i = 0; i < arr.length; i++) {
binarySortTree.add(new SNode(arr[i]));
}
binarySortTree.add(new SNode(2));
binarySortTree.infixOrder();
// 删除
System.out.println("***********");
binarySortTree.delNode(2);
binarySortTree.delNode(3);
binarySortTree.delNode(5);
binarySortTree.delNode(7);
binarySortTree.delNode(9);
binarySortTree.delNode(12);
System.out.println("root:" + binarySortTree.getRoot());
binarySortTree.infixOrder();
}
}
class BinarySortTree{
private SNode root;
// 查找要删除的节点
public SNode getRoot() {
return root;
}
public SNode searchDelNode(int value) {
if(root == null) {
return null;
} else {
return root.searchDelNode(value);
}
}
// 查找要删除节点的父节点
public SNode searchParent(int value) {
if(root == null) {
return null;
} else {
return root.searchParent(value);
}
}
/**
* @param node 传入的节点(当作二叉排序树的根节点)
* @return 返回的以node为根节点的二叉排序树的最小节点的值
*/
public int delRightTreeMin(SNode node) {
SNode target = node;
// 循环地查找左节点,就会找到最小值
while(target.left != null) {
target = target.left;
}
delNode(target.value);// !!!!
return target.value;// !!!!!
}
// 删除节点
public void delNode(int value) {
if(root == null) {
return;
} else {
// 找删除节点
SNode targetNode = searchDelNode(value);
// 没有找到
if(targetNode == null) {
return;
}
// 如果发现当前这棵二叉树只有一个节点
if(root.left == null && root.right == null) {
root = null;
return;
}
// 去找到targetNode的父节点
SNode parent = searchParent(value);
// 如果删除的节点是叶子节点
if(targetNode.left == null && targetNode.right == null) {
// 判断targetNode是父节点的左子节点还是右子节点
if(parent.left != null && parent.left.value == value) {
parent.left = null;
} else if(parent.right != null && parent.right.value == value) {
parent.right = null;
}
} else if(targetNode.left != null && targetNode.right != null) { // 有左右子节点
int delRightTreeMin = delRightTreeMin(targetNode.right);
targetNode.value = delRightTreeMin;
} else {// 只有一个子节点
// 要删除的节点只有左节点
if(targetNode.left != null) {
if(parent != null) {
// 如果targetNode是parent的左子节点
if(parent.left.value == value) {
parent.left = targetNode.left;
} else {
parent.right = targetNode.left;
}
} else {
root = targetNode.left;
}
} else {// 要删除的节点有右子节点
if(parent != null) {
if(parent.left.value == value) {
parent.left = targetNode.right;
} else {
parent.right = targetNode.right;
}
} else {
root = targetNode.right;
}
}
}
}
}
// 中序遍历
public void infixOrder() {
if(root == null) {
System.out.println("空树!");
} else {
root.infixOrder();
}
}
// 添加
public void add(SNode node) {
if(root == null) {
root = node;
} else {
root.add(node);
}
}
}
class SNode{
protected int value;
protected SNode left;
protected SNode right;
public SNode(int value) {
// TODO Auto-generated constructor stub
this.value = value;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "Node = [value = " + value + "]";
}
// 添加节点
public void add(SNode node) {
if(node == null) {
return;
}
if(node.value < this.value) {
if(this.left == null) {
this.left = node;
} else {
this.left.add(node);
}
} else {
if(this.right == null) {
this.right = node;
} else {
this.right.add(node);
}
}
}
// 中序遍历
public void infixOrder() {
if(this.left != null) {
this.left.infixOrder();
}
System.out.println(this);
if(this.right != null) {
this.right.infixOrder();
}
}
// 查找要删除的节点
public SNode searchDelNode(int value) {
if(this.value == value) {
return this;
} else if(this.value > value) {
// 如果左子节点为空
if(this.left == null) {
return null;
}
return this.left.searchDelNode(value);
} else {
if(this.right == null) {
return null;
}
return this.right.searchDelNode(value);
}
}
// 查找要删除节点的父节点, 如果没有则返回null
public SNode searchParent(int value) {
if(( this.left != null && this.left.value == value)
|| ( this.right != null && this.right.value == value )) {
return this;
} else {
// 如果查找的值小于当前节点的值,并且当前节点的左子节点不为空
if(value < this.value && this.left != null) {
return this.left.searchParent(value);
} else if(value >= this.value && this.right != null) {
return this.right.searchParent(value);
} else {
return null;
}
}
}
}

仅供参考,有错误还请指出!
有什么想法,评论区留言,互相指教指教。
觉得不错的可以点一下右边的推荐哟
Java 树结构实际应用 三(二叉排序树)的更多相关文章
- Java 处理 XML 的三种主流技术及介绍
Java 处理 XML 的三种主流技术及介绍 原文地址:https://www.ibm.com/developerworks/cn/xml/dm-1208gub/ XML (eXtensible Ma ...
- java解析xml的三种方法
java解析XML的三种方法 1.SAX事件解析 package com.wzh.sax; import org.xml.sax.Attributes; import org.xml.sax.SAXE ...
- 20145213《Java程序设计》实验三敏捷开发与XP实践
20145213<Java程序设计>实验三敏捷开发与XP实践 实验要求 1.XP基础 2.XP核心实践 3.相关工具 实验内容 1.敏捷开发与XP 软件工程是把系统的.有序的.可量化的方法 ...
- 20145213《Java程序设计》第三周学习总结
20145213<Java程序设计>第三周学习总结 教材学习内容总结 正所谓距离产生美,上周我还倾心于Java表面的基础语法.其简单的流程结构,屈指可数的基本类型分类,早已烂熟于心的运算符 ...
- 20145206《Java程序设计》实验三实验报告
20145206<Java程序设计>实验三实验报告 实验内容 XP基础 XP核心实践 相关工具 实验步骤 (一)敏捷开发与XP 软件工程是把系统的.有序的.可量化的方法应用到软件的开发.运 ...
- 20145308刘昊阳 《Java程序设计》实验三 敏捷开发与XP实践 实验报告
20145308刘昊阳 <Java程序设计>实验三 敏捷开发与XP实践 实验报告 实验名称 敏捷开发与XP实践 实验内容 XP基础 XP核心实践 相关工具 统计的PSP(Personal ...
- 20145330《Java程序设计》第三周学习总结
20145330 <Java程序设计>第三周学习总结 第三周知识的难度已经逐步上升,并且一周学习两章学习压力也逐渐加大,需要更高效率的来完成学习内容,合理安排时间. 类与对象 对象(Obj ...
- 20145337《Java程序设计》第三周学习总结
20145337 <Java程序设计>第三周学习总结 教材学习内容总结 类与对象 类与对象的关系:要产生对象必须先定义类,类是对象的设计图,对象是类的实例.我觉得在视频中对类与对象关系的描 ...
- 20145320《Java程序设计》第三次实验报告
20145320<Java程序设计>第三次实验报告 北京电子科技学院(BESTI)实验报告 课程:Java程序设计 班级:1453 指导教师:娄嘉鹏 实验日期:2016.04.22 15: ...
随机推荐
- 牛客多校第六场G Is Today Friday?(吉姆拉尔森/蔡勒公式 + 思维)题解
题意: 给你\(A-J\)的字母组成的日期,形式为\(yyyy/mm/dd\).现给你\(n\)个这样的串\((n<=1e5)\),问你把字母映射成数字,并且使得所有日期合法且为星期五的最小字典 ...
- Keras模型拼装
在训练较大网络时, 往往想加载预训练的模型, 但若想在网络结构上做些添补, 可能出现问题一二... 一下是添补的几种情形, 此处以单输出回归任务为例: # 添在末尾: base_model = Inc ...
- windows10 浏览器跑分对比!
2015-12-12 windows10 浏览器跑分对比! YOUR BROWSER SCORES! MaxScore=555http://html5test.com/i ...
- GraphQL API In Action
GraphQL API In Action GraphQL API express $ yarn add express express-graphql graphql # OR $ npm i -S ...
- macOS & wifi & ip
macOS & wifi & ip mac show wifi ip # wireless ipconfig getifaddr en1 # ethernet ipconfig get ...
- std::vector与std::list效能对比(基于c++11)
测试对象类型不同,数量级不同时,表现具有差异: 测试数据对象为std::function时: test: times(1000)vector push_back time 469 usvector e ...
- java线程池趣味事:这不是线程池
要想写出高性能高并发的应用,自然有许多关键,如io,算法,异步,语言特性,操作系统特性,队列,内存,cpu,分布式,网络,数据结构,高性能组件. 胡说一通先. 回到主题,线程池.如果说多线程是提高系统 ...
- 配置安装oh-my-bash
背景 最近项目上有用到petalinux,所以只能在bash环境下使用命令行,所以不能使用oh-my-zsh 之前看一个dalao用oh-my-bash来规避这个问题.所以这里试一下安装oh-my-b ...
- Ctf_show Web[1-8]
CTFshow_web1: F12查看JS即在代码里 CTFshow_web2: 进入网址后页面如下: ①:尝试使用admin登陆,发现页面无回显 ②:尝试单引号闭合,并且添加' or 1=1#,此时 ...
- 【转+】以C++为核心语言的高频交易系统的讨论
[前言]高频交易是量化交易的核心.主要分两个方向:计算机技术和交易策略.策略各有不同,一般都是数据分析的专家或者金融,机器学习从业者.在计算机技术方面,一个是交易平台的性能,二者是硬件的性能,延时的多 ...