当前位置:网站首页>Binary sort tree: BST
Binary sort tree: BST
2022-06-28 04:54:00 【Cc824 constant】
1. Introduce :

2. Creation and traversal of binary sort tree 【 Traversal adopts medium order traversal 】:
Add node 【 stay Node Class 】:
Then in the binary sort tree class
class BinarySortTree Encapsulation method public void add(Node node) {
if(node == null) {
return;
}
// Determine the value of the incoming node , The value relationship with the root node of the current subtree
if(node.value < this.value) {
// If the left child of the current node is null
if(this.left == null) {
this.left = node;
} else {
// Recursively add... To the left subtree
this.left.add(node);
}
} else { // The value of the added node is greater than The value of the current node
if(this.right == null) {
this.right = node;
} else {
// Recursively add... To the right subtree
this.right.add(node);
}
}
}3. Delete node 【 The following two methods are all encapsulated in a binary sort tree 】:
You need to write two methods first : 1. Find the target node 2. Find the parent node of the target node
1. Find the target node :
public Node search(int value) {
if(value == this.value) // Find the node
return this;
else if(value < this.value) {// If the search value is less than the current node , Search left subtree recursively
// If the left child node is empty
if(this.left == null)
return null;
else
return this.left.search(value);
} else { // If the search value is not less than the current node , Search right subtree recursively
if(this.right == null)
return null;
else
return this.right.search(value);
}
}2. Find the parent node of the target node :
public Node searchParent(int value) {
// If the current node is the parent of the node to be deleted , Just go back to
if((this.left != null && this.left.value == value) ||
(this.right != null && this.right.value == value))
return this;
else {
// If the search value is less than the value of the current node , And the left child of the current node is not empty
if(value < this.value && this.left != null)
return this.left.searchParent(value); // Search left subtree recursively
else if (value >= this.value && this.right != null)
return this.right.searchParent(value); // Search right subtree recursively
else
return null; // No parent found
}
}There are three situations :
(1) Delete leaf node 【BinarySortTree Class 】:

(2) Delete a node with only one subtree


(3) Delete a node with two subtrees

public void delNode(int value) {
if(root == null)
return;
else {
// Find the target node
Node target = root.search(value);
if(target == null) {
System.out.println(" Unable to find the target node ");
return;
}
// There is only one node
else if (root.right == null && root.left == null) {
root = null;
return;
}
// Find parent node
Node parent = searchParent(value);
//1. Delete the leaf node
if (target.left == null && target.right == null) {
if (parent.left != null && parent.left.value == target
.value)
parent.left = null;
else if (parent.right != null && parent.right.value ==
target.value)
parent.right = null;
}
//2. Delete a node with only one subtree
//2.1 If target There's a Zuozi tree
if(target.left!=null&&target.right == null){
//target Is the left subtree of the parent node
if (parent != null) { // !!! Consider whether the parent node is empty
if (parent.left != null && parent.left.value == value)
parent.left = target.left;
//target Is the right node of the parent node
if (parent.right != null && parent.right.value == value)
parent.right = target.left;
}
else root = target.left;
}
//2.2 If target There are right subtrees
else if(target.right!=null&&target.left == null){
//target Is the left node of the parent node
if (parent != null) {
if (parent.left != null && parent.left.value == value)
parent.left = target.right;
//target Is the right node of the parent node
if (parent.right != null && parent.right.value == value)
parent.right = target.right;
}
else root = target.right;
}
// Delete two subtrees
// find target The minimum node of the right subtree of
else if (target.left != null && target.right != null) {
int min = delRightTreeMin(target.right); // Find the smallest left child node of the right subtree and delete , Then return the number
target.value = min; // Directly assigned to the target node
}
}
}// Find the minimum left child of the right subtree of the target node
public int delRightTreeMin(Node node) {
Node target = node; //target Temporary variable
// Loop to find the left child node , You'll find the minimum
while(target.left != null) {
target = target.left;
}
// At this time target It points to the smallest node
// Delete the minimum node
delNode(target.value);
return target.value;
}边栏推荐
- [CSP-J2020] 优秀的拆分
- Analysis of distributed transaction TCC
- The number of small stores in Suning has dropped sharply by 428 in one year. Zhangkangyang, the son of Zhang Jindong, is the actual controller
- Short video platform development, click links and pictures to automatically jump to a new page
- 2022-06-27:给出一个长度为n的01串,现在请你找到两个区间, 使得这两个区间中,1的个数相等,0的个数也相等, 这两个区间可以相交,但是不可以完全重叠,即两个区间的左右端点不可以完全一样。
- Find an SQL that can judge the data in the table and only fill in the SQL that is not overwritten
- The development of the Internet has promoted the emergence of a series of new models such as unbounded retail, digital retail and instant retail
- If mysqlcdc sets multiple parallelism, will the incremental data repeat?
- Project practice! Teach you JMeter performance test hand in hand
- Precautions for using C language global variables (global variables in C and H files, static global variables)
猜你喜欢

MySQL gets the current date of the year
![[Matlab bp regression prediction] GA Optimized BP regression prediction (including comparison before optimization) [including source code 1901]](/img/73/1e4c605991189acc674d85618cf0ef.png)
[Matlab bp regression prediction] GA Optimized BP regression prediction (including comparison before optimization) [including source code 1901]

What to do when MySQL changes the password and reports an error

Distributed transaction - Final consistency scheme based on message compensation (local message table, message queue)

CUPTI error: CUPTI could not be loaded or symbol could not be found.

Severe tire damage: the first rock band in the world to broadcast live on the Internet
![[early knowledge of activities] list of recent activities of livevideostack](/img/aa/8b14f9863cd675a7be06a0c3855a93.png)
[early knowledge of activities] list of recent activities of livevideostack

A doctor's 22 years in Huawei (full of dry goods)

Generate QR code in wechat applet

With favorable policies, more than 20 provinces and cities have launched the yuanuniverse development plan
随机推荐
!‘cat‘ 不是内部或外部命令,也不是可运行的程序 或批处理文件。
Detailed reading of the thesis: implementing volume models for handowriting text recognition
玩转双指针
10: 00 interview, came out at 10:02, the question is really too
Password encryption MD5 and salt treatment
Light collector, Yunnan Baiyao!
浅析搭建视频监控汇聚平台的必要性及场景应用
The coming wave of Web3
Short video platform development, click links and pictures to automatically jump to a new page
Congratulations to myself, official account has more than ten thousand fans
lotus v1.16.0 calibnet
June 27, 2022: give a 01 string with a length of N. now please find two intervals so that the number of 1 and the number of 0 in the two intervals are equal. The two intervals can intersect, but not c
Source code of live video system, countdown display, countdown of commodity spike
短视频本地生活版块成为热门,如何把握新的风口机遇?
flinkcdc采集oracle,oracle数据库是CDB的
[early knowledge of activities] list of recent activities of livevideostack
With favorable policies, more than 20 provinces and cities have launched the yuanuniverse development plan
判断对象中是否存在某一个属性
知识点滴 - 关于汉语学习的网络资源
2022年安全员-A证考试题库及模拟考试