当前位置:网站首页>Search Binary Tree - find nodes, insert nodes, delete nodes
Search Binary Tree - find nodes, insert nodes, delete nodes
2022-07-23 17:08:00 【Another day of idle fish】
Trees
public class BinarySearchTree {
static class TreeNode {
public int val;
public TreeNode left;
public TreeNode right;
public TreeNode(int val) {
this.val = val;
}
}
public TreeNode root;
}One : Looking for nodes
1. requirement : public TreeNode search(int key) lookup key Whether in
2. Ideas : Search binary trees —— The values on the left side of the node are all less than The value of the root , The values on the right side of the node are greater than Value of root node You can use this point to judge - The value of the root node is less than key- Look to the right of the root node ( The value of the root node is greater than key- Look to the left of the root node )
3. Code :
public TreeNode search(int key){
TreeNode cur = root;
while(cur != null){
if (cur.val > key){
cur = cur.left;
}else if(cur.val < key){
cur = cur.right;
}else {
return cur;
}
}
return null;
}Two : Insert node
1. requirement :public boolean insert(int key) Insert a data into it key
2. Ideas :
Look at the picture below : If you are given a tree to insert nodes , Think about it , You have to plug in the nodes in the original tree ?—— Leaf position —— So you have to find the original tree node .left/.right==null The point of
First of all - This tree is Empty tree - The empty tree directly makes this data the root second - Find a location to use cur=root,p=null, When cur!=null- Judge its and key Of size ( Big or small p=cur,cur=cur.left / right)( equal Then return to false Can't have the same data )- until cur==null-key And p.val By comparison, it is inserted p Left or right

3. Code :
public boolean insert(int key){
TreeNode node = new TreeNode(key);
if(root == null){
root = node;
return true;
}
TreeNode cur = root;
TreeNode p = null;
while(cur != null){
if (cur.val > key){
p = cur;
cur = cur.left;
}else if(cur.val < key){
p = cur;
cur = cur.right;
}else {
return false;
}
}
if(p.val > key){
p.left = node;
}else {
p.right = node;
}
return true;
}4. Look at the picture and walk again :
3、 ... and : Delete node
1. requirement : public void remove(int key) The deletion value is key The node of
2. Ideas :
Whether the node can be found cur—— The left side of the deleted node is empty 、 The right side of the deleted node is empty 、 The left and right sides of the deleted node are not empty
Deleted nodes cur Left is empty ——cur==root -> root = cur.right 、cur==p.left ->p.left=cur.right 、cur==p.right
Deleted nodes cur The right side is empty —— It's similar to the one above
The left and right sides of the deleted node are not empty —— It's time for you to choose Find the minimum value on the right ,( Find the maximum value on the left )—— Found his value cur Give the node you want to delete —— Judge cur yes p.left / p.right—— And then =cur.right
3. Code :
public void remove(int key){
TreeNode cur = root;
TreeNode p = null;
while(cur != null){
if (cur.val > key){
p = cur;
cur = cur.left;
}else if(cur.val < key){
p = cur;
cur = cur.right;
}else {
removeNode(cur,p);
return;
}
}
}
private void removeNode(TreeNode cur,TreeNode p){
if(cur.left == null){
if(cur == root){
root = cur.right;
}else if(cur == p.left){
p.left = cur.right;
}else {
p.right = cur.right;
}
}
else if(cur.right == null){
if(cur == root){
root = cur.left;
}else if(cur == p.right){
p.right = cur.left;
}else {
p.left = cur.left;
}
}
else {
TreeNode target = cur.right;
TreeNode targetParent = cur;
while(target.left != null){
targetParent = target;
target = target.left;
}
cur.val = target.val;
if(targetParent.left == target){
targetParent.left = target.right;
}else {
targetParent.right = target.right;
}
}
}
public void inorderNode(TreeNode root){
if(root == null) return;
inorderNode(root.left);
System.out.print(root.val+" ");
inorderNode(root.right);
}4. Look at the picture and walk again


边栏推荐
- 排序-介绍,代码思路,使用建议,代码实现-1
- CSR、SSR 与 SSG
- Advanced authentication of uni app [Day12]
- Shell | ssh失败原因及解决方法不完全总结
- pwn入门(3)堆
- Wechat applet class binding, how to bind two variables
- 怎么正确设置路由器
- Compose canvas pie chart effect drawing
- Ros2 self study notes: rqt visualization tool
- Fundamentals of C language -- 2-5 points of knowledge about pointers and functions
猜你喜欢
随机推荐
软件配置 | Anaconda下载、安装及环境配置和卸载
Deep learning convolutional neural network paper study alexnet
php文件锁抽奖防止并发
Notes on Microcomputer Principle and technical interface
JS之闭包
Ie box model and standard box model
Object.defineproperty method, data agent
Detector: detect objects with recursive feature pyramid and switchable atolos convolution
Eureka notes
软件测试计划包括哪些内容,测试计划如何编写。分享测试计划模板
本周投融报:Web3游戏熊市吸金
keil错误和解决办法(1):FCARM - Output Name not specified, please check ‘Options for Target - Utilities‘
分类模型——逻辑回归、Fisher线性判别(SPSS)
Browser homology policy
灰色预测(MATLAB)
灰色关联分析(MATLAB)
死磕递归1:递推公式
智慧物联网源码 带组态物联网源码 工业物联网源码:支持传感器解析服务,数据实时采集和远程控制
Compose Canvas饼图效果绘制
JMeter之函数二次开发/插件开发(细版)






![[30. N-queen problem]](/img/ed/7e2832695613c16da034f05bd4040b.png)


