二叉排序树删除节点详解
说明
- 二叉排序树有着比数组查找快,比链表增删快的优势,比较常见
- 二叉排序树要删除节点,首先要找到该节点和该节点对应的父节点,因为只根据当前节点是不能删除节点本身的,因此需要找到父节点
- 二叉排序树删除节点,根据节点所在的位置不同,需要分为三种情况
- 即要删除的节点是叶子节点,要删除的节点只有一颗子树的情况和要删除的节点有两颗子树的情况
- 考虑第一种情况,即要删除的节点是叶子节点
- 直接找到要删除节点的父节点,然后置空即可
- 考虑第二种情况,即要删除的节点有一颗子树
- 先找到该节点和该节点的父节点,根据该节点是父节点的左子节点还是右子节点和该节点有左子节点还是有有子节点,应该分为四种情况讨论,每种情况改变父节点的引用即可实现删除
- 考虑第三种情况,即要删除的节点有两颗子树
- 这种情况删除思路应该为找到当前节点右子树的的最大值节点或者找到当前节点左子树的最小值节点,先将这个节点删除,再用这个节点替换当前节点
- 实质操作先记录删除子树的最大最小值节点的值,然后删除这个节点,最后用记录的这个值替换掉要删除的节点,实现删除有左右子树节点的思路
- 源码及思路见下
源码及分析
package algorithm.tree.bst;
public class BinarySortTree {
public static void main(String[] args) {
int[] arr = {7, 3, 10, 12, 5, 1, 9, 2};
BST bst = new BST();
for (int i = 0; i < arr.length; i++) {
bst.add(new Node(arr[i]));
}
System.out.println("中序遍历");
bst.infixOrder();
System.out.println("删除叶子节点");
bst.delNode(7);
bst.infixOrder();
}
}
class BST {
private Node root;
public void infixOrder() {
if (root != null) {
root.infixOrder();
} else {
System.out.println("树是空的");
}
}
public void add(Node node) {
if (root == null) {
root = node;
} else {
root.add(node);
}
}
public Node search(int value) {
if (root == null) {
return null;
} else {
return root.search(value);
}
}
public Node searchParent(int value) {
if (root == null) {
return null;
} else {
return root.searchParent(value);
}
}
public void delNode(int value) {
if (root == null) {
return;
} else {
if (root.left == null && root.right == null && root.value == value) {
root = null;
return;
}
Node target = search(value);
if (target == null) {
return;
}
Node parent = searchParent(value);
if (target.left == null && target.right == null) {
if (parent.left != null && parent.left.value == value) {
parent.left = null;
}
if (parent.right != null && parent.right.value == value) {
parent.right = null;
}
} else if (target.left != null && target.right != null) {
int minVal = delRightNodeMin(target.right);
target.value = minVal;
} else {
if (target.left != null){
if (parent != null) {
if (parent.left.value == value) {
parent.left = target.left;
} else {
parent.right = target.left;
}
}else {
root = target.left;
}
}else {
if (parent != null) {
if (parent.left.value == value) {
parent.left = target.right;
} else {
parent.right = target.right;
}
}else {
root = target.right;
}
}
}
}
}
public int delRightNodeMin(Node node){
Node target = node;
while (target.left != null){
target = target.left;
}
delNode(target.value);
return target.value;
}
}
class Node {
int value;
Node left;
Node right;
public Node(int value) {
this.value = value;
}
public Node search(int value) {
if (value == this.value) {
return this;
}
if (value < this.value) {
if (this.left == null) {
return null;
}
return this.left.search(value);
} else {
if (this.right == null) {
return null;
}
return this.right.search(value);
}
}
public Node searchParent(int value) {
if ((this.left != null && this.left.value == value) ||
(this.right != null && this.right.value == value)) {
return this;
} else {
if (value < this.value && this.left != null) {
return this.left.searchParent(value);
} else if (value >= this.value && this.right != null) {
return this.right.searchParent(value);
} else {
return null;
}
}
}
public void add(Node node) {
if (node == null) {
return;
}
if (node.value < this.value) {
if (this.left == null) {
this.left = node;
} else {
this.left.add(node);
}
} else {
if (this.right == null) {
this.right = node;
} else {
this.right.add(node);
}
}
}
public void infixOrder() {
if (this.left != null) {
this.left.infixOrder();
}
System.out.println(this);
if (this.right != null) {
this.right.infixOrder();
}
}
@Override
public String toString() {
return "Node{" +
"value=" + value +
'}';
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)