当前位置:网站首页>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);
}
}
边栏推荐
- Discrimination between left and right limits of derivatives and left and right derivatives
- HCM Beginner (II) - information type
- 【ManageEngine卓豪】用统一终端管理助“欧力士集团”数字化转型
- 手把手教你实现一个深度学习框架...
- Transformer le village de tiantou en un village de betteraves sucrières
- [summary of knowledge points] chi square distribution, t distribution, F distribution
- 2022 年面向初学者的 10 大免费 3D 建模软件
- 【ManageEngine】终端管理系统,助力华盛证券数字化转型
- 让厦门灌口镇田头村变“甜头”村的特色农产品之一是
- [postgraduate entrance examination advanced mathematics Wu Zhongxiang +880 version for personal use] advanced mathematics Chapter II Basic Stage mind map
猜你喜欢

jdbc 数据库操作

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

Freeswitch dial the extension number

【文件系统】如何在ubi之上运行squashfs
![[summary of knowledge points] chi square distribution, t distribution, F distribution](/img/a6/bb5cabbfffb0edc9449c4c251354ae.png)
[summary of knowledge points] chi square distribution, t distribution, F distribution

three. JS summary

【ITSM】什么是ITSM,IT部门为什么需要ITSM

SystemVerilog learning-08-random constraints and thread control

Smartinstantiationawarebeanpostprocessor of the extension point series determines which construction method to execute - Chapter 432
![[postgraduate entrance examination advanced mathematics Wu Zhongxiang +880 version for personal use] advanced mathematics Chapter II Basic Stage mind map](/img/c0/299a406efea51f24b1701b66adc1e3.png)
[postgraduate entrance examination advanced mathematics Wu Zhongxiang +880 version for personal use] advanced mathematics Chapter II Basic Stage mind map
随机推荐
阿里OSS Postman Invalid according to Policy: Policy Condition failed: [“starts-with“, “$key“, “test/“]
make: g++:命令未找到
Linux closes the redis process SYSTEMd+
69 cesium code datasource loading geojson
【ManageEngine卓豪 】助力世界顶尖音乐学院--茱莉亚学院,提升终端安全
Flink practice -- multi stream merge
Highmap gejson data format conversion script
Geoffrey Hinton: my 50 years of in-depth study and Research on mental skills
Kubedm builds kubenetes cluster (Personal Learning version)
jdbc 数据库操作
[postgraduate entrance examination advanced mathematics Wu Zhongxiang +880 version for personal use] advanced mathematics Chapter II Basic Stage mind map
Teach you how to implement a deep learning framework
DHT11 温湿度传感器
PLA不粘贴在床上:6个简单的解决方案
可动的机械挂钟
3D打印机穿线:5种简单的解决方案
golang panic recover自定义异常处理
DEV XPO对比之XAF BO
Thesis learning record essay multi label lift
Servlet