当前位置:网站首页>高阶-二叉搜索树详解
高阶-二叉搜索树详解
2022-07-01 06:09:00 【qq_52025208】
1.二叉搜索树又称二叉排序树,它或者是一棵空树,或者是具有以下性质的二叉树:
(1)若它的左子树不为空,则左子树上所有节点的值都小于根节点的值
(2)若它的右子树不为空,则右子树上所有节点的值都大于根节点的值
(3)它的左右子树也分别为二叉搜索树
2.二叉搜索树的实现
class BSNode {
public int val;
public BSNode left;
public BSNode right;
public BSNode() {
}
public BSNode(int val) {
this.val = val;
}
public BSNode root = null;
public boolean insert(int val) {
BSNode node = new BSNode(val);
if(root == null) {
root = node;
return true;
}
BSNode cur = root;
BSNode par = null;
while (cur != null) {
if(cur.val == val) {
return false;
} else if (cur.val > val){
par = cur;
cur = cur.left;
} else {
par = cur;
cur = cur.right;
}
}
if(par.val > val) {
par.left = node;
} else {
par.right = node;
}
return true;
}
public boolean search(int val) {
if(root == null) return false;
BSNode cur = root;
while (cur != null) {
if(cur.val == val) {
return true;
} else if(cur.val > val) {
cur = cur.left;
} else {
cur = cur.right;
}
}
return false;
}
public void remove(int val) {
if(root == null) return;
BSNode cur = root;
BSNode pre = null;
while (cur != null) {
if(cur.val == val) {
//删除操作
removeNode(val,pre,cur);
} else if (cur.val > val) {
pre = cur;
cur = cur.left;
} else {
pre = cur;
cur = cur.right;
}
}
}
public void removeNode(int val, BSNode pre, BSNode cur) {
if(cur.left == null) {
if (root == cur) {
root = cur.right;
} else if(cur == pre.left){
pre.left = cur.right;
} else if (cur == pre.right) {
pre.right = cur.right;
}
} else if (cur.right == null) {
if(root == cur) {
root = cur.left;
} else if(cur == pre.left) {
pre.left = cur.left;
} else if (cur == pre.right) {
pre.right = cur.left;
}
} else {
BSNode targetParent = cur;
BSNode target = cur.right;
while (target.left != null) {
targetParent = target;
target = target.left;
}
cur.val = target.val;
if(target == targetParent.left) {
targetParent.left = target.right;
} else {
targetParent.right = target.right;
}
}
}
}
public class BST1 {
public static void preOrder(BSNode root) {
if(root != null) {
System.out.print(root.val+" ");
preOrder(root.left);
preOrder(root.right);
}
}
public static void inOrder(BSNode root) {
if(root != null) {
inOrder(root.left);
System.out.print(root.val+" ");
inOrder(root.right);
}
}
public static void postOrder(BSNode root) {
if(root != null) {
postOrder(root.left);
postOrder(root.right);
System.out.print(root.val+" ");
}
}
public static void main(String[] args) {
BSNode bsNode = new BSNode();
bsNode.insert(4);
bsNode.insert(3);
bsNode.insert(1);
bsNode.insert(15);
preOrder(bsNode.root);
System.out.println();
System.out.println(bsNode.search(3));
System.out.println(bsNode.search(2));
bsNode.remove(1);
preOrder(bsNode.root);
}
}
边栏推荐
- HDU - 1501 Zipper(记忆化深搜)
- 1034 Head of a Gang
- Skywalking integrated Nacos dynamic configuration
- Tidb database characteristics summary
- Smartinstantiationawarebeanpostprocessor of the extension point series determines which construction method to execute - Chapter 432
- Using Baidu map to query national subway lines
- Servlet
- [file system] how to run squashfs on UBI
- Talking from mlperf: how to lead the next wave of AI accelerator
- Understanding of C manualresetevent class
猜你喜欢

【自动化运维】自动化运维平台有什么用

Thesis learning record essay multi label lift

three.js小结

Seven major technical updates that developers should pay most attention to on build 2022

相同区域 多源栅格数据 各个像元行列号一致,即行数列数相同,像元大小相同

excel动态图表

Preliminary level of C language -- selected good questions on niuke.com

PLA不粘貼在床上:6個簡單的解决方案

MongoDB:一、MongoDB是什么?MongoDB的优缺点

【ManageEngine卓豪】网络运维管理是什么,网络运维平台有什么用
随机推荐
JDBC connection pool
TIDB数据库特性总结
Distributed lock implementation
Scope data export mat
数据库问题,如何优化Oracle SQL查询语句更快,效率更高
扩散(多源广搜)
Geoffrey Hinton: my 50 years of in-depth study and Research on mental skills
Solve the problem of garbled files uploaded by Kirin v10
OpenGL es: (3) EGL, basic steps of EGL drawing, eglsurface, anativewindow
OpenGL es: (5) basic concepts of OpenGL, the process of OpenGL es generating pictures on the screen, and OpenGL pipeline
【自动化运维】自动化运维平台有什么用
C language beginner level - realize the minesweeping game
LED lighting used in health lighting
Elements of database ER diagram
Record currency in MySQL
【ManageEngine】终端管理系统,助力华盛证券数字化转型
DEV XPO对比之XAF BO
Fixed height of the first column in El table dynamic header rendering
OpenGL es: (1) origin of OpenGL es (transfer)
指数法和Random Forest实现山东省丰水期地表水体信息