二叉排序树的删除
删除会有三种情况
- 删除叶子节点
-
删除只有一棵子树的节点
-
删除含有两棵子树的节点
思路图的分析
第一种情况: 删除叶子节点
1.需要先去找到要删除的节点 targetNode
2.找到targetNode的父节点parent
3.确定targetNode是parentNode的左子节点还是右子节点
4.根据目前的情况来对应删除
第二种情况:删除只有一棵子树的节点
1.需要先去找到要删除的节点 targetNode
2.找到targetNode的父节点parentNode
3.确定targetNode的子节点是左子节点还是有子节点
4.targetNode是parentNode的左子节点还是右子节点
。。。。。。。。
。。。。。。。。
(不总结了)
直接上代码
package bstTree;
public class BinarysortTreeTest {
public static void main(String[] args) {
BinarySortTree2 bst = new BinarySortTree2(null);
int arr[] = { 7, 3, 10, 12, 5, 1, 9, 2 }; // 把这个数据元素添加到这个集合进行处理 //添加元素
for (int i = 0; i < arr.length; i++) {
bst.add(new Node2(arr[i]));
}
// 模拟删除
bst.delNode(3);
// 遍历元素
bst.infixOrder();
}
}
//这是一颗二叉排序树
class BinarySortTree2 {
Node2 root;
public BinarySortTree2(Node2 root) {
this.root = root;
}
// 添加
public void add(Node2 node) {
if (root == null) {
root = node;
} else {
root.add(node);
}
}
// 遍历
public void infixOrder() {
if (root == null) {
System.out.println("这个节点为空");
} else {
root.infixOrder();
}
}
// 查找要删除的节点
public Node2 search(int value) {
if (root == null) {
return null;
} else {
return root.search(value);
}
}
// 查找要删除节点的父节点
public Node2 searchParent(int value) {
if (root == null) {
return null;
} else {
return root.searchParent(value);
}
}
// 编写方法
// 1.返回以node为根节点的二叉树的最小节点的值
// 2.删除node为为根节点的二叉排序树的最小节点
//
/**
*
* @param node 传入的节点(当作二叉排序的根节点)
* @return 返回以node为根节点的二叉树的最小节点的值
*/
public int delLeftTreeMin(Node2 node) {
Node2 target = node;
// 循环查找左子节点,就会找到最小值、
while (target.left != null) {
target = target.left;
}
int temp = target.value;
delNode(target.value);
return temp;
}
// 删除节点
public void delNode(int value) {
if (root == null) {
return;
} else {
// 需要先找到要删除的节点
Node2 targetNode = search(value);
if (targetNode == null) {
return;
}
// 如果我们发现当前这棵二叉排序树只有一颗节点
if (root.left == null && root.right == null) {
root = null;
return;
}
// 去找到targetNode的父节点
Node2 parentNode = searchParent(value);
// ^^^^^^______^^^^^^如果要删除的为叶子节点
if (targetNode.left == null && targetNode.right == null) {
// 判断targetNode节点是父亲的左子节点还是右子节点
if (parentNode.left != null && parentNode.left.value == value) {
// 说明这个子节点就是父亲的左子节点
parentNode.left = null;
} else if (parentNode.right != null && parentNode.right.value == value) {
parentNode.right = null;
}
} else if (targetNode.left != null && targetNode.right != null) {
// ^^^^^^______^^^^^^如果要删除的节点为有两颗子树的节点
int temp = delLeftTreeMin(targetNode.right);
targetNode.value = temp;
} else {
// 删除只有一棵子树的节点
// 如果要删除的节点有左子节点
if (targetNode.left != null) {
// 如果targetNode 是 parent的左子节点
if (parentNode.left.value == value) {
parentNode.left = targetNode.left;
} else {
parentNode.right = targetNode.left;
}
} else {
// 如果要删除的节点有右子节点
if (parentNode.left.value == value) {
parentNode.left = targetNode.right;
} else {
parentNode.right = targetNode.right;
}
}
}
}
}
}
//建立树的节点
class Node2 {
int value;
Node2 left;
Node2 right;
public Node2(int value) {
this.value = value;
}
// 遍历
public void infixOrder() {
if (this.left != null) {
this.left.infixOrder();
}
System.out.println(this.value);
if (this.right != null) {
this.right.infixOrder();
}
}
// 添加
public void add(Node2 node) {
if (node == null) {
return;
}
if (this.value > node.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 Node2 search(int value) {
if (value == this.value) {
return this;
} else 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 Node2 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;
}
}
}
}