二叉搜索树:
- 是一个二叉树
- 左子树所有节点值<根结点值<右子树的所有节点值
package ds.bin_tree;
import java.util.NoSuchElementException;
/**
* 二叉搜索树(左结点值<根节点值<右节点值),中序遍历为一个升序的数组
* @Author liusifan
* @Data 2022/3/11 21:46
*/
public class BST {
private int size;
private Node root;
private class Node {
private int val;
private Node left;
private Node right;
public Node(int val) {
this.val = val;
}
}
/**
* 插入结点
*
* @param value
*/
public void add(int value) {
root = add(root, value);
}
private Node add(Node root, int value) {
if (root == null) {
Node newNode = new Node(value);
size++;
return newNode;
}
if (root.val > value) {
root.left = add(root.left, value);
}
if (root.val < value) {
root.right = add(root.right, value);
}
return root;
}
/**
* 判断是否存在val
*
* @param val
* @return
*/
public boolean contain(int val) {
return contain(root, val);
}
private boolean contain(Node root, int val) {
if (root == null) {
return false;
}
if (root.val == val) {
return true;
} else {
if (root.val > val) {
return contain(root.left, val);
} else {
return contain(root.right, val);
}
}
}
/**
* 查找最小值
* @return
*/
public int findMin() {
if (size == 0) {
throw new NoSuchElementException("BST is empty");
}
Node minNode=minNode(root);
return minNode.val;
}
private Node minNode(Node root) {
if (root.left==null)
{
return root;
}
return minNode(root.left);
}
/**
* 查找最大值
* @return
*/
public int findMax()
{
if (size==0)
{
throw new NoSuchElementException("BST is empty");
}
Node maxNode=maxNode(root);
return maxNode.val;
}
private Node maxNode(Node root) {
if (root.right==null)
{
return root;
}
return maxNode(root.right);
}
/**
* 删除最大值的结点
* @return
*/
public int removeMax()
{
int max=findMax();
root=removeMax(root);
return max;
}
private Node removeMax(Node root)
{
if (root.right==null)
{
Node left=root.left;
root.left=root=null;
size--;
return left;
}
root.right=removeMax(root.right);
return root;
}
/**
* 删除最小值的结点
* @return
*/
public int removeMin()
{
int min=findMin();
root=removeMin(root);
return min;
}
private Node removeMin(Node root) {
if (root.left==null)
{
Node right=root.right;
root.right=root=null;
size--;
return right;
}
root.left=removeMin(root.left);
return root;
}
/**
* 以root为根结点删除val值
* @param val
*/
public void remove(int val)
{
root=remove(root,val);
}
public Node remove(Node root,int val)
{
if (root == null)
{
throw new NoSuchElementException("BST 没有值为"+val+"结点");
}
else if (root.val<val)
{
root.right=remove(root.right,val);
return root;
}
else if (root.val>val)
{
root.left=remove(root.left,val);
return root;
}
else
{
//只有右树
if (root.left==null)
{
Node right=root.right;
root.right=root=null;
size--;
return right;
}
//只有左树
if (root.right==null)
{
Node left=root.left;
root.left=root=null;
size--;
return left;
}
//左右树都有
Node successor = minNode(root.right);
successor.right = removeMin(root.right);
successor.left = root.left;
root.left=root.right=root = null;
return successor;
}
}
@Override
public String toString() {
StringBuilder stringBuilder = new StringBuilder();
generateBSTString(root);
return stringBuilder.toString();
}
private void generateBSTString(Node root) {
if (root == null)
{
return;
}
generateBSTString(root.left);
System.out.print(" "+root.val);
generateBSTString(root.right);
}
}