当前位置:网站首页>高阶-二叉搜索树详解
高阶-二叉搜索树详解
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);
}
}
边栏推荐
- SOE spatial analysis server MySQL and PostGIS geospatial database of Postgres anti injection attack
- FPGA - 7系列 FPGA内部结构之Clocking -01- 时钟架构概述
- highmap gejson数据格式转换脚本
- Highmap gejson data format conversion script
- Primary application case of Excel DuPont analyzer
- SOE空间分析服务器 MySQL以及PostGres的地理空间库PostGIS防注入攻击
- MySQL怎么存储emoji?
- 3D打印机穿线:5种简单的解决方案
- excel初级应用案例——杜邦分析仪
- 地宫取宝(记忆化深搜)
猜你喜欢

Advanced drawing skills of Excel lecture 100 (1) - use Gantt chart to show the progress of the project

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

Infinite horizontal marble game

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

68 cesium code datasource loading czml

记磁盘扇区损坏导致的Mysql故障排查

freeswitch拨打分机号

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

π disk, turning your computer into a personal private cloud

IT服务管理(ITSM)在高等教育领域的应用
随机推荐
c# Xml帮助类
excel可视化
【ManageEngine卓豪】移动终端管理解决方案,助力中州航空产业数字化转型
Database problems, how to optimize Oracle SQL query statements faster and more efficient
Timer based on LabVIEW
【ManageEngine卓豪 】助力世界顶尖音乐学院--茱莉亚学院,提升终端安全
数据库er图组成要素
指数法和Random Forest实现山东省丰水期地表水体信息
【ManageEngine】如何实现网络自动化运维
Linux closes the redis process SYSTEMd+
Tidb database characteristics summary
LED lighting used in health lighting
Thesis learning record essay multi label lift
SystemVerilog学习-10-验证量化和覆盖率
3D打印机穿线:5种简单的解决方案
讓田頭村變甜頭村的特色農產品是仙景芋還是白菜
Stack Title: parsing Boolean expressions
基于LabVIEW的计时器
Differences between in and exists in MySQL
OpenGL es: (4) detailed explanation of EGL API (Continued)