二叉搜索树(二叉排序树)

二叉搜索树(二叉排序树)一 概念二叉搜索树又称二叉排序树 具有以下性质 若它的左子树不为空 则左子树上所有节点的值都小于根节点的值若它的右子树不为空 则右子树上所有节点的值都大于根节点的值它的左右子树也分别为二叉搜索树注意 二叉搜索树中序遍历的结果是有序的二 基本操作 1 查找元素思路 二叉搜索树的左子树永远是比根节点小的 而它的右子树则都是比根节点大的值 当前节点比要找的大就往左走 当前元素比要找的小就往右走 publicNodese intkey if root

一.概念

二叉搜索树又称二叉排序树,具有以下性质:

  • 若它的左子树不为空,则左子树上所有节点的值都小于根节点的值
  • 若它的右子树不为空,则右子树上所有节点的值都大于根节点的值
  • 它的左右子树也分别为二叉搜索树

注意:二叉搜索树中序遍历的结果是有序的
在这里插入图片描述

二、基本操作

1.查找元素

思路:二叉搜索树的左子树永远是比根节点小的,而它的右子树则都是比根节点大的值。当前节点比要找的大就往左走,当前元素比要找的小就往右走

 public Node search(int key) { 
    if(root == null) { 
    return null; } Node cur = root; while (cur != null) { 
    if(cur.val == key) { 
    return cur; }else if(cur.val > key) { 
    cur = cur.left; }else{ 
    cur = cur.right; } } return null; } 

2.插入元素

在这里插入图片描述
代码实现:

 public boolean insert(int key) { 
    Node node = new Node(key); if(root == null) { 
    this.root = node; return true; } Node parent = null; Node cur = root; while (cur != null) { 
    if(cur.val == key) { 
    //有相同的元素直接return return false; }else if(cur.val > key) { 
    parent = cur; cur = cur.left; }else{ 
    parent = cur; cur = cur.right; } } if (parent.val > key) { 
    parent.left = node; }else{ 
    parent.right = node; } return true; } 

3.删除元素

删除元素是一个比较难的点,要考虑到很多种情况

  1. cur.left == null
    1. cur 是 root,则 root = cur.right
    2. cur 不是 root,cur 是 parent.left,则 parent.left = cur.right
    3. cur 不是 root,cur 是 parent.right,则 parent.right = cur.right
  2. cur.right == null
    1. cur 是 root,则 root = cur.left
    2. cur 不是 root,cur 是 parent.left,则 parent.left = cur.left
    3. cur 不是 root,cur 是 parent.right,则 parent.right = cur.left
  3. cur.left != null && cur.right != null
    采用替罪羊的方式删除

    1. 找到要删除节点,右树最左边的节点或者找到左树最右边的节点,替换这个两个节点的val值。
    2. 这样才能保证,删除后左树一定比根节点小,右树一定比根节点大
      在这里插入图片描述

 public boolean remove(int key) { 
    if(this.root == null) { 
    return false; } Node parent = null; Node cur = this.root; while (cur != null) { 
    if(cur.val == key) { 
    removeKey(parent,cur); return true; }else if(cur.val < key) { 
    parent = cur; cur = cur.right; }else{ 
    parent = cur; cur = cur.left; } } return false; } public void removeKey(Node parent,Node cur) { 
    if(cur.left == null) { 
    if(cur == this.root) { 
    this.root = this.root.right; }else if(cur == parent.left) { 
    parent.left = cur.right; }else{ 
    parent.right = cur.right; } }else if(cur.right == null) { 
    if(this.root == cur) { 
    this.root = this.root.left; }else if(cur == parent.left) { 
    parent.left = cur.left; }else{ 
    parent.right = cur.left; } }else{ 
   //左右都不为空的情况 Node targetParent = cur; Node target = cur.right; while (target.left != null) { 
    targetParent = target; target = target.left; } cur.val = target.val; if(targetParent.left == target) { 
    targetParent.left = target.right; }else{ 
    targetParent.right = target.right; } } } 

4.性能分析

最好情况:二叉搜索树为完全二叉树,其平均比较次数为 O(log 2 _2 2n)

在这里插入图片描述

最坏情况:二叉搜索树退化为单支树,其平均比较次数为:O

在这里插入图片描述

所有代码:

public class BinarySearchTree { 
    public static class Node { 
    int val; Node left; Node right; public Node(int val) { 
    this.val = val; } } public Node root = null; / * 查找某个节点 * @param key */ public Node search(int key) { 
    if(root == null) { 
    return null; } Node cur = root; while (cur != null) { 
    if(cur.val == key) { 
    return cur; }else if(cur.val > key) { 
    cur = cur.left; }else{ 
    cur = cur.right; } } return null; } / * 插入元素 * @param key * @return */ public boolean insert(int key) { 
    Node node = new Node(key); if(root == null) { 
    this.root = node; return true; } Node parent = null; Node cur = root; while (cur != null) { 
    if(cur.val == key) { 
    //有相同的元素直接return return false; }else if(cur.val > key) { 
    parent = cur; cur = cur.left; }else{ 
    parent = cur; cur = cur.right; } } if (parent.val > key) { 
    parent.left = node; }else{ 
    parent.right = node; } return true; } / * 删除元素 * @param key */ public boolean remove(int key) { 
    if(this.root == null) { 
    return false; } Node parent = null; Node cur = this.root; while (cur != null) { 
    if(cur.val == key) { 
    removeKey(parent,cur); return true; }else if(cur.val < key) { 
    parent = cur; cur = cur.right; }else{ 
    parent = cur; cur = cur.left; } } return false; } public void removeKey(Node parent,Node cur) { 
    if(cur.left == null) { 
    if(cur == this.root) { 
    this.root = this.root.right; }else if(cur == parent.left) { 
    parent.left = cur.right; }else{ 
    parent.right = cur.right; } }else if(cur.right == null) { 
    if(this.root == cur) { 
    this.root = this.root.left; }else if(cur == parent.left) { 
    parent.left = cur.left; }else{ 
    parent.right = cur.left; } }else{ 
    Node targetParent = cur; Node target = cur.right; while (target.left != null) { 
    targetParent = target; target = target.left; } cur.val = target.val; if(targetParent.left == target) { 
    targetParent.left = target.right; }else{ 
    targetParent.right = target.right; } } } } 
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/217802.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月18日 上午8:44
下一篇 2026年3月18日 上午8:44


相关推荐

  • 图像拼接算法总结(一)

    图像拼接算法总结(一)图像的拼接技术包括三大部分 特征点提取与匹配 图像配准 图像融合 1 基于 SRUF 的特征点的提取与匹配为了使拼接具有良好的精度和鲁棒性 同时又使其具有较好的实时性 本实验采用 SURF 算法完成图像序列特征点的提取 SURF 算法又称快速鲁棒特征 借鉴了 SIFT 中简化近似的思想 将 DoH 中的高斯二阶微分模板进行了近似简化 使得模板对图像的滤波只需要进行几个简单的加减法运算 并且这种

    2026年3月26日
    2
  • 月之暗面K2.5模型开源,全面提升多模态智能与Agent集群能力!

    月之暗面K2.5模型开源,全面提升多模态智能与Agent集群能力!

    2026年3月12日
    2
  • dsp28335复位电路_28335串口不能中断

    dsp28335复位电路_28335串口不能中断0前言本期实验目标:采用外部中断方式响应按键触发,实现LED电平反转。外部中断是DSP十分常用的功能,通常用来响应一些控制操作,比如判断按键是否按下,传感器是否接收到信号等等。那么通过该例程,大家则可以快速学会使用外部中断的功能!本节仍然将分为硬件部分、软件部分和实验展示三个方面进行介绍。1硬件部分DSP28335支持XINT1-XINT7和XNMI共8路外部中断源,其中中断源XINT1/2和XNMI可以设定为从GPIO端口A的任意一个管脚输入,即GPIO0-GPIO31。而XINT3/4/5/

    2025年9月26日
    7
  • 无人驾驶感知篇之超声波雷达

    无人驾驶感知篇之超声波雷达昨天上海又新增了快六千多例,早上醒来的第一眼都很关注,这个时候,在想如果无人驾驶送餐车在各个街道行驶送餐那该多好,希望这一天能早点到来,让无人驾驶遍布咱们生活的每个角落。OK,言归正传,首先讲讲什么是超声波雷达。1.什么是超声波雷达安装在汽车周边的超声波雷达,主要用于倒车时的防撞报警系统,又俗称倒车雷达。超声波是一种在弹性介质中的机械振荡,纵向分辨率较高,对色彩、光照、电磁场不敏感,因此超声波测距系统对于黑暗,有灰尘或者烟幕、有毒等恶劣环境有很强的适应能力。超声波测距…

    2025年10月27日
    4
  • shell脚本语言(超全超详细)[通俗易懂]

    shell脚本语言(超全超详细)[通俗易懂]shell脚本语言1、shell的概述2、脚本的调用形式打开终端时系统自动调用:/etc/profile或~/.bashrc3、shell语法初识3.1、定义以开头:#!/bin/bash3.2、单个”#”号代表注释当前行第一步:编写脚本文件第二步:加上可执行权限第三步:运行三种执行方式(./xxx.shbashxxx.sh.xxx.sh)./xxx.sh…

    2022年7月11日
    21
  • R 笔记 prophet[通俗易懂]

    R 笔记 prophet[通俗易懂]0理论部分论文笔记:ForecastingatScale(Prophet)_UQI-LIUWJ的博客-CSDN博客Prophet是一种基于加法模型预测时间序列数据的程序,其中非线性趋势、季节性以及假日效应相匹配。它最适用于具有强烈季节性和有几个季节历史数据的时间序列。Prophet对缺失数据和趋势变化具有鲁棒性,并且通常可以很好地处理异常值。…

    2022年6月18日
    72

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号