public class BinarySortTreeDemo {
public static void main(String[] args) {
int[] arr = {7, 3, 10, 12, 5, 1, 9, 2};
BinarySortTree binarySortTree = new BinarySortTree();
for (int i : arr) {
binarySortTree.add(new Node(i));
}
// 7
// / \
// 3 10
// / \ / \
// 1 5 9 12
// \
// 2
System.out.println("删除前中序遍历的结果为:");
binarySortTree.inOrder();
binarySortTree.delNode(8);
System.out.println("删除后中序遍历的结果为:");
binarySortTree.inOrder();
}
}
class BinarySortTree {
private Node root;
// 添加结点
public void add(Node node) {
if (this.root == null) {
this.root = node;
} else {
this.root.add(node);
}
}
// 查找删除的结点
public Node searchNode(int value) {
if (this.root != null) {
return root.searchNode(value);
} else {
return null;
}
}
// 查找删除结点的父结点
public Node searchParentNode(int value) {
if (this.root != null) {
return root.searchParentNode(value);
} else {
return null;
}
}
public int minRightTree(Node node) {
Node temp = node;
// 循环寻找以node为根结点的二叉树的最小值
while (temp.left != null) {
temp = temp.left;
}
// 删除这个最小值的结点
delNode(temp.value);
return temp.value;
}
// 删除结点
public void delNode(int value) {
if (root != null) {
// 先找到要删除的结点
Node targetNode = this.searchNode(value);
// 没有找到要删除的结点
if (targetNode == null) {
System.out.println("删除的结点不存在!");
return;
}
Node parentNode = this.searchParentNode(value);
// 如果只有一个结点,并且要删除这个结点
if (root.left == null && root.right == null) {
root = null;
return;
}
// 如果不止一个结点
// <1>如果要删除的是叶子结点
if (targetNode.left == null && targetNode.right == null) {
if (parentNode.left != null && parentNode.left.value == value) {
parentNode.left = null;
} else if (parentNode.right != null && parentNode.right.value == value) {
parentNode.right = null;
}
// <2>如果要删除的结点有两个子结点
} else if (targetNode.left != null && targetNode.right != null) {
int minValue;
minValue = minRightTree(targetNode.right);
targetNode.value = minValue;
// <3>如果要删除的结点只有左子结点
} else {
if (targetNode.left != null) {
if (parentNode != null) {
// target是parent的左子结点
if (parentNode.left.value == value) {
parentNode.left = targetNode.left;
// target是parent的右子结点
} else {
parentNode.right = targetNode.left;
}
} else {
root = targetNode.left;
}
// <4>如果要删除的结点只有右子结点
} else {
if (parentNode != null) {
// target是parent的左子结点
if (parentNode.left.value == value) {
parentNode.left = targetNode.right;
// target是parent的右子结点
} else {
parentNode.right = targetNode.right;
}
} else {
root = targetNode.right;
}
}
}
}
}
// 中序遍历
public void inOrder() {
if (this.root != null) {
this.root.inOrder();
} else {
System.out.println("二叉树为空,无法遍历!");
}
}
}
class Node {
public int value;
public Node left;
public Node right;
public Node(int value) {
this.value = value;
}
@Override
public String toString() {
return "Node{" +
"value=" + value +
'}';
}
// 递归的方式添加结点,满足二叉排序树的要求
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 Node searchNode(int value) {
if (value == this.value) {
return this;
} else if (value < this.value) {
if (this.left == null) {
return null;
}
return this.left.searchNode(value);
} else {
if (this.right == null) {
return null;
}
return this.right.searchNode(value);
}
}
// 查找要删除的结点的父结点
public Node searchParentNode(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.searchParentNode(value);
} else if (value >= this.value && this.right != null) {
// 如果查找的值大于当前结点的值,并且当前结点的右子结点不为空
return this.right.searchParentNode(value);
} else {
// 没有找到父结点
return null;
}
}
}
// 中序遍历
public void inOrder() {
if (this.left != null) {
this.left.inOrder();
}
System.out.println(this);
if (this.right != null) {
this.right.inOrder();
}
}
}
datastructure:二叉排序树
最新推荐文章于 2025-05-09 17:11:59 发布