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,一经查实,立即删除!

相关文章

c++的基础语法

看到几篇讲c基础语法非常好的文章&#xff0c;忍不住想存下来&#xff0c;以备以后查找。 文章目录 1. 指针(*)、取地址(&)、解引用(*)与引用(&)1.1C 指针运算符&#xff08;& 和 *&#xff09; 1. 指针()、取地址(&)、解引用()与引用(&) 指针(*)、取地址…

开发语言漫谈-PHP

PHP即“Hypertext Preprocessor”&#xff0c;怎么看都看不出怎么缩写为PHP。其实最初称为“Personal Home Page”。和之前介绍的语言不同&#xff0c;他是一种服务器端脚本语言&#xff0c;主要用于开发动态网页。PHP语法简单&#xff0c;而且具有跨平台的特性&#xff0c;开发…

python项目练习——29.贪吃蛇

游戏初始化: 设置游戏窗口的大小和标题。创建蛇的初始位置和长度。创建食物的初始位置。游戏主循环: 不断监听用户的输入,控制蛇的移动方向。检测蛇是否吃到了食物,如果吃到了,蛇的长度增加,食物重新生成在随机位置。检测蛇是否撞到了墙壁或者自己的身体,游戏结束。蛇的…

同旺科技 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…

C语言Linux vim

1. actionmotion dG删到文件尾 ggdG先到开头再删除到末尾 d^到达行首 d$到行尾 2. num action 2dd删除两行 t"向后寻找"找到&#xff0c;找到前面一个位置 f"向后寻找"找到&#xff0c;直接找到本来的位置 diw删除单词并保持在视图状态&#xff…

姑苏寻韵~庆开放原子开源大赛 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;帮助读者根据自己的需求做出明…

vue 如何调用子组件内的方法

在Vue中&#xff0c;子组件的方法可以通过父组件的该子组件的引用来调用。 在父组件中&#xff0c;给子组件添加一个ref属性&#xff0c;用来获取子组件的引用。例如&#xff1a; <template><div><child-component ref"child"></child-compon…

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 …

C语言入门算法——爬楼梯(了解动态规划)

题目描述&#xff1a; 假设你正在爬楼梯。需要 n 阶你才能到达楼顶。 每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢&#xff1f; 示例 1&#xff1a; 输入&#xff1a;n 2 输出&#xff1a;2 解释&#xff1a;有两种方法可以爬到楼顶。 1. 1 阶 1 阶…

java锁面试题

这里写目录标题 1.悲观锁和乐观锁2.悲观锁和乐观锁的场景3.自旋锁和自适应自旋锁4.无锁、偏向锁、轻量级锁、重量级锁5.公平锁和非公平锁6.可重入锁7.排他锁和共享锁8.锁优化技术 1.悲观锁和乐观锁 悲观锁&#xff1a;在修改数据时&#xff0c;一定有别的线程来使用&#xff0…

代码随想录算法训练营第三十二天| LeetCode122.买卖股票的最佳时机II、LeetCode55.跳跃游戏、LeetCode45.跳跃游戏II

LeetCode 122 买卖股票的最佳时机II 题目链接&#xff1a;122. 买卖股票的最佳时机 II - 力扣&#xff08;LeetCode&#xff09; 【解题思路】 利润当天成交价-昨天成交价 当遇到利润为正数的情况&#xff0c;将其收集。 【解题步骤】 1.定义一个result&#xff0c;将每天…