Java 实现二叉搜索树 代码

新建文件
创建TreeNode类,实例化

直接在BinarySearchTree类里面写就可以

java 复制代码
static class TreeNode {
        public int key;
        public TreeNode left;
        public TreeNode right;

        TreeNode(int key) {
            this.key = key;
        }
    }

    public TreeNode root;
插入节点 insert
java 复制代码
 public boolean insert(int key) {
        TreeNode node = new TreeNode(key);
        if(root==null){
            //检查是否为空树
            root=node;
            return true;//表示成功插入
        }
        TreeNode cur=root;//读取根节点
        TreeNode parent=null;
        //查找合适的位置 如果已经有了则返回false
        while(cur!=null){
            if(cur.key<key){
                parent=cur;
                cur=cur.right;
            } else if (cur.key>key) {
                parent=cur;
                cur=cur.left;
            }else{
                return false;
            }
        }
        //插入
        if(key< parent.key){
            parent.left=node;
        }else{
            parent.right=node;
        }
        return true;
    }

找到则返回该节点,没找到返回null

java 复制代码
public TreeNode search(int key) {
        if(root==null) return null;
        TreeNode cur=root;
        while(cur!=null){
            if(cur.key==key){
                return cur;
            }else if(cur.key>key){
                cur=cur.left;
            }else{
                cur=cur.right;
            }
        }
        return null;
    }
删除节点 remove

找到节点并删除,成功则返回true,失败false

java 复制代码
 public boolean remove(int key) {
        TreeNode cur=root;
        TreeNode parent=null;
        while(cur!=null){
            if(cur.key<key){
                parent=cur;
                cur=cur.right;
            } else if (cur.key>key) {
                parent=cur;
                cur=cur.left;
            }else{
                removeNode(parent,cur);//进行删除
                return true;
            }
        }
        return false;
    }

注意,这里删除又写了一个自定义函数。已至删除的位置和其双亲结点,如何让其删除呢?

如果左右子节点都在,则用左子树的最大值或者右子树的最小值替换即可,下面给出用右子树的最小值替换的方法。

java 复制代码
public  void removeNode(TreeNode parent,TreeNode cur){
        //在子树部分删除cur节点后重新构建子搜索二叉数
        if(cur.left==null){
            if(cur==root) root=cur.right;
            if(cur==parent.left) parent.left=cur.right;
            if(cur==parent.right) parent.right=cur.right;
        }else if(cur.right==null){
            if(cur==root) root=cur.left;
            if(cur==parent.left) parent.left=cur.left;
            if(cur==parent.right) parent.right=cur.left;
        }else{
            TreeNode target=cur.right;
            TreeNode targetParent=cur;
            while(target.left!=null){
                targetParent=target;
                target=target.left;
            }
            cur.key= target.key;
            if(target==targetParent.left) {
                targetParent.left = target.right;
            }else{
                targetParent.right = target.right;
            }
        }
    }

如有什么没讲清楚的地方,欢迎留言探讨!

相关推荐
二哈赛车手1 天前
新人笔记---ApiFox的一些常见使用出错
java·笔记·spring
为何创造硅基生物1 天前
C语言 结构体内存对齐规则(通俗易懂版)
c语言·开发语言
吃好睡好便好1 天前
在Matlab中绘制横直方图
开发语言·学习·算法·matlab
栗子~~1 天前
JAVA - 二层缓存设计(本地缓冲+redis缓冲+广播所有本地缓冲失效) demo
java·redis·缓存
星寂樱易李1 天前
iperf3 + Python-- 网络带宽、网速、网络稳定性
开发语言·网络·python
YDS8291 天前
DeepSeek RAG&MCP + Agent智能体项目 —— RAG知识库的搭建和接口实现
java·ai·springboot·agent·rag·deepseek
仰泳之鹅1 天前
【C语言】自定义数据类型2——联合体与枚举
c语言·开发语言·算法
之歆1 天前
DAY_12JavaScript DOM 完全指南(二):实战与性能篇
开发语言·前端·javascript·ecmascript
未若君雅裁1 天前
MyBatis 一级缓存、二级缓存与清理机制
java·缓存·mybatis
cen__y1 天前
Linux12(Git01)
linux·运维·服务器·c语言·开发语言·git