-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinary Search tree.java
More file actions
74 lines (74 loc) · 1.62 KB
/
Copy pathBinary Search tree.java
File metadata and controls
74 lines (74 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
public class Main {
public static void main(String[] args) {
BT bt=new BT();
bt.root=bt.insert(40,bt.root);
bt.root=bt.insert(70,bt.root);
bt.root=bt.insert(30,bt.root);
bt.root=bt.insert(15,bt.root);
bt.root=bt.insert(55,bt.root);
bt.root=bt.insert(75,bt.root);
bt.root=bt.insert(75,bt.root);
bt.preorder(bt.root);
bt.root=bt.deletion(75,bt.root);
System.out.println();
bt.preorder(bt.root);
}
}
class Node {
int data;
Node left, right;
public Node(int data) {
this.data = data;
}
}
class BT {
Node root;
public BT() {
this.root = null;
}
public Node insert(int data, Node root) {
if (root == null) {
root = new Node(data);
} else if (data < root.data) {
root.left = insert(data, root.left);
} else if (data > root.data) {
root.right = insert(data, root.right);
} else {
System.out.println("Please don't enter duplicate value!");
}
return root;
}
public Node deletion(int data, Node root) {
if (data < root.data) {
root.left = deletion(data, root.left);
} else if (data > root.data) {
root.right = deletion(data, root.right);
} else {
if (root.left == null && root.right == null) {
return null;
} else if (root.right == null) {
return root.left;
} else if (root.left == null) {
return root.right;
}
int min_value=find_min(root.right);
root.data=min_value;
root.right=deletion(min_value,root.right);
}
return root;
}
public int find_min(Node root){
if(root.left==null){
return root.data;
}
return find_min(root.left);
}
public void preorder(Node root){
if(root==null){
return;
}
System.out.println(root.data);
preorder(root.left);
preorder(root.right);
}
}