当前位置:网站首页>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)
边栏推荐
- Are the top ten leading securities companies safe to open accounts
- RMAN backup database_ catalogue
- 什么是算子?
- How to delay the delay function
- [in depth understanding of tcapulusdb technology] business guide for creating doc acceptance
- anaconda下载清华源
- Is it convenient to open a stock account? Is online account opening safe?
- 【深入理解TcaplusDB技术】如何实现Tmonitor单机安装
- 【深入理解TcaplusDB技术】单据受理之表管理
- 視覺SLAM十四講 第9講 卡爾曼濾波
猜你喜欢
![[in depth understanding of tcapulusdb technology] tcapulusdb operation and maintenance doc](/img/7b/8c4f1549054ee8c0184495d9e8e378.png)
[in depth understanding of tcapulusdb technology] tcapulusdb operation and maintenance doc

【深入理解TcaplusDB技术】TcaplusDB运维单据

什么是算子?

Centos7 installing redis 7.0.2
![[deeply understand tcapulusdb technology] tcapulusdb import data](/img/31/4e33fafa090e0bb5b55e11978cdff8.png)
[deeply understand tcapulusdb technology] tcapulusdb import data

視覺SLAM十四講 第9講 卡爾曼濾波

快手616战报首发,次抛精华引新浪潮,快品牌跃入热榜top3

Class 02 loader subsystem

Dell r530 built in hot spare status change description

JVM problem replication
随机推荐
.NET Worker Service 添加 Serilog 日志记录
04 program counter (PC register)
OSError: Unable to open file (truncated file: eof = 254803968, sblock->base_addr = 0, stored_eof = 2
QT using SQLite database
RMAN backup database_ catalogue
[in depth understanding of tcapulusdb technology] how to realize single machine installation of tmonitor
Install spark + run Scala related projects with commands + crontab scheduled execution
【深入理解TcaplusDB技术】TcaplusDB机型
揭秘GES超大规模图计算引擎HyG:图切分
Article 7: there is no code prompt in clion,,,
Network security algorithm
new TypeReference用法 fastjson[通俗易懂]
QT中QString包含“\u0000“的处理方式
How to develop the hash quiz game system? Hash quiz game system development application details case and source code
Anaconda download Tsinghua source
跨境电商亚马逊、eBay、Shopee、Lazada、速卖通、沃尔玛、阿里国际等平台,怎样进行自养号测评更安全?
Slam visuel Leçon 14 leçon 9 filtre Kalman
What is the ranking of the top ten securities companies? Is it safe to open a mobile account?
C generic class case
【深入理解TcaplusDB技术】单据受理之建表审批