二叉排序树的删除有下面三种情况需要考虑:
1、删除叶子节点
2、删除只有一颗字树的节点
3、删除有两颗子树的节点
public class BinarySortTreeDemo {
public static void main(String[] args) {
int[] array = {7, 3, 10, 12, 5, 1, 9, 2};
// int[] array = {1, 2};
/*
7
/ \
3 10
/ \ / \
1 5 9 12
\
2
*/
BinaryTree binaryTree = new BinaryTree();
for (int i = 0; i < array.length; i++) {
binaryTree.add(new Node(array[i]));
}
binaryTree.infixOrder();
binaryTree.deleteNode(7);
System.out.println();
binaryTree.infixOrder();
}
}
class BinaryTree {
private Node root;
public void add(Node node) {
if (this.root == null) {
root = node;
} else {
root.add(node);
}
}
public Node search(int num) {
if (root == null) {
return null;
} else {
return root.search(num);
}
}
public Node searchParent(int num) {
if (root == null) {
return null;
} else {
return root.searchParent(num);
}
}
/**
* 删除右子树的最小值
* @param node
* @return
*/
public int delRightTreeMin(Node node) {
Node target = node;
while (target.left != null) {
target = target.left;
}
deleteNode(target.num);
return target.num;
}
/**
* 删除左子树的最大值
* @param node
* @return
*/
public int delLeftTreeMax(Node node) {
Node target = node;
while (target.right != null) {
target = target.right;
}
deleteNode(target.num);
return target.num;
}
public void deleteNode(int num) {
if (root == null) {
return;
}
Node targetNode = search(num);
if (targetNode == null) {
return;
}
if (root.left == null && root.right == null) { //如果根节点是唯一的节点,且为目标节点
root = null;
return;
}
Node parent = searchParent(num);
if (targetNode.left == null && targetNode.right == null) { //如果删除的是叶子结点
if (parent.left != null && parent.left.num == num) {
parent.left = null;
return;
} else if (parent.right != null && parent.right.num == num) { //如果删除的结点有两颗子树
parent.right = null;
return;
}
} else if (targetNode.left != null && targetNode.right != null) {
// int min = delRightTreeMin(targetNode.right);
// targetNode.num = min;
int max = delLeftTreeMax(targetNode.left);
targetNode.num = max;
} else { //如果删除的结点只有一颗子树
if (targetNode.left != null) {
if (parent != null) {
if (parent.left != null && parent.left == targetNode) {
parent.left = targetNode.left;
} else {
parent.right = targetNode.left;
}
} else {
root = targetNode.left;
}
} else {
if (parent != null) {
if (parent.left != null && parent.left == targetNode) {
parent.left = targetNode.right;
} else {
parent.right = targetNode.right;
}
} else {
root = targetNode.right;
}
}
}
}
public void infixOrder() {
if (root != null) {
root.infixOrder();
} else {
System.out.println("二叉树为空~");
}
}
}
class Node {
public int num;
public Node left;
public Node right;
public Node(int num) {
this.num = num;
}
@Override
public String toString() {
return num + "";
}
/**
* 中序遍历
*/
public void infixOrder() {
if (this.left != null) {
this.left.infixOrder();
}
System.out.print("=>" + this);
if (this.right != null) {
this.right.infixOrder();
}
}
/**
* 添加结点
* @param node
*/
public void add(Node node) {
if (node == null) {
return;
}
if (node.num < this.num) {
if (this.left != null) {
this.left.add(node);
} else {
this.left = node;
}
} else {
if (this.right != null) {
this.right.add(node);
} else {
this.right = node;
}
}
}
/**
* 查找结点
* @param num
* @return
*/
public Node search(int num) {
if (this.num == num) {
return this;
} else if (num < this.num) {
if (this.left != null) {
return this.left.search(num);
} else {
return null;
}
} else {
if (this.right != null) {
return this.right.search(num);
} else {
return null;
}
}
}
/**
* 查找删除节点的父节点
* @param num
* @return
*/
public Node searchParent(int num) {
if ((this.left != null && this.left.num == num) ||
(this.right != null && this.right.num == num)) {
return this;
} else {
if (num <= this.num && this.left != null) {
return this.left.searchParent(num);
} else if (num > this.num && this.right != null) {
return this.right.searchParent(num);
} else {
return null;
}
}
}
}