-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBtreevsBST.java
285 lines (282 loc) · 9.55 KB
/
BtreevsBST.java
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import java.time.Duration;
import java.time.Instant;
import java.util.Scanner;
//the class for BST begins here
class BST
{
class BSTNode
{
int Value;
BSTNode LeftChild,RightChild;
public BSTNode(int Data){
Value=Data;
LeftChild=RightChild=null;
}
}
BSTNode bstroot;
void inordertraversalinvoker(){
InOrderTraversal(bstroot);
}
void InOrderTraversal(BSTNode bstroot){
if(bstroot!=null){
InOrderTraversal(bstroot.LeftChild);
System.out.println(bstroot.Value+" ");
InOrderTraversal(bstroot.RightChild);
}
}
void InsertNode(int N)
{
bstroot=InsertInBST(bstroot,N);
}
// Recursive Function to insert a Node in BST
BSTNode InsertInBST(BSTNode bstroot,int Value)
{
if(bstroot==null){
bstroot=new BSTNode(Value);
}
else if(Value<bstroot.Value){
bstroot.LeftChild=InsertInBST(bstroot.LeftChild, Value);
}
else{
bstroot.RightChild=InsertInBST(bstroot.RightChild, Value);
}
return bstroot;
}
BSTNode SearchInBST(BSTNode bstroot,int Value_to_be_searched){
if(bstroot==null||bstroot.Value==Value_to_be_searched){
return bstroot;
}
else if(bstroot.Value>Value_to_be_searched){
return SearchInBST(bstroot.LeftChild, Value_to_be_searched);
}
else{
return SearchInBST(bstroot.RightChild, Value_to_be_searched);
}
}
boolean searchinbstInvoker(int Value_to_be_searched){
SearchInBST(bstroot, Value_to_be_searched);
if (bstroot!= null){
return true;
}
else{
return false;
}
}
}
class BTree
{
// the order of the given tree is 3
private int T=3;
public class Node {
// int n is the current number of keys in the B tree
int n;
// the maximum number of keys that a node can hold is 2T-1 see that this is not applicable for the root
int key[] = new int[2*T-1];
// the maximum number of children that a Node can have is one more than Keys in the Node
Node child[] = new Node[2*T];
// an array of child pointer is created on above line and below line tells us that it is leaf node
boolean leaf = true;
public int Find(int k) {
for (int i = 0; i < this.n; i++) {
if (this.key[i] == k) {
return i;
}
}
return -1;
}
}
Node root=new Node();
public BTree() {
root = new Node();
root.n = 0;
root.leaf = true;
}
private void split(Node x, int pos, Node y) {
// taking a new node for operations and making some attributes of that Nodes to new node
Node z = new Node();
z.leaf = y.leaf;
z.n = T - 1;
// from the sorted Node we write all elements of next hlf to begining of new node
for (int j = 0; j < T - 1; j++) {
z.key[j] = y.key[j + T];
}
// iff given Node is not leaf than manage the children
if (!y.leaf) {
for (int j = 0; j < T; j++) {
// make the Next hlf children of y children of z
z.child[j] = y.child[j + T];
}
}
// setting the Keys in Node Y equal to T-1
y.n = T - 1;
// adjusting the position of the child from the given pos
for (int j = x.n; j >= pos + 1; j--) {
x.child[j + 1] = x.child[j];
}
// make z the child at pos+1 on node x
x.child[pos + 1] = z;
for (int j = x.n - 1; j >= pos; j--) {
x.key[j + 1] = x.key[j];
}
x.key[pos] = y.key[T - 1];
x.n = x.n + 1;
}
public void insert(final int key) {
// allocate the memory of a temporary Node and make it equal to root
Node r = root;
// we need to handle the case where Node is Full
if (r.n == 2*T - 1) {
// creating another Temp Node
Node s = new Node();
// making that node as root declaring it as non-leaf node and making the number of Keys as 0 for that node
root = s;
s.leaf = false;
s.n = 0;
s.child[0] = r;
// now we split the Node such that we are abe to insert a given value
split(s, 0, r);
_insert(s, key);
} else {
_insert(r, key);
}
}
final private void _insert(Node x, int k) {
// if the given node is a leaf Node
if (x.leaf) {
int i = 0;
//shift all the Greater than NewKey Nodes to right by one and find the position for the NewKey
for (i = x.n - 1; i >= 0 && k < x.key[i]; i--) {
x.key[i + 1] = x.key[i];
}
// overwrite the value of key to that position and increment the current counter
x.key[i + 1] = k;
x.n = x.n + 1;
} else {
int i = 0;
//finding the Child who is going to have the NewKey and also the value from we can spilt the node
while (i >= 0 && x.key[i] > k){
i--;
}
// assigning that value to the tmporary Node
Node tmp = x.child[i];
// handling the case where that child is full by spilting
if (tmp.n == 2*T - 1) {
split(x, i, tmp);
if (k > x.key[i]) {
i++;
}
}
_insert(x.child[i], k);
}
}
public boolean Contain(int k) {
if (this.Search(root, k) != null) {
return true;
} else {
return false;
}
}
private void display(Node x) {
assert (x == null);
for (int i = 0; i < x.n; i++) {
System.out.print(x.key[i] + " ");
}
if (!x.leaf) {
for (int i = 0; i < x.n + 1; i++) {
display(x.child[i]);
}
}
}
public void display() {
display(root);
}
private Node Search(Node x, int key) {
int i = 0;
//if there is No Node than return x;
if (x == null)
return x;
for (i = 0; i < x.n; i++) {
// iff we did not find the Key than break and execute else part
if (key < x.key[i]) {
break;
}
if (key == x.key[i]) {
return x;
}
}
if (x.leaf) {
return null;
}
// recursively search again for the given Key
else {
return Search(x.child[i], key);
}
}
};
public class BtreevsBST{
public static void main(String[] args) {
BTree bt = new BTree();
BST b=new BST();
int N,option;
Scanner sc=new Scanner(System.in);
while(true)
{
System.out.println("---------------B-tree vs BST---------------");
System.out.println("\n1.Insert Node in BST \t2.Perform Inorder Traversal \n3.Search Node \t\t4.Exit");
System.out.println("Enter the choice of operation to be performed:");
option=sc.nextInt();
switch (option)
{
case 1:
System.out.println("Enter the Number you want to Insert:");
N=sc.nextInt();
long start2 = System.nanoTime();
bt.insert(N);
long end2 = System.nanoTime();
System.out.println("Time to insert in B tree is "+ (end2-start2)+"ms");
long start1 = System.nanoTime();
b.InsertNode(N);
long end1 = System.nanoTime();
System.out.println("Time to insert in BST is "+ (end1-start1)+"ms");
break;
case 2:
System.out.println("The Traversal of the B-tree is :-");
long start5 = System.nanoTime();
bt.display();
long end5 = System.nanoTime();
System.out.println("\n Time to Display in B-tree is "+ (end5-start5)+"ms");
System.out.println("The Traversal of the BST is :-");
long start6 = System.nanoTime();
b.inordertraversalinvoker();
long end6 = System.nanoTime();
System.out.println("\n Time to Display in BST is "+ (end6-start6)+"ms");
break;
case 3:
System.out.println("Enter the Number you want to Search:");
N=sc.nextInt();
long start3 = System.nanoTime();
boolean contains=bt.Contain(N);
long end3 = System.nanoTime();
long start4 = System.nanoTime();
boolean present=b.searchinbstInvoker(N);
long end4 = System.nanoTime();
if(present){
System.out.println("The Node is present in B-Tree and the time taken to search in B-tree is "+(end4-start4)+"ns");
}
if(contains){
System.out.println("The Node is present in BST and the time taken to search in BST is "+(end3-start3)+"ns");
}
if(present!=true){
System.out.println("Node is not Present");
}
break;
case 4:
System.out.println("Program ended successfully.\n Hence we conclude that B-Tree is efficient on DBMS as compared to BST and AVL trees ");
System.exit(0);
default:
System.out.println("Sorry Please Enter the Right Choice!!!");
break;
}
}
}
}