一:二叉排序树的特点
-
BST: (Binary Sort(Search) Tree), 对于二叉排序树的 任何一个非叶子节点 ,要求 左子节点的值比当 前节点的值小 , 右子节点的值比当前节点的值大。
-
特别说明 :如果有相同的值,可以将该节点放在左子节点或右子节点。
![](https://img-blog.csdnimg.cn/20210315221115454.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM3NDY5MDU1,size_16,color_FFFFFF,t_70)
1:二叉树的添加和遍历比较简单,这个没什么好说的,看之前的博客讲树的遍历就可以实现。
2:二叉排序树的删除
二叉排序树的删除情况比较复杂,有下面三种情况需要考虑
- 删除叶子节点 (比如:2, 5, 9, 12)
- 删除只有一颗子树的节点 (比如:1)
- 删除有两颗子树的节点. (比如:7, 3,10 )
操作的思路分析:
第一种情况:
删除叶子节点 (比如:2, 5, 9, 12)
思路
(1) 需求先去找到要删除的结点 targetNode
(2) 找到 targetNode 的 父结点 parent
(3) 确定 targetNode 是 parent 的左子结点 还是右子结点
(4) 根据前面的情况来对应删除
左子结点 parent.left = null
右子结点 parent.right = null;
第二种情况: 删除只有一颗子树的节点 比如 1
思路
(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
情况三 : 删除有两颗子树的节点. (比如:7, 3,10 )
思路
(1) 需求先去找到要删除的结点 targetNode
(2) 找到 targetNode 的 父结点 parent
(3) 从 targetNode 的右子树找到最小的结点
(4) 用一个临时变量,将 最小结点的值保存 temp = 11
(5) 删除该最小结点
(6) targetNode.value = temp
3:代码的实现
Node实体类
package com.tree.binarysort;
/**
* @author lizhangyu
* @date 2021/3/15 19:55
*/
public 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 midOrder() {
if (this.left != null) {
this.left.midOrder();
}
System.out.println(this);
if (this.right != null) {
this.right.midOrder();
}
}
/**
* 添加节点
* @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);
}
}
}
/**
* 搜索当前节点
* @param value
* @return
*/
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 (this.left != null && value < this.value) {
return this.left.searchParent(value);
}else if (this.right != null && value >= this.value) {
return this.right.searchParent(value);
}else {
return null;
}
}
}
}
BinarySortTree实体类
package com.tree.binarysort;
/**
* @author lizhangyu
* @date 2021/3/15 20:07
*/
public class BinarySortTree {
private Node root;
public BinarySortTree() {
}
public Node getRoot() {
return root;
}
/**
* 添加节点
* @param node
*/
public void add(Node node) {
if (root == null) {
root = node;
}else {
root.add(node);
}
}
/**
* 中序遍历
*/
public void midOrder() {
if (root != null) {
root.midOrder();
}else {
System.out.println("树为空,不能遍历");
}
}
/**
* @param node 传入的结点(当做二叉排序树的根结点)
* @return 返回的 以 node 为根结点的二叉排序树的最小结点的值
*/
public int deleteRightTreeMin(Node node) {
Node target = node;
while (target.left != null) {
target = target.left;
}
deleteNode(target.value);
return target.value;
}
/**
* 查找当前节点
* @param value
* @return
*/
public Node search(int value) {
if (root != null) {
return root.search(value);
}else {
return null;
}
}
/**
* 查找当前节点的父节点
* @param value
* @return
*/
public Node searchParent(int value) {
if (root != null) {
return root.searchParent(value);
}else {
return null;
}
}
/**
* @param value
*/
public void deleteNode(int value) {
if (root == null) {
return;
}else {
Node targetNode = search(value);
//如果当前节点没有找到
if (targetNode == null) {
return;
}
//如果我们发现二叉树只有一个节点
if (root.left == null && root.right == null) {
root = null;
return;
}
//找打当前节点的父节点
Node parent = searchParent(value);
//如果要删除的节点为叶子节点
if (targetNode.left == null && targetNode.right == null) {
if (parent.left != null && parent.left.value == value) {
parent.left = null;
}else if (parent.right != null && parent.right.value == value) {
parent.right = null;
}
}else if (targetNode.left != null && targetNode.right != null) { //如果要删除的节点存在左右节点
int minVal = deleteRightTreeMin(targetNode.right);
targetNode.value = minVal;
}else {
//如果目标节点存在左节点
if (targetNode.left != null) {
if (parent != null) {
if (parent.left.value == value) {
parent.left = targetNode.left;
}else if (parent.right.value == value) {
parent.right = targetNode.left;
}else {
root = targetNode.left;
}
}
}else { //如果目标节点存在右节点
if (parent != null) {
if (parent.left.value == value) {
parent.left = targetNode.right;
}else if (parent.right.value == value) {
parent.right = targetNode.right;
}else {
root = targetNode.right;
}
}
}
}
}
}
}
BinarySortTreeDemo实体类:
package com.tree.binarysort;
/**
* @author lizhangyu
* @date 2021/3/15 20:15
*/
public class BinarySortTreeDemo {
public static void main(String[] args) {
int[] arr = {7, 9, 8, 6, 1, 2, 4};
BinarySortTree binarySortTree = new BinarySortTree();
for (int i = 0; i < arr.length; i++) {
Node node = new Node(arr[i]);
binarySortTree.add(node);
}
System.out.println("输出的中序遍历为:");
binarySortTree.midOrder();
System.out.println("输出根节点" + binarySortTree.getRoot());
binarySortTree.deleteNode(7);
System.out.println("删除之后的输出结果");
binarySortTree.midOrder();
System.out.println("输出根节点" + binarySortTree.getRoot());
}
}
输出的结果为:
输出的中序遍历为:
Node{value=1}
Node{value=2}
Node{value=4}
Node{value=6}
Node{value=7}
Node{value=8}
Node{value=9}
输出根节点Node{value=7}
删除之后的输出结果
Node{value=1}
Node{value=2}
Node{value=4}
Node{value=6}
Node{value=8}
Node{value=9}
输出根节点Node{value=8}