1. 基本介绍
二叉排序树BST: (Binary Sort(Search) Tree), 对于二叉排序树的任何一个非叶子节点,要求左子节点的值比当前节点的值小,右子节点的值比当前节点的值大。如果有相同的值,可以将该节点放在左子节点或右子节点。
2. 二叉排序树的创建和遍历
一个数组创建成对应的二叉排序树,并使用中序遍历二叉排序树,比如: 数组为 Array(7, 3, 10, 12, 5, 1, 9) , 创建成对应的二叉排序树如下所示 :
二叉排序树的创建即为一个个节点的添加,根据二叉排序树的特点,二叉排序树的遍历需要使用中序遍历。下面定义一个类作为树节点:
class Node {
int value;
Node left;
Node right;
public Node(int value) {
this.value = value;
}
/**
* 递归处理添加节点
*
* @param node
*/
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 +
'}';
}
}
3. 二叉排序树的删除
二叉排序树的删除情况比较复杂,有下面三种情况需要考虑
例如对于数列为{7,3,10,12,5,1,9}对应的二叉排序树,
如果是
- 删除叶子节点 (如叶子节点有2, 5, 9, 12)
- 删除只有一颗子树的节点 (比如:1)
- 删除有两颗子树的节点(比如:7, 3,10 )
3.1 删除叶子节点
实现步骤如下所示:
- 需求先去找到要删除的结点 targetNode
- 找到 targetNode 的 父结点 parent
- 确定 targetNode 是 parent 的左子结点 还是右子结点
- 根据前面的情况来对应删除:如果左子结点 则parent.left = null ;如果右子结点,则parent.right = null。
/**
* 二叉排序树
*/
class BinarySortTree {
/**
* 查找要删除的节点
*
* @param value
*/
public Node 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);
}
}
/**
* 查找要删除的父节点
*
* @param value
* @return
*/
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;
}
}
}
}
目前只考虑删除叶子节点,删除节点的方法如下:
/**
* 删除节点
*
* @param value
*/
public void delNode(int value) {
Node targetNode = search(value);
if (targetNode == null) {
System.out.println("找不到目标节点");
return;
}
Node parent = searchParent(value);
//如果目标节点是叶子节点的情况
if (targetNode.left == null && targetNode.right == null) {
if (parent.left != null && parent.left.value == value) {
parent.left = null;
} else {
parent.right = null;
}
}
}
3.2 删除只有一颗子树的节点
实现步骤如下所示:
(1) 先去找到要删除的结点 targetNode
(2) 找到 targetNode 的 父结点 parent
(3) 确定 targetNode 的子结点是左子结点还是右子结点
(4) targetNode 是 parent 的左子结点还是右子结点
(5) 如果 targetNode 有左子结点
5.1 如果 targetNode 是 parent 的左子结点parent.left = targetNode.left;
5.2 如果 targetNode 是 parent 的右子结点 parent.right = targetNode.left;
(6) 如果 targetNode 有右子结点
6.1 如果 targetNode 是 parent 的左子结点 parent.left = targetNode.right;
6.2 如果 targetNode 是 parent 的右子结点 parent.right = targetNode.right
考虑上删除只有一颗子树的节点的情况,补充代码:
/**
* 删除节点
*
* @param value
*/
public void delNode(int value) {
Node targetNode = search(value);
if (targetNode == null) {
System.out.println("找不到目标节点");
return;
}
Node parent = searchParent(value);
//如果目标节点是叶子节点的情况
if (targetNode.left == null && targetNode.right == null) {
if (parent.left != null && parent.left.value == value) {
parent.left = null;
} else {
parent.right = null;
}
}
//如果目标节点有一个子树的情况
else {
//目标节点有一个左子树
if (targetNode.left != null) {
if (parent != null) {
//判断该目标节点是其父节点的左子节点还是右子结点
if (parent.left != null && parent.left.value == value) {
parent.left = targetNode.left;
} else {
parent.right = targetNode.left;
}
} else {
parent = targetNode.left;
}
}
//目标节点有一个右子树
else {
if (parent != null) {
//判断该目标节点是其父节点的左子节点还是右子结点
if (parent.right != null && parent.right.value == value) {
parent.right = targetNode.right;
} else {
parent.left = targetNode.right;
}
} else {
parent = targetNode.right;
}
}
}
}
3.3 删除有两颗子树的节点
实现步骤如下所示
(1) 先去找到要删除的结点 targetNode
(2) 找到 targetNode 的 父结点 parent
(3) 从 targetNode 的右子树找到最小的结点或者从左子树找到最大的节点
(4) 用一个临时变量,将 最小(大)结点的值保存到temp变量
(5) 删除该最小(大)结点
(6) targetNode.value = temp
考虑删除有两颗子树的节点,补充代码:
/**
* 删除节点
*
* @param value
*/
public void delNode(int value) {
Node targetNode = search(value);
if (targetNode == null) {
System.out.println("找不到目标节点");
return;
}
Node parent = searchParent(value);
//如果目标节点是叶子节点的情况
if (targetNode.left == null && targetNode.right == null) {
if (parent.left != null && parent.left.value == value) {
parent.left = null;
} else {
parent.right = null;
}
}
//如果目标节点有两个子树的情况
else if (targetNode.left != null && targetNode.right != null) {
// int minVal = delRightTreeMin(targetNode);
// targetNode.value = minVal;
int maxVal = delLeftTreeMax(targetNode);
targetNode.value = maxVal;
}
//如果目标节点有一个子树的情况
else {
//目标节点有一个左子树
if (targetNode.left != null) {
if (parent != null) {
//判断该目标节点是其父节点的左子节点还是右子结点
if (parent.left != null && parent.left.value == value) {
parent.left = targetNode.left;
} else {
parent.right = targetNode.left;
}
} else {
parent = targetNode.left;
}
}
//目标节点有一个右子树
else {
if (parent != null) {
//判断该目标节点是其父节点的左子节点还是右子结点
if (parent.right != null && parent.right.value == value) {
parent.right = targetNode.right;
} else {
parent.left = targetNode.right;
}
} else {
parent = targetNode.right;
}
}
}
}
/**
* 找到右子树最小的值的节点,并删除该节点
*
* @param node
* @return
*/
private int delRightTreeMin(Node node) {
Node target = node.right;
//循环的查找右子结点
while (target.left != null) {
target = target.left;
}
//找到了最小的节点
delNode(target.value);
return target.value;
}
/**
* 找到左子树最大的值的节点,并删除该节点
*
* @param node
* @return
*/
private int delLeftTreeMax(Node node) {
Node target = node.left;
//循环查找左子树的最大值
while (target.right != null) {
target = target.right;
}
delNode(target.value);
return target.value;
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/44300.html