为什么要引入BST(Best Sort Tree)?
使用数组
- 数组未排序
优点:直接在数组尾添加,速度快。
缺点:查找速度慢. - 数组排序,
优点:可以使用二分查找,查找速度快,
缺点:为了保证数组有序,在添加新数据时,找到插入位置后,后面的数据需整体移动,速度慢。
使用链式存储-链表
- 不管链表是否有序,查找速度都慢,添加数据速度比数组快,不需要数据整体移动。
使用二叉排序树
- BST(Binary Search Tree)目的是为了提高查找的性能,其查找在平均和最坏的情况下都是logn级别,接近二分查找。
其特点是:对于二叉排序树的任何一个非叶子节点,要求左子节点的值比当前节点的值小,右子节点的值比当前节点的值大。
代码实现
Node结点
package com.edu.tree.binarysorttree;
/**
* @Date 2020/6/11 8:54
* @Author by LiShiYan
* @Description 二叉排序树结点
* 二叉排序树:BST: (Binary Sort(Search) Tree)
* 1.对于二叉排序树的任何一个 非叶子 节点,
* 2.要求 左子节点的值 比当前节点的值小
* 3.右子节点的值比当前节点的值大。
*/
public class Node {
private Integer data;
private Node left;
private Node right;
public Node(Integer data) {
this.data = data;
}
/**
* 查找要删除结点的父结点
* @param data 删除结点
* @return 删除结点的父结点, 如果没有返回null
*/
public Node searchParent(int data) {
//如果当前结点是删除结点的父节点,返回true
boolean isParent = (this.left != null && this.left.data == data) || (this.right != null && this.right.data == data);
if (isParent) {
//返回当前结点
return this;
} else {
//如果查找的值小于当前结点的值,并且当前结点的左子结点不为空
if (data < this.data && this.left != null) {
//向左子树递归
return this.left.searchParent(data);
} else if (data >= this.data && this.right != null) {
//向右子树递归
return this.right.searchParent(data);
} else {
//没有找到父结点
return null;
}
}
}
/**
* 查找要删除的结点
*
* @param data 要删除结点的值
* @return 如果找到返回该结点,否则返回null
*/
public Node search(int data) {
//找到该结点
if (data == this.data) {
return this;
}
//如果查找的值小于当前结点,向左子树递归查找
else if (data < this.data) {
//如果左子树为空,返回null
if (this.left == null) {
return null;
}
return this.left.search(data);
}
//如果查找的值大于等于当前结点,向右子树递归查找
else {
if (this.right == null) {
return null;
}
return this.right.search(data);
}
}
/**
* 递归的形式添加节点
*
* @param node 要添加的结点
*/
public void add(Node node) {
if (node == null) {
return;
}
//判断传入结点的值和当前子树的根节点的值的关系
//1.添加结点的值小于当前结点的值
if (node.data < this.data) {
if (this.left == null) {
//当前结点的left指针指向node结点
this.left = node;
} else {
//递归的向左子树添加
this.left.add(node);
}
} else {
//2.添加的结点值大于 当前结点的值
if (this.right == null) {
//当前结点的right指针指向node结点
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();
}
}
public Integer getData() {
return data;
}
public void setData(Integer data) {
this.data = data;
}
public Node getLeft() {
return left;
}
public void setLeft(Node left) {
this.left = left;
}
public Node getRight() {
return right;
}
public void setRight(Node right) {
this.right = right;
}
@Override
public String toString() {
return "Node{" +
"data=" + data +
'}';
}
}
BinarySortTree 结点
package com.edu.tree.binarysorttree;
/**
* @Date 2020/6/11 8:54
* @Author by LiShiYan
* @Description 二叉排序树
*/
public class BinarySortTree {
/**
* 根结点
*/
private Node root;
public Node getRoot() {
return root;
}
public void setRoot(Node root) {
this.root = root;
}
/**
* 1. 返回的 以node 为根结点的二叉排序树的最小结点的值
* 2. 删除node 为根结点的二叉排序树的最小结点
*
* @param node 传入的结点(当做二叉排序树的根节点)
* @return 以node为根节点的二叉排序树的最小结点的值
*/
public int delRightTreeMin(Node node) {
Node targetNode = node;
//循环的查找左子结点,就会找到最小值
while (targetNode.getLeft() != null) {
targetNode = targetNode.getLeft();
}
//循环结束,此时target指向了最小的结点
//删除最小结点
delNode(targetNode.getData());
return targetNode.getData();
}
/**
* @param data 要删除结点
*/
public void delNode(int data) {
if (root == null) {
return;
} else {
//1.找到要删除结点
Node targetNode = search(data);
//如果没有找到要删除的结点,返回
if (targetNode == null) {
return;
}
//如果当前这颗二叉排序树只有一个结点
if (root.getLeft() == null && root.getRight() == null) {
root = null;
return;
}
//2.找到targetNode父结点
Node parent = searchParent(data);
//3.1如果删除的结点是叶子结点
if (targetNode.getLeft() == null && targetNode.getRight() == null) {
//判断targetNode是父结点的左子结点还是右子结点
if (parent.getLeft() != null && parent.getLeft().getData() == data) {
//左子结点
parent.setLeft(null);
}
if (parent.getRight() != null && parent.getRight().getData() == data) {
//右子结点
parent.setRight(null);
}
}
//3.2 删除有两颗子树的节点
else if (targetNode.getLeft() != null && targetNode.getRight() != null) {
//1.找到最小结点
int minValue = delRightTreeMin(targetNode.getRight());
//2.设置删除结点的值为最小结点的值
targetNode.setData(minValue);
}
//3.3 删除只有一颗子树的结点
else {
//1.如果要删除的结点有 左子结点: targetNode.getLeft()
if (targetNode.getLeft() != null) {
//父结点不为空
if (parent != null) {
//1.2如果targetNode是 parent 左子结点
if (parent.getLeft().getData() == data) {
//将parent 左结点指针 指向targetNode的左子结点
parent.setLeft(targetNode.getLeft());
} else {
//1.3如果targetNode是 parent 右子结点
parent.setRight(targetNode.getLeft());
}
}else {
//父结点为空,让目标结点的左节点成为根结点
root=targetNode.getLeft();
}
} else {
//2. 如果要删除的结点有 右子结点: targetNode.getRight()
if (parent != null) {
//2.2如果targetNode是 parent 左子结点
if (parent.getLeft().getData() == data) {
parent.setLeft(targetNode.getRight());
} else {
//2.3如果targetNode是 parent 右子结点
parent.setRight(targetNode.getRight());
}
}else {
//父结点为空,让目标结点的右节点成为根结点
root=targetNode.getRight();
}
}
}
}
}
/**
* 查找要删除的结点
*
* @param data 要删除结点
* @return 以删除结点
*/
public Node search(int data) {
if (root == null) {
return null;
} else {
return root.search(data);
}
}
/**
* 查找父结点
*
* @param data 要删除结点
* @return 要删除结点的父结点
*/
public Node searchParent(int data) {
if (root == null) {
return null;
} else {
return root.searchParent(data);
}
}
/**
* 添加结点
*
* @param node 结点
*/
public void add(Node node) {
if (root == null) {
root = node;
} else {
root.add(node);
}
}
/**
* 中序遍历
*/
public void infixOrder() {
if (root != null) {
root.infixOrder();
} else {
System.out.println("二叉排序树为空,不能遍历");
}
}
}