核心逻辑
二叉查找树(Binary Search Tree),(又:二叉搜索树,二叉排序树)它或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值; 若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值; 它的左、右子树也分别为二叉排序树。二叉搜索树作为一种经典的数据结构,它既有链表的快速插入与删除操作的特点,又有数组快速查找的优势;所以应用十分广泛,例如在文件系统和数据库系统一般会采用这种数据结构进行高效率的排序与检索操作.
适用范围
- 插入一个数值
- 查询是否包含某个数值
- 删除某个数值
算法复杂度
和树深度强相关 O(log n)
package com.zby.calc;
import com.zby.calc.A_BinaryHeap.Student;
import com.zby.calc.B_LRUCache.Node;
/**
*
* 二叉搜索树
*
* @author zby
* @time 2021/6/10 16:24
* ****************************************
*/
public class C_BST {
// 根节点
private TreeNode<Integer, Student> root;
/**
*
* @desc 删除
******************************************
*/
public void remove(int iq) {
if(root==null)
return;
TreeNode<Integer, Student> choseNode = root;
TreeNode<Integer, Student> choseNodeParent = null;
while (choseNode!=null) {
if(choseNode.getKey()==iq)
break;
else if(choseNode.getKey()>iq) {
if(choseNode.getLeft()==null)
return;
choseNodeParent = choseNode;
choseNode = choseNode.getLeft();
} else {
if(choseNode.getRight()==null)
return;
choseNodeParent = choseNode;
choseNode = choseNode.getRight();
}
}
if(choseNode.getKey()!=iq||choseNodeParent==null)
return;
TreeNode<Integer, Student> replaceNode = getReplaceNode(choseNode);
if(replaceNode==null) {
if(choseNodeParent.getRight()==choseNode)
choseNodeParent.setRight(null);
if(choseNodeParent.getLeft()==choseNode)
choseNodeParent.setLeft(null);
return;
} else {
if(choseNodeParent.getRight()==choseNode)
choseNodeParent.setRight(replaceNode);
if(choseNodeParent.getLeft()==choseNode)
choseNodeParent.setLeft(replaceNode);
}
}
/**
*
* @desc 获取替换节点
* 1.叶子节点 不用找了 直接替换
* 2.左右节点只有一个 直接替换
* 3.左右节点都有 找左子节点最大值
******************************************
*/
private TreeNode<Integer, Student> getReplaceNode(TreeNode<Integer,Student> node) {
if(node==null)
return null;
if(node.getLeft()==null&&node.getRight()==null)
return null;
if(node.getLeft()==null)
return node.getRight();
if(node.getRight()==null)
return node.getLeft();
node = node.getLeft();
while(node!=null) {
if(node.getRight()==null)
return node;
node = node.getRight();
}
return null;
}
/**
*
* @desc 插入数据
******************************************
*/
public void add(Student v) {
if (root==null) {
root = new TreeNode<>(v.getIq(),v);
return;
}
TreeNode<Integer, Student> choseNode = root;
while (choseNode!=null) {
if(choseNode.getValue().getIq()==v.getIq())
return;
else if(choseNode.getValue().getIq()>v.getIq()) {
if(choseNode.getRight()==null) {
choseNode.setRight(new TreeNode<>(v.getIq(),v));
return;
}
choseNode = choseNode.getRight();
} else {
if(choseNode.getLeft()==null) {
choseNode.setLeft(new TreeNode<>(v.getIq(),v));
return;
}
choseNode = choseNode.getLeft();
}
}
}
/**
*
* @desc 获取节点
******************************************
*/
public Student get(int iq) {
TreeNode<Integer,Student> choseNode = root;
while (choseNode!=null) {
if(choseNode.getKey()==iq)
return choseNode.getValue();
else if(iq>choseNode.getKey()) {
if(choseNode.right==null)
return null;
choseNode = choseNode.right;
continue;
} else {
if(choseNode.left==null)
return null;
choseNode = choseNode.left;
continue;
}
}
return null;
}
public static void main(String[] args) {
}
/**
*
* 树节点
*
* @author zby
* @time 2021/6/10 16:26
* ****************************************
*/
class TreeNode<K,V> {
private TreeNode<K,V> left;
private TreeNode<K,V> right;
private K key;
private V value;
public void setLeft(TreeNode<K, V> left) {
this.left = left;
}
public void setRight(TreeNode<K, V> right) {
this.right = right;
}
public TreeNode<K, V> getLeft() {
return left;
}
public TreeNode<K, V> getRight() {
return right;
}
public K getKey() {
return key;
}
public V getValue() {
return value;
}
public TreeNode(K k, V v) {
this.key = k;
this.value = v;
}
}
}