当前位置:网站首页>高阶-二叉搜索树详解
高阶-二叉搜索树详解
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);
}
}
边栏推荐
- TiDB单机模拟部署生产环境集群(闭坑实践,亲测有效)
- Talking from mlperf: how to lead the next wave of AI accelerator
- Oracle sequence + trigger
- kotlin位运算的坑(bytes[i] and 0xff 报错)
- srpingboot security demo
- [file system] how to run squashfs on UBI
- three. JS summary
- Timer based on LabVIEW
- Primary application case of Excel DuPont analyzer
- highmap gejson数据格式转换脚本
猜你喜欢

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

JDBC database operation

uniapp树形层级选择器

机械臂速成小指南(六):步进电机驱动器

C# ManualResetEvent 类的理解

让厦门灌口镇田头村变“甜头”村的特色农产品之一是

TiDB单机模拟部署生产环境集群(闭坑实践,亲测有效)

Excel dynamic chart

freeswitch拨打分机号

The row and column numbers of each pixel of multi-source grid data in the same area are the same, that is, the number of rows and columns are the same, and the pixel size is the same
随机推荐
Diagramme dynamique Excel
Record currency in MySQL
【文件系统】如何在ubi之上运行squashfs
68 cesium code datasource loading czml
HDU - 1501 zipper (memory deep search)
MinIO纠错码、分布式MinIO集群搭建及启动
蚂蚁新村田头村变甜头村 让厦门灌口镇田头村变甜头村的特色农产品之一是
jdbc 数据库操作
PLA不粘贴在床上:6个简单的解决方案
skywalking集成nacos动态配置
Index method and random forest to realize the information of surface water body in wet season in Shandong Province
【ManageEngine卓豪】局域网监控的作用
[file system] how to run squashfs on UBI
three. JS summary
分布式锁实现
Infinite horizontal marble game
DHT11 温湿度传感器
highmap gejson数据格式转换脚本
Geoffrey Hinton: my 50 years of in-depth study and Research on mental skills
Primary application case of Excel DuPont analyzer