Skip to content

Commit d3d05d2

Browse files
author
Bhrigu Kansra
authored
Merge pull request #136 from riya24899/master
Added Binary Search Tree data structure.
2 parents 22c5e7e + 2e56e41 commit d3d05d2

File tree

1 file changed

+206
-0
lines changed
  • Data Structures/BST

1 file changed

+206
-0
lines changed

Data Structures/BST/Java

Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
import java.io.BufferedReader;
2+
3+
import java.io.IOException;
4+
5+
import java.io.InputStream;
6+
7+
import java.io.InputStreamReader;
8+
9+
import java.util.StringTokenizer;
10+
11+
12+
13+
14+
15+
/** Class for buffered reading int and double values */
16+
17+
class Reader {
18+
19+
static BufferedReader reader;
20+
21+
static StringTokenizer tokenizer;
22+
23+
24+
25+
/** call this method to initialize reader for InputStream */
26+
27+
static void init(InputStream input) {
28+
29+
reader = new BufferedReader(
30+
31+
new InputStreamReader(input) );
32+
33+
tokenizer = new StringTokenizer("");
34+
35+
}
36+
37+
38+
39+
/** get next word */
40+
41+
static String next() throws IOException {
42+
43+
while ( ! tokenizer.hasMoreTokens() ) {
44+
45+
//TODO add check for eof if necessary
46+
47+
tokenizer = new StringTokenizer(
48+
49+
reader.readLine() );
50+
51+
}
52+
53+
return tokenizer.nextToken();
54+
55+
}
56+
57+
58+
59+
static int nextInt() throws IOException {
60+
61+
return Integer.parseInt( next() );
62+
63+
}
64+
65+
static long nextLong() throws IOException {
66+
67+
return Long.parseLong( next() );
68+
69+
}
70+
71+
static double nextDouble() throws IOException {
72+
73+
return Double.parseDouble( next() );
74+
75+
}
76+
77+
}
78+
79+
class node{
80+
long value;
81+
node left;
82+
node right;
83+
node (long v, node l, node r){
84+
value=v;
85+
left=l;
86+
right=r;
87+
}
88+
}
89+
90+
class bst{
91+
92+
node insert (node root1, long target) {
93+
if (root1 == null)
94+
root1 = new node (target,null,null);
95+
else {
96+
if (target <= root1.value)
97+
root1.left = insert (root1.left, target);
98+
else
99+
root1.right = insert (root1.right, target);
100+
}
101+
return root1;
102+
}
103+
104+
void predisplay (node root2,int count1) {
105+
if (root2==null && count1==1)
106+
System.out.println(-1);
107+
else if (root2!=null){
108+
System.out.print (root2.value + " ");
109+
predisplay (root2.left,++count1);
110+
predisplay (root2.right,++count1);
111+
}
112+
}
113+
114+
void indisplay (node root3,int count2) {
115+
if (root3==null && count2==1)
116+
System.out.println(-1);
117+
else if (root3!=null) {
118+
indisplay (root3.left,++count2);
119+
System.out.print (root3.value + " ");
120+
indisplay (root3.right,++count2);
121+
}
122+
}
123+
124+
void postdisplay (node root4,int count3) {
125+
if (root4==null && count3==1)
126+
System.out.println(-1);
127+
else if (root4!=null){
128+
postdisplay (root4.left,++count3);
129+
postdisplay (root4.right,++count3);
130+
System.out.print (root4.value + " ");
131+
}
132+
}
133+
134+
node del (node root7, long val)
135+
{
136+
if (root7 == null)
137+
return root7;
138+
else if (root7.value > val)
139+
root7.left = del (root7.left, val);
140+
else if (root7.value < val)
141+
root7.right = del (root7.right, val);
142+
else
143+
{
144+
145+
if (root7.left != null && root7.right != null)
146+
{
147+
node min = root7.right;
148+
while(min.left!=null) {
149+
min=min.left;
150+
}
151+
root7.value = min.value;
152+
root7.right=del (root7.right, min.value);
153+
}
154+
155+
else if (root7.left != null)
156+
root7 = root7.left;
157+
158+
else if (root7.right != null)
159+
root7 = root7.right;
160+
161+
else
162+
root7 = null;
163+
}
164+
return root7;
165+
}
166+
167+
public static void main(String[] args) throws IOException {
168+
169+
Reader.init(System.in);
170+
node start=null;
171+
bst obj=new bst();
172+
int test=Reader.nextInt();
173+
for (int i=1;i<=test;i++) {
174+
String data=Reader.next();
175+
switch(data)
176+
{
177+
case "INSERT":
178+
long a=Reader.nextLong();
179+
start=obj.insert(start,a);
180+
break;
181+
182+
case "DELETE":
183+
long b=Reader.nextLong();
184+
start=obj.del(start,b);
185+
break;
186+
187+
case "PRINTIN":
188+
obj.indisplay(start,1);
189+
System.out.println();
190+
break;
191+
192+
case "PRINTPOST":
193+
obj.postdisplay(start,1);
194+
System.out.println();
195+
break;
196+
197+
case "PRINTPRE":
198+
obj.predisplay(start,1);
199+
System.out.println();
200+
break;
201+
}
202+
}
203+
204+
}
205+
}
206+

0 commit comments

Comments
 (0)