-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBSTVisulization.java
360 lines (318 loc) · 7.92 KB
/
BSTVisulization.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
import java.util.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
public class BSTVisulization extends JFrame implements ActionListener,KeyListener
{
private Node root;
//Node Structure
private static class Node{
JLabel data;
Node left;
Node right;
Points p;
Node(int info){
data=new JLabel(info+"",SwingConstants.CENTER);
data.setFont(new Font("Arial",Font.BOLD,20));
data.setBorder(BorderFactory.createLineBorder(Color.black));
data.setOpaque(true);
data.setBackground(Color.green);
p=null;
}
void setPoints(int x1,int y1,int x2,int y2){
p = new Points(x1,y1,x2,y2);
}
}
//Points structure
private static class Points{
int x1=0,x2=0,y2=0,y1=0;
Points(int x1,int y1,int x2,int y2){
this.x1=x1;
this.x2=x2;
this.y2=y2;
this.y1=y1;
}
public String toString(){
return "x1 = "+x1+", y1 = "+y1+", x2 = "+x2+", y2 = "+y2;
}
}
private JButton btnAdd,btnDelete;
private JTextField tf;
private int X=300,Y=75;
private Graphics2D g2;
private Rectangle size;
public void paint(Graphics g){
super.paintComponents(g);
g2 = (Graphics2D)g;
g2.setStroke(new BasicStroke(3.0f));
Stack<Node> s=new Stack<>();
Node curr=root;
Points pts;
while(!s.isEmpty() || curr!=null){
while(curr!=null){
s.push(curr);
curr=curr.left;
}
if(!s.isEmpty())
curr=s.pop();
pts=curr.p;
g2.drawLine(pts.x1+7, pts.y1+30, pts.x2+3, pts.y2+10);
curr=curr.right;
}
// x1 = label.getX()+7
// y1 = label.getY()+30
}
public BSTVisulization(){
setLayout(null); // layout
setSize(1200, 700); //frame size
size=getBounds();
X=size.width/2;
//For geting data.
tf=new JTextField("");
tf.setFont(new Font("Arial",Font.BOLD,20));
tf.setBounds(size.width-300,20,150,30);
tf.addKeyListener(this);
add(tf);
//Add Button
btnAdd=new JButton("Add");
btnAdd.setFont(new Font("Arial",Font.BOLD,20));
btnAdd.setBounds(size.width-130,20,100,30);
btnAdd.addActionListener(this);
add(btnAdd);
//Delete Button
btnDelete=new JButton("Delete");
btnDelete.setFont(new Font("Arial",Font.BOLD,20));
btnDelete.setBounds(size.width-130,60,100,30);
btnDelete.addActionListener(this);
add(btnDelete);
tf.requestFocusInWindow();
setTitle("Binary Search Tree Visulization"); //Title Frame
setResizable(false);
setLocationRelativeTo(null);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
//Override method.
public void actionPerformed(ActionEvent evt){
try{
int data=Integer.parseInt(tf.getText());
if(evt.getSource()==btnAdd){
add(data);
}else{
delete(data);
}
inorder(root);
System.out.println("\nBST Height : "+calculateHeight(root)+"\n");
tf.setText("");
tf.requestFocusInWindow();
}
catch(Exception e){
JOptionPane.showMessageDialog(null,"Please Enter Integer.");
}
}
public void keyTyped(KeyEvent evt){
char c=evt.getKeyChar();
if(c=='a' || c=='A'){
try{
String data=tf.getText();
evt.consume(); // Not type 'a' or 'A' character in textfield
if(!data.isEmpty()){
add(Integer.parseInt(data));
}
inorder(root);
System.out.println("\nBST Height : "+calculateHeight(root)+"\n");
tf.requestFocusInWindow();
}
catch(Exception e){
JOptionPane.showMessageDialog(null,"Please Enter Integer.");
}
tf.setText("");
}
else if(c=='d' || c=='D'){
try{
String data=tf.getText();
evt.consume(); // Not type 'd' or 'D' character in textfield
if(!data.isEmpty()){
delete(Integer.parseInt(data));
}
inorder(root);
System.out.println("\nBST Height : "+calculateHeight(root)+"\n");
tf.requestFocusInWindow();
}
catch(Exception e){
JOptionPane.showMessageDialog(null,"Please Enter Integer.");
}
tf.setText("");
}
else if(c>='a' && c<='z' || c>='A' && c<='Z')
evt.consume();
}
public void keyPressed(KeyEvent evt){}
public void keyReleased(KeyEvent evt){}
//Add element in BST.
public void add(int info){
Node newNode=new Node(info);
if(root==null){
root=newNode;
newNode.data.setBounds(600,10,40,40);
newNode.p=new Points(0,0,0,0);
}
else{
Node curr=root,pre=root;
int temp;
X=getBounds().width/2;
while(curr!=null){
pre=curr;
temp=Integer.parseInt(curr.data.getText());
if(info==temp){
JOptionPane.showMessageDialog(null,info+" is already exist.");
return;
}
else if(temp>info){
curr=curr.left;
}
else{
curr=curr.right;
}
X/=2;
}
temp=Integer.parseInt(pre.data.getText());
int x=pre.data.getX();
int y=pre.data.getY();
if(temp>info){
pre.left=newNode;
newNode.data.setBounds(x-X,y+Y,40,40);
// x1=x;y1=y+20;x2=x-X+20;y2=y+Y+20;
newNode.p=new Points(x,y+20,x-X+20,y+Y+20);
}
else{
pre.right=newNode;
newNode.data.setBounds(x+X,y+Y,40,40);
// x1=x+40;y1=y+20;x2=x+X+20;y2=y+Y+20;
newNode.p=new Points(x+40,y+20,x+X+20,y+Y+20);
}
}
paint(getGraphics());
add(newNode.data);
validate();
repaint();
}
//Inorder logic
public void inorder(Node root){
if(root==null)
return;
inorder(root.left);
System.out.print(root.data.getText()+" ");
inorder(root.right);
}
// Calculate Height of BST using recursive method.
public int calculateHeight(Node root){
if(root==null){
return 0;
}
return 1 + Math.max(calculateHeight(root.left), calculateHeight(root.right));
}
// Delete Node from BST
public String delete(int data){
if(root==null){
JOptionPane.showMessageDialog(null,"BST is empty.");
}else{
Node curr=root,pre=root;
while(curr!=null){
int info=Integer.parseInt(curr.data.getText());
if(info==data){
break;
}else if(info>data){
pre=curr;
curr=curr.left;
}else{
pre=curr;
curr=curr.right;
}
}
if(curr==null){ // data is not find.
JOptionPane.showMessageDialog(null,data+" is not available.");
}
else if(curr.left==null || curr.right==null){ // data has 0 or 1 child
this.remove(curr.data);
revalidate();
repaint();
if(curr!=root){
Node address = curr.left!=null?curr.left:curr.right;
// curr.data>pre.data
int preData=Integer.parseInt(pre.data.getText());
int currData=Integer.parseInt(curr.data.getText());
if(currData>preData){
pre.right=address;
}else{
pre.left=address;
}
}else{
if(curr.left!=null){
root=curr.left;
}else{
root=curr.right;
}
}
reArrangeNode(root, root, getBounds().width/2);
return data+" deleted successfully.";
}else{ // data has 2 child.
this.remove(curr.data);
revalidate();
repaint();
Node nextRoot=null,preRoot=curr;
nextRoot=curr.left;
while(nextRoot.right!=null){
preRoot=nextRoot;
nextRoot=nextRoot.right;
}
if(preRoot!=curr){
preRoot.right=nextRoot.left;
}else{
preRoot.left=nextRoot.left;
}
curr.data=nextRoot.data;
reArrangeNode(root, root, getBounds().width/2);
return data+" deleted successfully.";
}
}
return "";
}
// Rearrange nodes
private void reArrangeNode(Node node,Node pre,int X){
if(node==null)
return;
if(root==node){
node.data.setBounds(X,10,40,40);
node.p=new Points(0,0,0,0);
}else{
int x=pre.data.getX();
int y=pre.data.getY();
int preData=Integer.parseInt(pre.data.getText());
int nodeData=Integer.parseInt(node.data.getText());
if(nodeData<preData){
node.data.setBounds(x-X,y+Y,40,40);
node.p=new Points(x,y+20,x-X+20,y+Y+20);
}
else{
node.data.setBounds(x+X,y+Y,40,40);
node.p=new Points(x+40,y+20,x+X+20,y+Y+20);
}
}
reArrangeNode(node.left , node, X/2);
reArrangeNode(node.right , node, X/2);
}
public static void main(String arg[])
{
BSTVisulization bst=new BSTVisulization();
// bst.add(50);
// bst.add(25);
// bst.add(35);
// bst.add(20);
// bst.add(75);
// bst.add(100);
// bst.add(70);
// bst.add(74);
}
}