一些说明
主要是在删除时,有三种形态需要说明一下,方便写代码时的思路:
红
色
\color{red}{红色}
红色代表要删除的节点。
- 要删除的节点是叶子结点
- 要删除的节点有左节点但是没有右节点,或者有右节点但是没有左节点
- 要删除的节点既有左节点又有右节点,在这种情况下,我们只需要将找到待删节点的右子树中值最小的节点,将其删除并且获取其值,并用其值替换待删节点的值即可.
具体代码实现如下:
/**
* @Description 二叉排序树
* @Author lvxinchun
* @Date 2020-08-06 13:37
*/
public class BinarySortTree {
public class TreeNode {
int value;
TreeNode left;
TreeNode right;
public TreeNode(int value) {
this.value = value;
}
public void add(TreeNode treeNode) {
if (treeNode == null) {
return;
}
if (treeNode.value < this.value) {
if (this.left == null) {
this.left = treeNode;
} else {
this.left.add(treeNode);
}
} else {
if (this.right == null) {
this.right = treeNode;
} else {
this.right.add(treeNode);
}
}
}
/**
* 前序遍历
*
* @param treeNode
*/
public void preOrder(TreeNode treeNode) {
if (treeNode == null) {
return;
}
System.out.println(treeNode.value);
preOrder(treeNode.left);
preOrder(treeNode.right);
}
/**
* 中序遍历
*/
public void midOrder(TreeNode treeNode) {
if (treeNode == null) {
return;
}
midOrder(treeNode.left);
System.out.println(treeNode.value);
midOrder(treeNode.right);
}
/**
* 后序遍历
*/
public void postOrder(TreeNode treeNode) {
if (treeNode == null) {
return;
}
postOrder(treeNode.left);
postOrder(treeNode.right);
System.out.println(treeNode.value);
}
/**
* 查找某一点
*
* @param value
* @return
*/
public TreeNode search(int value) {
if (this.value == value) {
return this;
} else if (this.value < value) {
if (this.right == null) {
return null;
}
return this.right.search(value);
} else {
if (this.left == null) {
return null;
}
return this.left.search(value);
}
}
/**
* 寻找父节点
*
* @return
*/
public TreeNode searchParent(int value) {
if (this.left != null && this.left.value == value) {
return this;
} else if (this.right != null && this.right.value == value) {
return this;
} else {
if (this.value > value && this.left != null) {
return this.left.searchParent(value);
} else if (this.value < value && this.right != null) {
return this.right.searchParent(value);
}
}
return null;
}
}
private TreeNode root;
public void add(TreeNode treeNode) {
if (root == null) {
root = treeNode;
} else {
root.add(treeNode);
}
}
public TreeNode search(int value) {
if (root == null) {
return null;
} else {
return root.search(value);
}
}
public TreeNode searchParent(int value) {
if (root == null) {
return null;
}
return root.searchParent(value);
}
public void delete(int value) {
if (root == null) {
return;
} else {
TreeNode targetNode = search(value);
if (targetNode == null) {
return;
}
//找到它的父节点
TreeNode parent = searchParent(value);
//如果要删除的节点是叶子节点
if (targetNode.left == null && targetNode.right == null) {
if (targetNode.left.value == value) {
parent.left = null;
} else {
parent.right = null;
}
} else if (targetNode.left != null && targetNode.right != null) {
//如果要删除的节点有左右两个子节点
int min = minDelete(targetNode.right);
targetNode.value = min;
} else {
//需要删除的目标节点的左节点不为空,没有右节点
if (targetNode.left != null) {
//要删除的子节点是其父节点的左子节点,并且该子节点没有右节点
if (parent.left.value == value) {
parent.left = targetNode.left;
} else {
parent.right = targetNode.left;
}
} else {
//需要删除的目标节点的右节点不为空,没有左节点
if (targetNode.right != null) {
//要删除的子节点是其父节点的右子节点,并且该子节点没有
if (parent.left.value == value) {
parent.left = targetNode.right;
} else {
parent.right = targetNode.right;
}
}
}
}
}
}
/**
* 删除tree最小的一个节点
*
* @param treeNode
* @return
*/
public int minDelete(TreeNode treeNode) {
TreeNode targetDelTreeNode = treeNode;
while (targetDelTreeNode.left != null) {
targetDelTreeNode = targetDelTreeNode.left;
}
delete(targetDelTreeNode.value);
return targetDelTreeNode.value;
}
public static void main(String[] args) {
int[] arra = new int[]{3,4,2, 9, 7, 12, 14, 1, 21, 19, 18};
BinarySortTree binarySortTree = new BinarySortTree();
for (int i : arra) {
binarySortTree.add(binarySortTree.new TreeNode(i));
}
System.out.println(binarySortTree);
}
}