当前位置:网站首页>High order binary search tree
High order binary search tree
2022-07-01 06:20:00 【qq_ fifty-two million twenty-five thousand two hundred and eight】
1. Binary search tree is also called binary sort tree , It could be an empty tree , Or a binary tree with the following properties :
(1) 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
(2) 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
(3) Its left and right subtrees are also binary search trees 
2. Implementation of binary search tree
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) {
// Delete operation
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);
}
}
边栏推荐
- OpenGL es: (5) basic concepts of OpenGL, the process of OpenGL es generating pictures on the screen, and OpenGL pipeline
- 1034 Head of a Gang
- 手把手教你实现一个深度学习框架...
- 【企业数据安全】升级备份策略 保障企业数据安全
- SystemVerilog learning-06-class encapsulation
- Pychart configuring jupyter
- HDU - 1501 Zipper(记忆化深搜)
- Using Baidu map to query national subway lines
- 3D printer threading: five simple solutions
- 记磁盘扇区损坏导致的Mysql故障排查
猜你喜欢

Pla ne colle pas sur le lit: 6 solutions simples

高阶-二叉平衡树

浅谈SIEM

HCM Beginner (III) - quickly enter pa70 and pa71 to browse employee information PA10

OpenGL es: (3) EGL, basic steps of EGL drawing, eglsurface, anativewindow

【企业数据安全】升级备份策略 保障企业数据安全

FPGA - clocking -02- clock wiring resources of internal structure of 7 Series FPGA

SystemVerilog learning-10-validation quantification and coverage

Excel visualization

让厦门灌口镇田头村变“甜头”村的特色农产品之一是
随机推荐
Kubedm builds kubenetes cluster (Personal Learning version)
利用百度地图查询全国地铁线路
扩散(多源广搜)
golang panic recover自定义异常处理
Projects and dependencies in ABP learning solutions
Record currency in MySQL
PLA not pasted on the bed: 6 simple solutions
Thoughts on a "01 knapsack problem" expansion problem
【企业数据安全】升级备份策略 保障企业数据安全
高阶-二叉搜索树详解
How does MySQL store Emoji?
交换机配置软件具有的作用
Uniapp tree level selector
Treasure taking from underground palace (memory based deep search)
HCM Beginner (IV) - time
可动的机械挂钟
MySQL里记录货币
证券类开户有什么影响 开户安全吗
ABP 学习解决方案中的项目以及依赖关系
【ManageEngine】如何实现网络自动化运维