当前位置:网站首页>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);
}
}
边栏推荐
- ManageEngine卓豪助您符合ISO 20000标准(四)
- Movable mechanical wall clock
- Solve the problem of garbled files uploaded by Kirin v10
- Although pycharm is marked with red in the run-time search path, it does not affect the execution of the program
- PLA不粘贴在床上:6个简单的解决方案
- 10 golang operator
- 端口扫描工具对企业有什么帮助?
- IT服务管理(ITSM)在高等教育领域的应用
- Essay learning record essay multi label Global
- Pit of kotlin bit operation (bytes[i] and 0xff error)
猜你喜欢

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

Uniapp tree level selector

Solve the problem of garbled files uploaded by Kirin v10

69 Cesium代码datasource加载geojson

PLA not pasted on the bed: 6 simple solutions

Infinite horizontal marble game

Index method and random forest to realize the information of surface water body in wet season in Shandong Province

Geoffrey Hinton: my 50 years of in-depth study and Research on mental skills

3D printer threading: five simple solutions

Understanding of C manualresetevent class
随机推荐
阶乘约数(唯一分解定理)
10 golang operator
高阶-二叉搜索树详解
One of the characteristic agricultural products that make Tiantou village, Guankou Town, Xiamen into a "sweet" village is
69 cesium code datasource loading geojson
交换机配置软件具有的作用
ManageEngine卓豪助您符合ISO 20000标准(四)
[note] e-commerce order data analysis practice
HCM Beginner (III) - quickly enter pa70 and pa71 to browse employee information PA10
Recueillir des trésors dans le palais souterrain (recherche de mémoire profonde)
1034 Head of a Gang
端口扫描工具对企业有什么帮助?
让田头村变甜头村的特色农产品是仙景芋还是白菜
SOE spatial analysis server MySQL and PostGIS geospatial database of Postgres anti injection attack
Understanding of C manualresetevent class
HDU - 1501 Zipper(记忆化深搜)
JMM详解
HCM Beginner (IV) - time
做技术,自信不可或缺
Database problems, how to optimize Oracle SQL query statements faster and more efficient