当前位置:网站首页>Basic operation details of binary search tree (BST) (complete code, including test cases)
Basic operation details of binary search tree (BST) (complete code, including test cases)
2022-06-25 18:26:00 【Zebra and running】
Catalog
1. Concept and its characteristics
1. Concept and its characteristics
Binary search tree :
1. It's a binary tree
2. Save keywords in each node (Key)
3. Keywords need to be able to compare
4. Each node complies with : All of the left subtree Key < Its key < All of the right subtree Key
5. There will be no equal in a binary tree Key
6. The middle order traversal of a binary search tree must be ordered
2. Basic operation
We need a node class and a binary search tree class
Node class Node:
public class Node {
public Integer key;
public Node left;
public Node right;
public Node(Integer key) {
this.key = key;
}
@Override
public String toString() {
return "Node{" +
"key=" + key +
'}';
}
}Binary search tree class :
public class BST {
private Node root;
}(1). lookup
1. Method body analysis :

2. Ideas :
When searching , If it is smaller than the current node, go to the left , If it is larger than the current node, go to the right , Exit if equal , find null And quit , The specific code is as follows :
public boolean search(Integer key){
Node current = this.root;
while (current != null){
int cmp = key.compareTo(current.key);
if (cmp == 0){
return true;
}else if (cmp < 0){
current = current.left;
}else {
current = current.right;
}
}
return false;
}(2). Insert
If an object is needed in a method , Then this method must be non static .
1. Method body analysis :

2. Code thinking :
Similar to finding , Set a parent node , Maintaining it has always been cur The parent node of , then cur Look down , If we find equal nodes , Throw an exception ; Empty found , You can insert ( According to cmp Decide whether to insert it to the left or the right ), The specific code is as follows :
public void insert(Integer key) {
if (root == null) {
root = new Node(key);
return;
}
// Always keep parent Namely current The parent node of
Node parent = null;
Node current = root;
int cmp = 0;
while (current != null) {
cmp = key.compareTo(current.key);
if (cmp == 0) {
throw new RuntimeException("BST The same... Is not allowed in key");
} else if (cmp < 0) {
parent = current;
current = current.left;
} else {
parent = current;
current = current.right;
}
}
// current A certain null.
// parent Is the parent node to insert the new node
Node node = new Node(key);
if (cmp < 0) {
parent.left = node;
} else {
parent.right = node;
}
}(3). Delete
1. analysis

2. Code thinking
1). Find contains key The node of , Write it down as node.node The parent node of is marked as parent
2). Judge
Situation 1 :node.left == null && node.right == null
Situation two :node.left != null && node.right == null
Situation three : Similar to case 2
Situation four : Substitution method ( Select the largest node in the left subtree ( Node right == null, Write it down as ghost))
node Is the node to be deleted ,ghost Is the largest node in the left subtree

The specific code is as follows :
public boolean remove(Integer key) {
// 1. Find the to delete key The node , Write it down as node.node The parent node of , Write it down as parent
Node parent = null;
Node current = root;
while (current != null) {
int cmp = key.compareTo(current.key);
if (cmp == 0) {
removeInternal(current, parent);
return true;
} else if (cmp < 0) {
parent = current;
current = current.left;
} else {
parent = current;
current = current.right;
}
}
return false;
}
private void removeInternal(Node node, Node parent) {
if (node.left == null && node.right == null) {
if (node == root) {
root = null;
} else if (node == parent.left) {
parent.left = null;
} else {
parent.right = null;
}
} else if (node.left != null && node.right == null) {
if (node == root) {
root = node.left;
} else if (node == parent.left) {
parent.left = node.left;
} else {
parent.right = node.left;
}
} else if (node.left == null && node.right != null) {
if (node == root) {
root = node.right;
} else if (node == parent.left) {
parent.left = node.right;
} else {
parent.right = node.right;
}
} else {
// Use the substitution method to delete , Use node The node where the maximum value is located in the left subtree of , Write it down as ghost.ghost My parents wrote ghostParent
Node ghostParent = node;
Node ghost = node.left;
// Look all the way to the right , until ghost.right == null
while (ghost.right != null) {
ghostParent = ghost;
ghost = ghost.right;
}
// there ghost There must be no right subtree
// 1. Replace
node.key = ghost.key;
// 2. Delete ghost node ( The right child must be null)
if (node == ghostParent) {
// The deleted point is the parent of the largest point of the left subtree , This is the only case ,ghost It is possible to ghostParent The left subtree
// This situation is node My left child has no right child
ghostParent.left = ghost.left;
} else {
// otherwise ,ghost It must be ghostParent Right subtree
// This situation is node My right child has no left child
ghostParent.right = ghost.left;
}
}
}3. summary
Binary search tree
Important features : The middle order is ordered !
operation :
1. Insert / lookup / Delete
2. Time complexity : Best and average :O(log(n)) The worst :O(n)
边栏推荐
- Article 7: there is no code prompt in clion,,,
- [deeply understand tcapulusdb technology] one click installation of tmonitor background
- Move graph explorer to jupyterab: use ges4jupyter to connect ges and explore graphs
- RMAN备份数据库_双重备份备份集(Duplexing Backup Sets)
- .NET Worker Service 添加 Serilog 日志记录
- 【深入理解TcaplusDB技术】单据受理之创建游戏区
- [in depth understanding of tcapulusdb technology] tcapulusdb regular documents
- 【深入理解TcaplusDB技术】单据受理之创建业务指南
- 解析数仓lazyagg查询重写优化
- 【深入理解TcaplusDB技术】TcaplusDB新增机型
猜你喜欢

LeetCode力扣(剑指offer 26-30)26. 树的子结构 27. 二叉树的镜像 28. 对称的二叉树 29. 顺时针打印矩阵 30. 包含min函数的栈

Class 02 loader subsystem

【深入理解TcaplusDB技术】TcaplusDB运维
![[deeply understand tcapulusdb technology] transaction execution of document acceptance](/img/7b/8c4f1549054ee8c0184495d9e8e378.png)
[deeply understand tcapulusdb technology] transaction execution of document acceptance

Install spark + run Scala related projects with commands + crontab scheduled execution

【深入理解TcaplusDB技术】TcaplusDB机型

【深入理解TcaplusDB技术】TcaplusDB导入数据

Use pagoda to set up mqtt server

【深入理解TcaplusDB技术】Tmonitor后台一键安装

Optimization of lazyagg query rewriting in parsing data warehouse
随机推荐
Is it safe for a securities company to open an account with the lowest handling fee among the top ten
JVM problem replication
[deeply understand tcapulusdb technology] table management of document acceptance
158_模型_Power BI 使用 DAX + SVG 打通制作商业图表几乎所有可能
Which is better and safer, GF easy gold rush or compass
Fixed frequency and voltage regulation scheme of Qi v1.2.4 protocol
[in depth understanding of tcapulusdb technology] business guide for creating doc acceptance
Is it convenient to open a stock account? Is online account opening safe?
How to open a stock account is it safe to open an account
.NET Worker Service 如何优雅退出
1. Understanding of norm
[in depth understanding of tcapulusdb technology] tcapulusdb business data backup
存在重复元素III[利用排序后的有序性进行剪枝]
篇7:CLion中没有代码提示,,,
03 runtime data area overview and threads
【路径规划】如何给路径增加运动对象
What is the ranking of the top ten securities companies? Is it safe to open a mobile account?
踩坑记录---线程池拒绝策略引起的一系列巧合
QT中QString包含“\u0000“的处理方式
05 virtual machine stack