Java实现二叉搜索树

Java实现二叉搜索树

文章目录

  • Java实现二叉搜索树
    • 一、二叉搜索树概念
    • 二、二叉搜索树常规操作
    • 三、`TreeNode` 存储结构实现
    • 四、二叉搜索树及其常规操作实现
      • 1. 二叉搜索树插入
      • 2.二叉搜索树查找
      • 3.二叉搜索树删除
      • 4.二叉搜索树高度
      • 5. 中序遍历打印二叉搜索树
    • 五、`BinarySearchTree`完整代码

一、二叉搜索树概念

​ 二叉搜索树(Binary Search Tree,BST),相比与普通树,有以下四点性质

  • 非空左子树的所有键值小于其根结点的键值;
  • 非空右子树的所有键值大于其根结点的键值;
  • 左右子树均为二叉搜索树
  • 树中没有键值相等的结点

​ 因为性质特殊,所以在查询的时候十分便捷。

二、二叉搜索树常规操作

  • Position Find(TreeNode X,BinarySearchTree BST) 从二叉搜索树中找到节点 X并返回其地址。
  • Position FindMin(BinarySearchTree BST) 从二叉搜索树中找到最小节点 X并返回其地址。
  • Position FindMan(BinarySearchTree BST) 从二叉搜索树中找到最大节点 X并返回其地址。
  • BinarySearchTree Insert(TreeNode X,BinarySearchTree BST) 在二叉搜索树中插入节点 X并返回二叉搜索树。
  • BinarySearchTree Delete(TreeNode X,BinarySearchTree BST) 在二叉搜索树中删除节点 X并返回二叉搜索树。

三、TreeNode 存储结构实现

package org.example.bcbd;/*** @ClassName TreeNode* @Description TODO* @Author 枫飘长安* @Date 2024/4/16 20:49* @Version 1.0**/public class TreeNode<T extends Comparable<T>> {// 数据域private T data;// 左子树public TreeNode leftChild;// 右子树public TreeNode rightChild;public TreeNode(T data) {this(null, data, null);}public TreeNode(TreeNode leftChild, T data, TreeNode rightChild) {this.leftChild = leftChild;this.data = data;this.rightChild = rightChild;}public T getData() {return data;}public TreeNode<T> getLeftChild() {return leftChild;}public TreeNode<T> getRightChild() {return rightChild;}public void setData(T data) {this.data = data;}private TreeNode<T> insert(TreeNode<T> node,T value) {if (node == null) {return  new TreeNode<>(value);} else {if (compare(node,value) < 0) {node.leftChild = insert(node.getLeftChild(),value);}else if (compare(node,value) > 0) {node.rightChild = insert(node.getRightChild(),value);}return node;}}private int compare(TreeNode<T> node,T value) {return value.compareTo(node.getData());}}

四、二叉搜索树及其常规操作实现

1. 二叉搜索树插入

Tips: 因为其特殊性质,我们很容易用递归实现插入操作

每次插入一个结点,从根节点出发作比较,小的往左子树插,大的往右子树插

    public void insert(T value) {if (value == null) {return;}this.root = insert(this.root, value);}private TreeNode<T> insert(TreeNode<T> node, T value) {if (node == null) {return new TreeNode<>(value);}else {if (compare(node, value) < 0) {node.leftChild = insert(node.getLeftChild(), value);}else if (compare(node,value) > 0) {node.rightChild = insert(node.getRightChild(), value);}}return node;}

2.二叉搜索树查找

Tips:在一颗二叉搜索树上,最小的值一定在最左边的结点上,最大值一定在最右边的结点上。查找二叉树最值递归实现即可。在查找特定值时注意如果查找的元素不在树中,我们得对其做出异常处理

    public TreeNode<T> find(T value) {if (this.root == null) return null;return find(this.root, value);}private TreeNode<T> find(TreeNode<T> node, T value) {if (node == null) throw new RuntimeException("the value must not in the tree");if (compare(node, value) < 0) {return find(node.getLeftChild(), value);}else if (compare(node,value) > 0) {return find(node.getRightChild(), value);}else {return node;}}public T findMax() {if (this.root == null) return null;return findMax(this.root);}private T findMax(TreeNode<T> node) {TreeNode<T> temp = node;while (temp.getRightChild() != null) {temp = temp.getRightChild();}return temp.getData();}public T findMin() {if (this.root == null) return null;return findMin(this.root);}private T findMin(TreeNode<T> node) {TreeNode<T> temp = node;while (temp.getLeftChild() != null) {temp = temp.getLeftChild();}return temp.getData();}

3.二叉搜索树删除

Tips: 删除是最难的操作了,因为删掉节点后,整棵树仍然需要为二叉搜索树

  • 叶子结点:直接删除,其父结点指向null
  • 包含一个孩子的结点 :父结点指向要删除结点的自结点(相当于链表中间删除一个元素);
  • 包含左右子树的结点:右子树最小值或左子树最大值替换此结点
    public void delete(T value) {if (this.root == null || value == null) return;this.root = delete(this.root, value);}public TreeNode<T> delete(TreeNode<T> node, T value) {if (node == null) return null;if (compare(node, value) < 0) { // 去删左子树node.leftChild = delete(node.getLeftChild(), value);}else if (compare(node, value) > 0) { // 去删右子树node.rightChild = delete(node.getRightChild(), value);}else { // 找到要删除的节点if (node.getLeftChild() != null && node.getRightChild() != null) {// 被删除的结点,包含左右子树T temp = findMin(node.getRightChild()); // 得到右子树的最小值node.setData(temp); //右子树最小值替换当前结点node.rightChild = delete(node.getRightChild(), temp); // 从右子树删除这个最小值的结点} else {// 被删除的结点,包含一个子树或没有子树if (node.getLeftChild() != null) {node = node.getLeftChild();} else {node = node.getRightChild();}}}return node;}

4.二叉搜索树高度

Tips: 就是求树的高度,没什么好讲的。

    public int getTreeHeight() {if (this.root == null) return 0;return getTreeHeight(this.root);}private int getTreeHeight(TreeNode<T> node) {if (node == null) return 0;int leftHeight = getTreeHeight(node.getLeftChild());int rightHeight = getTreeHeight(node.getRightChild());return Math.max(leftHeight, rightHeight) + 1;}

5. 中序遍历打印二叉搜索树

    public void printTree() {printTree(this.root);}private void printTree(TreeNode<T> node) {if (node == null) {return;}printTree(node.getLeftChild());System.out.print(node.getData() + " ");printTree(node.getRightChild());}

五、BinarySearchTree完整代码

package org.example.bcbd; // 包名请按照自己的程序设置/*** @ClassName BinarySearchTree* @Description TODO* @Author 枫飘长安* @Date 2024/4/16 20:59* @Version 1.0**/
public class BinarySearchTree<T extends Comparable<T>> {private TreeNode<T> root;public BinarySearchTree() {this.root = null;}public BinarySearchTree(TreeNode<T> root) {this.root = root;}public void insert(T value) {if (value == null) {return;}this.root = insert(this.root, value);}private TreeNode<T> insert(TreeNode<T> node, T value) {if (node == null) {return new TreeNode<>(value);}else {if (compare(node, value) < 0) {node.leftChild = insert(node.getLeftChild(), value);}else if (compare(node,value) > 0) {node.rightChild = insert(node.getRightChild(), value);}}return node;}private int compare(TreeNode<T> node, T value) {return value.compareTo(node.getData());}public void printTree() {printTree(this.root);}private void printTree(TreeNode<T> node) {if (node == null) {return;}printTree(node.getLeftChild());System.out.print(node.getData() + " ");printTree(node.getRightChild());}public T findMax() {if (this.root == null) return null;return findMax(this.root);}private T findMax(TreeNode<T> node) {TreeNode<T> temp = node;while (temp.getRightChild() != null) {temp = temp.getRightChild();}return temp.getData();}public T findMin() {if (this.root == null) return null;return findMin(this.root);}private T findMin(TreeNode<T> node) {TreeNode<T> temp = node;while (temp.getLeftChild() != null) {temp = temp.getLeftChild();}return temp.getData();}public TreeNode<T> find(T value) {if (this.root == null) return null;return find(this.root, value);}private TreeNode<T> find(TreeNode<T> node, T value) {if (node == null) throw new RuntimeException("the value must not in the tree");if (compare(node, value) < 0) {return find(node.getLeftChild(), value);}else if (compare(node,value) > 0) {return find(node.getRightChild(), value);}else {return node;}}public int getTreeHeight() {if (this.root == null) return 0;return getTreeHeight(this.root);}private int getTreeHeight(TreeNode<T> node) {if (node == null) return 0;int leftHeight = getTreeHeight(node.getLeftChild());int rightHeight = getTreeHeight(node.getRightChild());return Math.max(leftHeight, rightHeight) + 1;}public void delete(T value) {if (this.root == null || value == null) return;this.root = delete(this.root, value);}public TreeNode<T> delete(TreeNode<T> node, T value) {if (node == null) return null;if (compare(node, value) < 0) { // 去删左子树node.leftChild = delete(node.getLeftChild(), value);}else if (compare(node, value) > 0) { // 去删右子树node.rightChild = delete(node.getRightChild(), value);}else { // 找到要删除的节点if (node.getLeftChild() != null && node.getRightChild() != null) {// 被删除的结点,包含左右子树T temp = findMin(node.getRightChild()); // 得到右子树的最小值node.setData(temp); //右子树最小值替换当前结点node.rightChild = delete(node.getRightChild(), temp); // 从右子树删除这个最小值的结点} else {// 被删除的结点,包含一个子树或没有子树if (node.getLeftChild() != null) {node = node.getLeftChild();} else {node = node.getRightChild();}}}return node;}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/826247.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

同旺科技 USB TO SPI / I2C适配器读写24LC256--页写

所需设备&#xff1a; 1、USB 转 SPI I2C 适配器&#xff1b;内附链接 2、24LC256芯片 适应于同旺科技 USB TO SPI / I2C适配器升级版、专业版&#xff1b; 从00地址开始写入64个字节&#xff0c;然后再将64个字节读回&#xff1b; 页写时序&#xff1a; 读时序&#xff1a…

iOS开发 刻度盘 仪表盘,圆点按钮滑动控制,渐变色

最近项目需要&#xff0c;想做一个渐变色的刻度盘&#xff0c;圆形按钮滑动控制&#xff0c;所以 用oc写了一下&#xff0c;代码没附上&#xff0c;想看代码可以私信联系&#xff0c;效果如下图。 部分代码 self.drawCenter CGPointMake(self.frame.size.width / 2.0, self.f…

姑苏寻韵~庆开放原子开源大赛 OpenTiny 前端 Web 应用开发挑战赛路演圆满落幕。

春日已至&#xff0c;姑苏古城迎来了一场编程的盛宴——开放原子开源大赛OpenTiny前端Web应用开发挑战赛。历时三个月的激烈角逐&#xff0c;OpenTiny与众多开发者携手共赴这场智慧的较量。决赛路演于4月14日在苏州&#xff08;太湖&#xff09;产业软件园圆满落下帷幕~ 开放原…

rabbitmq 使用SAC队列实现顺序消息

rabbitmq 使用SAC队列实现顺序消息 前提 SAC: single active consumer, 是指如果有多个实例&#xff0c;只允许其中一个实例消费&#xff0c;其他实例为空闲 目的 实现消息顺序消费&#xff0c;操作&#xff1a; 创建4个SAC队列,消息的路由key 取队列个数模&#xff0c;这…

Python可视化数据分析-柱状图/折线图

一、前言 使用python编写一个图表生成器&#xff0c;输入各公司的不良品数量&#xff0c;可以在一张图中同时展示数据的柱状图和折线图。 效果如下&#xff1a; 二、基础知识 绘制折线图和柱状图主要使用到了 pyecharts.charts 模块中的 Line 和 Bar 类。它们允许用户通过简…

完整、免费的把pdf转word文档

在线工具网 https://www.orcc.online/pdf 支持pdf转word&#xff0c;免费、完整、快捷 登录网站 https://orcc.online/pdf 选择需要转换的pdf文件&#xff1a; 等待转换完成 点击蓝色文件即可下载 无限制&#xff0c;完整转换。

动态IP与静态IP的区别,你选对了吗?

在互联网世界中&#xff0c;IP地址是每台设备在网络上的唯一标识。这些地址可以是动态的&#xff0c;也可以是静态的。对于非专业人士来说&#xff0c;理解这两者之间的区别可能会有些困难。本文旨在深入探讨动态IP和静态IP的主要差异&#xff0c;帮助读者根据自己的需求做出明…

Golang | Leetcode Golang题解之第37题解数独

题目&#xff1a; 题解&#xff1a; func solveSudoku(board [][]byte) {var line, column [9][9]boolvar block [3][3][9]boolvar spaces [][2]intfor i, row : range board {for j, b : range row {if b . {spaces append(spaces, [2]int{i, j})} else {digit : b - 1line…

docker网路和主机通讯问题

#注 1&#xff0c;安装docker和启动容器服务的时候如果防火墙处于开启状态&#xff0c;那么重启docker里面的容器的时候必须开启防火墙&#xff0c;否则会出现iptable错误&#xff1b; 2&#xff0c;linux开启防火墙会导致主机和docker网络之间单向通讯&#xff0c;主机可以访…

一周IT资讯

又降了&#xff1f;运维4月平均月薪1W6&#xff1f; 薪资作为大部分人的主要收入来源&#xff0c;是每个人最关注的话题之一。 最近&#xff0c;小编搜索了近半年的运维薪资趋势&#xff0c;看看你的钱包缩水了没&#xff1f; *数据来自看准网 据了解&#xff0c;运维2024年…

单链表详解(无哨兵位),实现增删改查

1.顺序表对比单链表的缺点 中间或头部插入时&#xff0c;需要移动数据再插入&#xff0c;如果数据庞大会导致效率降低每次增容就需要申请空间&#xff0c;而且需要拷贝数据&#xff0c;释放旧空间增容造成浪费&#xff0c;因为一般都是以2倍增容 2.链表的基础知识 链表也是线…

LeetCode---128双周赛

题目列表 3110. 字符串的分数 3111. 覆盖所有点的最少矩形数目 3112. 访问消失节点的最少时间 3113. 边界元素是最大值的子数组数目 一、字符串的分数 按照题目要求&#xff0c;直接模拟遍历即可&#xff0c;代码如下 class Solution { public:int scoreOfString(string …

如何使用ArcGIS Pro进行路径分析

路径分析是一种空间分析技术&#xff0c;用于确定两个或多个地点之间最佳路径或最短路径&#xff0c;这里为大家介绍一下在ArcGIS Pro中如何进行路径分析&#xff0c;希望能对你有所帮助。 数据来源 教程所使用的数据是从水经微图中下载的道路数据&#xff0c;除了道路数据&a…

阿里TTl使用管理日志

在管理日志的时候我们需要查看生成日志都是那些人干了那些事&#xff0c;那么怎么在日志上查看这些事情呢&#xff0c;首先呢可以直接使用Slf4j,然后再配置文件里配置一下 #日志文件最大上限 logging.file.max-size100MB #日志文件存储位置 logging.file.path./logs #日志文件…

中颖51芯片学习7. ADC模数转换

中颖51芯片学习7. ADC模数转换 一、ADC工作原理简介1. 概念2. ADC实现方式3. 基准电压 二、中颖芯片ADC功能介绍1. 中颖芯片ADC特性2. ADC触发源&#xff08;1&#xff09;**软件触发**&#xff08;2&#xff09;**TIMER4定时器触发**&#xff08;3&#xff09;**外部中断2触发…

面试: 悲观锁和乐观锁

一、悲观锁的代表是synchronized和Lock 锁 其核心思想是【线程只有占有了锁&#xff0c;才能去操作共享变量&#xff0c;每次只有一个线程占锁成功&#xff0c;获取锁失败的线程&#xff0c;都得停下来等待】线程从运行到阻塞、再从阻塞到唤醒&#xff0c;涉及线程上下文切换&a…

CTFHub(web sql注入)(三)

MYSQL 手工注入 1.判断字段数 输入1 输入2 输入3 得知字段有两个 2.判断注入类型 1 and 1 1 1 and 12 回显错误&#xff0c;说明存在sql注入 3.查看数据库内容 知道字段数量为2后&#xff0c;可以查看数据库位置 1 union select 1,2 使用union select 1,2查看未发现数…

【Java基础】21.重写(Override)与重载(Overload)

文章目录 一、重写(Override)1.方法重写2.方法的重写规则3.Super 关键字的使用 二、重载(Overload)1.方法重载2.重载规则3.实例 三、重写与重载之间的区别 一、重写(Override) 1.方法重写 重写&#xff08;Override&#xff09;是指子类定义了一个与其父类中具有相同名称、参…

阿里云OSS 存储对象的注册与使用

目录 一、什么是阿里云OSS 二、 点击免费试用 2.1 选择第一个&#xff0c;点击免费试用 ​编辑 2.2 登录管理控制台 2.3 进入Bucket 2.4、在阿里云网站上的个人中心配置Accesskey,查询accessKeyId和accessKeySecret。 2.5、进入AccssKey管理页面应该会出现下图提示&…

【VI/VIM】基本操作备忘录

简介 新建/打开文件 工作模式 常用命令 移动命令 文本选中 撤销、删除 复制粘贴 替换 缩排 查找 替换 插入 分屏 练习