/**
* <html>
* <body>
* <P> Copyright 1994 JsonInternational</p>
* <p> All rights reserved. - https://github.com/Jasonandy/Java-Core-Advanced </p>
* <p> Created by Jason</p>
* </body>
* </html>
*/
package cn.ucaner.datastructure.BinarySearchTree; /**
* @Package:cn.ucaner.datastructure.BinarySearchTree
* @ClassName:BinarySearchTree
* @Description: <p> 树集合了数组(查找速度快)和链表(插入、删除速度快)的优点 </br> CSDN {@link https://blog.csdn.net/a19881029/article/details/24379339} </p>
* 二叉树是一种特殊的树,即:树中的每个节点最多只能有两个子节点
* 二叉搜索树是一种特殊的二叉树,即:节点的左子节点的值都小于这个节点的值,节点的右子节点的值都大于等于这个节点的值
* Tips:如果树中允许存在重复数据,处理起来比较麻烦,故实现中不允许树中存在重复数据,即节点的右子节点的值必须大于节点的值.
* 搜索二叉树有一个特点,即如果使用中序遍历遍历搜索二叉树,将得到包含搜索二叉树中所有节点值的升序排序结果
* @Author: - Jason
* @CreatTime:2018年4月7日 下午8:37:11
* @Modify By:
* @ModifyTime: 2018年4月7日
* @Modify marker:
* @version V1.0
*/
public class BinarySearchTree { private TreeNode root;//定义树的根结点 /**
* BinarySearchTree. 根据已知序列构建二叉搜索树
* @param input
*/
public BinarySearchTree(int[] input) {
createBinarySearchTree(input);
} /**
* @Description: 根据已知序列构建二叉搜索树
* @param input void
* @Autor:Jason - jasonandy@hotmail.com
*/
public void createBinarySearchTree(int[] input) {
if (input != null) {
for (int i = 0; i < input.length; i++) {
root = insert(input[i], root);
}
}
} /**
* @Description: 二叉搜索树的搜索算法,递归算法
* @param target 目标值
* @param root 二叉搜索树的根结点
* @return TreeNode
* @Autor: Jason - jasonandy@hotmail.com
*/
public TreeNode search(int target, TreeNode root) {
TreeNode result = null;
if (root != null) { // 递归终止条件
if (target == root.data) { // 递归终止条件
result = root;
return result;
} else if (target < root.data) { // 目标值小于根结点值,从左子树查找
result = search(target, root.left);
} else { // 目标值大于根结点值,从右子树查找
result = search(target, root.right);
}
}
return result;
} /**
* @Description: 二叉搜索树的插入操作
* @param target
* @param node
* @return TreeNode
* @Autor: Jason - jasonandy@hotmail.com
*/
public TreeNode insert(int target, TreeNode node) {
if (search(target, node) == null) {
if (node == null) {
return new TreeNode(target);
} else {
if (target < node.data) {
node.left = insert(target, node.left);
} else {
node.right = insert(target, node.right);
}
}
}
return node;
} /**
* @Description: 删除搜索二叉树的制定结点
* @param target
* @param node
* @return TreeNode
* @Autor: jason - jasonandy@hotmail.com
*/
public TreeNode remove(int target, TreeNode node) {
TreeNode tmp = null;
if (node != null) {
if (target < node.data) { // 从左子树删除
node.left = remove(target, node.left);
} else if (target > node.data) { // 从右子树删除
node.right = remove(target, node.right);
} else if (node.left != null && node.right != null) { // 找到待删除结点,且其左右子树不为空
// 找到以待删除结点右子树的中序遍历第一个结点(最小结点)
tmp = node.right;
while (tmp.left != null) {
tmp = tmp.left;
} // 用最小结点补位待删除结点
node.data = tmp.data; // 删除待删除结点右子树上补位结点
node.right = remove(node.data, node.right);
} else {
if (node.left == null) {
node = node.right;
} else {
node = node.left;
}
}
}
return node;
} /**
* @Description: 中序遍历二叉搜索树,递归算法,升序排序
* @param node void
* @Autor: Jason - jasonandy@hotmail.com
*/
public void inOrder(TreeNode node) {
if (node != null) {
inOrder(node.left);
System.out.print(root.data + " ");
inOrder(node.right);
}
} /**
* @Description: 打印二叉搜索树
* @param node void
* @Autor:jason - jasonandy@hotmail.com
*/
public void printTree(TreeNode node) {
if (node != null) {
System.out.print(node.data);
if (node.left != null || node.right != null) {
System.out.print("(");
printTree(node.left);
System.out.print(",");
printTree(node.right);
System.out.print(")");
}
}
} /**
* @Description: 访问二叉搜索树的根结点
* @return TreeNode
* @Autor:Jason - jasonandy@hotmail.com
*/
public TreeNode getRoot() {
return root;
}
}
/**
* <html>
* <body>
* <P> Copyright 1994 JsonInternational</p>
* <p> All rights reserved. - https://github.com/Jasonandy/Java-Core-Advanced </p>
* <p> Created by Jason</p>
* </body>
* </html>
*/
package cn.ucaner.datastructure.BinarySearchTree; /**
* @Package:cn.ucaner.datastructure.BinarySearchTree
* @ClassName:TreeNode
* @Description: <p> Node节点</p>
* @Author: - Jason
* @CreatTime:2018年4月7日 下午8:41:02
* @Modify By:
* @ModifyTime: 2018年4月7日
* @Modify marker:
* @version V1.0
*/
public class TreeNode { /**
*结点的数据项
*/
public int data; /**
* 结点指向左孩子的引用
*/
public TreeNode left; /**
* 结点指向右孩子的引用
*/
public TreeNode right; /**
* TreeNode. 构造方法,初始化结点数据项
* @param data
*/
public TreeNode(int data){
this.data = data;
} @Override
public String toString() {
return "TreeNode [data=" + data + "]";
} }

Java 二叉搜索树 实现和学习的更多相关文章

  1. java二叉搜索树原理与实现

    计算机里面的数据结构 树 在计算机存储领域应用作用非常大,我之前也多次强调多磁盘的存取速度是目前计算机飞速发展的一大障碍,计算机革命性的的下一次飞跃就是看硬盘有没有质的飞跃,为什么这么说?因为磁盘是永 ...

  2. Java二叉搜索树实现

    树集合了数组(查找速度快)和链表(插入.删除速度快)的优点 二叉树是一种特殊的树,即:树中的每个节点最多只能有两个子节点 二叉搜索树是一种特殊的二叉树,即:节点的左子节点的值都小于这个节点的值,节点的 ...

  3. java 二叉搜索树

    java二叉查找树实现: 二叉查找树,上图:比根节点小者在其左边,比根节点大者在其右边. 抽象数据结构,上代码: /** * 二叉查找树数据结构(非线程安全): * 范型类型须实现Comparable ...

  4. 二叉搜索树Java实现(查找、插入、删除、遍历)

    由于最近想要阅读下 JDK1.8 中 HashMap 的具体实现,但是由于 HashMap 的实现中用到了红黑树,所以我觉得有必要先复习下红黑树的相关知识,所以写下这篇随笔备忘,有不对的地方请指出- ...

  5. Java实现二叉搜索树

    原创:转载需注明原创地址 https://www.cnblogs.com/fanerwei222/p/11406176.html 尝试一下用Java实现二叉搜索树/二叉查找树,记录自己的学习历程. 1 ...

  6. 【算法与数据结构】二叉搜索树的Java实现

    为了更加深入了解二叉搜索树,博主自己用Java写了个二叉搜索树,有兴趣的同学可以一起探讨探讨. 首先,二叉搜索树是啥?它有什么用呢? 二叉搜索树, 也称二叉排序树,它的每个节点的数据结构为1个父节点指 ...

  7. Java实现二叉搜索树的添加,前序、后序、中序及层序遍历,求树的节点数,求树的最大值、最小值,查找等操作

    什么也不说了,直接上代码. 首先是节点类,大家都懂得 /** * 二叉树的节点类 * * @author HeYufan * * @param <T> */ class Node<T ...

  8. Java与算法之(13) - 二叉搜索树

    查找是指在一批记录中找出满足指定条件的某一记录的过程,例如在数组{ 8, 4, 12, 2, 6, 10, 14, 1, 3, 5, 7, 9, 11, 13, 15 }中查找数字15,实现代码很简单 ...

  9. 二叉搜索树(Java实现)

    二叉搜索树基本操作 求树中的结点个数 判断节点是否为空 向树中插入新结点key-value 树中是否存在key 返回树中key对应的value值 先序遍历 中序遍历 后续遍历 层序遍历 求树中key最 ...

随机推荐

  1. 【POJ3083】Children of the Candy Corn

    本题传送门 本题知识点:深度优先搜索 + 宽度优先搜索 本题题意是求三个路径长度,第一个是一直往左手边走的距离,第二个是一直往右手边走的距离,第三个是最短距离. 第三个很好办,就是一个简单的bfs的模 ...

  2. java.lang.NumberFormatException: Infinite or NaN原因之浮点类型除数为0结果探究

    背景 在对Double类型的数据进行计算操作,将结果转化为BigDecimal时抛出了下面的异常,进行了Debug才发现了问题原因,同时也暴露出了自己在一些基础知识上还有些欠缺. Exception ...

  3. rust猜数游戏代码

    use std::io; use rand::Rng; use std::cmp::Ordering; fn main() { println!("Guess the number!&quo ...

  4. 非静态内部类中 static/final 成员变量相关知识

    原文链接:https://blog.csdn.net/qq_20328181/article/details/81391956

  5. idea在docker环境,调试spring boot程序

    允许docker被远程访问 见:https://www.cnblogs.com/wintersoft/p/10921396.html 教程见:https://spring.io/guides/gs/s ...

  6. Single Cell Genomics Day: A Practical Workshop

    干货满满! Single Cell Genomics Day: A Practical Workshop

  7. 利用lsof命令查找已经删除的文件来释放磁盘空间

    测试环境一台服务器/目录空间使用率达到97%,但是通过du -sh *发现实际空间没用到那么多,初步怀疑,之前删除的文件,有运行中的进程一直占用,导致空间没有释放,如图通过du -sh *发现共实际使 ...

  8. join方法

    用join拼接两个字符串 a='hehe' b='wawa' print(''.join((a,b))) #'hehewawa' 用join拼接列表中的字符串 l=['a''b','c','d'] p ...

  9. docker本地化异常:/bin/sh: warning: setlocale: LC_ALL: cannot change locale (en_US.UTF-8)

    docker中经常设置不了 环境变量$LC_ALL,  导致报很多奇怪的编码错误: /bin/sh: warning: setlocale: LC_ALL: cannot change locale ...

  10. ubuntu 18.04屏幕共享 -------(转载) ( Windows远程登录Ubuntu )

    原文地址: https://my.oschina.net/michaelshu/blog/3018932 ----------------------------------------------- ...