当前位置:网站首页>高阶-二叉搜索树详解
高阶-二叉搜索树详解
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);
}
}
边栏推荐
- 1034 Head of a Gang
- OpenGL es: (1) origin of OpenGL es (transfer)
- excel可视化
- 浏览器端保存数据到本地文件
- uniapp树形层级选择器
- Make Tiantou village sweet. Is Xianjing taro or cabbage the characteristic agricultural product of Tiantou Village
- pycharm 配置jupyter
- golang panic recover自定义异常处理
- Kubedm builds kubenetes cluster (Personal Learning version)
- 【企业数据安全】升级备份策略 保障企业数据安全
猜你喜欢

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

JDBC database operation

Essay learning record essay multi label Global

FPGA - 7系列 FPGA内部结构之Clocking -01- 时钟架构概述

Fixed height of the first column in El table dynamic header rendering

指数法和Random Forest实现山东省丰水期地表水体信息

Make Tiantou village sweet. Is Xianjing taro or cabbage the characteristic agricultural product of Tiantou Village

Diagramme dynamique Excel

Database problems, how to optimize Oracle SQL query statements faster and more efficient

PLA not pasted on the bed: 6 simple solutions
随机推荐
【ManageEngine卓豪】移动终端管理解决方案,助力中州航空产业数字化转型
SystemVerilog学习-06-类的封装
Fragment upload and breakpoint resume
TIDB数据库特性总结
three.js小结
Diffusion (multi-source search)
SQL必会题之留存率
1034 Head of a Gang
excel可视化
Top 10 Free 3D modeling software for beginners in 2022
El tooltip in the table realizes line breaking display
【网络安全工具】USB控制软件有什么用
Preliminary level of C language -- selected good questions on niuke.com
Fixed height of the first column in El table dynamic header rendering
相同区域 多源栅格数据 各个像元行列号一致,即行数列数相同,像元大小相同
Thesis learning record essay multi label lift
Code shoe set - mt3114 · interesting balance - explain it with examples
golang panic recover自定义异常处理
无限水平大理石游戏
kubeadm搭建kubenetes 集群(个人学习版)