当前位置:网站首页>Pre knowledge of hash table -- binary search tree
Pre knowledge of hash table -- binary search tree
2022-06-26 11:17:00 【Master_ hl】
Binary search tree
Concept
- If its left subtree is not empty , Then the values of all nodes on the left subtree are smaller than the values of the root nodes
- If its right subtree is not empty , Then the value of all nodes on the right subtree is greater than the value of the root node
- Its left and right subtrees are also binary search trees

Code implementation
Node static inner class
public class BinarySearchTree {
static class TreeNode {
public int key;
public TreeNode left;
public TreeNode right;
public TreeNode(int key) {
this.key = key;
}
}
public TreeNode root;
}Insert -- insert
public boolean insert(int key) {
if(this.root == null) {
this.root = new TreeNode(key);
return true;
}
TreeNode cur = this.root;
TreeNode parent = null;
// Find the inserted father
while(cur != null) {
if(cur.key < key) {
parent = cur;
cur = cur.right;// Go to the right
} else if(cur.key > key) {
parent = cur;
cur = cur.left;// Go to the left
} else {
// Cannot insert the same element
return false;
}
}
TreeNode node = new TreeNode(key);
if(key > parent.key) {
parent.right = node;
} else {
parent.left = node;
}
return true;
}The illustration

lookup -- search
public TreeNode search(int key) {
TreeNode cur = root;
while(cur != null) {
if(cur.key > key) {
cur = cur.left;
} else if(cur.key < key) {
cur = cur.right;
} else {
return cur;
}
}
return null;
}The lookup function is relatively simple , It's a binary search .
Delete -- remove ( difficulty )
public boolean remove(int key) {
TreeNode cur = root;
TreeNode parent = null;
while(cur != null) {
if(cur.key > key) {
parent = cur;
cur = cur.left;
} else if(cur.key < key) {
parent = cur;
cur = cur.right;
} else {
removeNode(parent, cur);
return true;
}
}
return false;
}
private void removeNode(TreeNode parent, TreeNode cur) {
//1. Left is empty 2. Right is empty. 3. It's not empty.
if(cur.left == null) {
if(cur == root) {
root = cur.right;
} else if(cur == parent.right) {
parent.right = cur.right;
} else {
parent.left = cur.right;
}
} else if(cur.right == null) {
if(cur == root) {
root = cur.left;
}else if(cur == parent.left) {
parent.left = cur.left;
} else {
parent.right = cur.left;
}
} else {
TreeNode targetParent = cur;
TreeNode target = cur.right;
// You can find a maximum value in the left subtree of the node to be deleted to overwrite it , You can also find a minimum value in the right subtree of the node to be deleted to overwrite it , Finally, remove the scapegoat
while(target.left != null) {
targetParent = target;
target = target.left;
}
cur.key = target.key;
// The scapegoat for judgment is targetParent The left child is still the right child
if(target == targetParent.left) {
targetParent.left = target.right;
} else {
targetParent.right = target.right;
}
}
}Ideas
Discussion by situation : Set the node to be deleted as cur , The node to be deleted is parent .
1.cur.left == null
- cur == root
- cur == parent.left
- ccur == parent.right
2.cur.right == null
- cur == root
- cur == parent.left;
- cur == parent.right
3.cur.left != null && cur.right != null
- Substitution method ( difficulty )
Draw pictures to explain the substitution method !
Case two is similar to case one

Substitution method

Substitution analysis
When the band deletes the element key When there are children around , Put... Directly key It is unrealistic to delete , Because you don't know key What is the following structure , Even if you know , There is no way to adjust it , So we need to use the substitution method to realize , Either way key Replace with the maximum value in the left subtree of , You can also choose key Replace with the lowest value in the right subtree of , Because only in this way can we ensure that the structure of the binary search tree is not destroyed .
Check insertion / Is it still a binary search tree after deletion -- In the sequence traversal
In the following figure, the result of sequence traversal is :0,1,2,3,4,5,6,7,8,9

Code implementation
public List<Integer> inOrder(TreeNode root) {
List<Integer> list = new ArrayList<>();
if(root == null) {
return list;
}
List<Integer> left = inOrder(root.left);
list.addAll(left);// Left
list.add(root.key);// in
List<Integer> right = inOrder(root.right);
list.addAll(right);// Right
return list;
}We are doing a binary search tree CURD In operation , You can determine whether it is correct by calling the debugging and printing of the sequence traversal !
That's all for this blog , See you next time !!!
边栏推荐
- laravel 安装报错 Uncaught ReflectionException: Class view does not exist
- 介紹一下實現建模中可能用到的時間序列預測之線性二次移動平均,Excel的簡單操作
- flannel的host-gw与calico
- 【北邮果园微处理器设计】10 Serial Communication 串口通信笔记
- 19:第三章:开发通行证服务:2:在程序中,打通阿里云短信服务;(仅仅是打通阿里云短信服务器,不涉及具体的业务开发)
- Code specification & explain in detail the functions and uses of husky, prettier, eslint and lint staged
- ACK攻击是什么意思?ACK攻击怎么防御?
- sliding window
- 3、 Linked list exercise
- Recent work report
猜你喜欢

sliding window

滑动窗口

Grain Mall - distributed Foundation

Detailed explanation of MySQL fuzzy query

哈希表的前置知识---二叉搜索树
![Compréhension approfondie de l'expérience de port série stm32 (registre) [Tutoriel de niveau nounou]](/img/b2/f09e220918a85b14a1993aa85f7720.png)
Compréhension approfondie de l'expérience de port série stm32 (registre) [Tutoriel de niveau nounou]

FasterRCNN

c语言 --- 运算符和表达式

C language -- operators and expressions

介紹一下實現建模中可能用到的時間序列預測之線性二次移動平均,Excel的簡單操作
随机推荐
2、 Linear table
APICloud 实现文档下载和预览功能
Laravel admin hidden button, and set button display, default sequence, form form form non modifiable value
JS take the date of the previous month 【 pit filling 】
Docker中实现MySQL主从复制
Group by is used in laravel to group and query the quantity
MySQL Performance Monitoring and SQL statements
Nacos2.x.x start error creating bean with name 'grpcclusterserver';
最强swarm集群一键部署+氢弹级容器管理工具介绍
MySQL 30 military regulations
FastRCNN
再获认可!知道创宇入选“业务安全推进计划”首批成员单位
express在nodejs中的基本使用
What does ack attack mean? How to defend against ack attacks?
Dynamic programming to solve stock problems (Part 2)
Recent work report
Laravel-admin 登录添加图形验证码
word中涂黑的方块
c语言 --- 运算符和表达式
一键部署ceph脚本