-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreesGUI.java
More file actions
217 lines (185 loc) · 7.09 KB
/
TreesGUI.java
File metadata and controls
217 lines (185 loc) · 7.09 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
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
package TimeAndSpace.DSA;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
class TreeNode {
int value;
TreeNode left, right;
public TreeNode(int value) {
this.value = value;
left = right = null;
}
}
public class TreesGUI extends JPanel {
private TreeNode root;
private TreeNode searchHighlight;
public TreesGUI() {
this.root = null;
this.searchHighlight = null;
}
public void insert(int value) {
root = insertRec(root, value);
repaint();
}
private TreeNode insertRec(TreeNode root, int value) {
if (root == null) {
return new TreeNode(value);
}
if (value < root.value) {
root.left = insertRec(root.left, value);
} else if (value > root.value) {
root.right = insertRec(root.right, value);
}
return root;
}
public void delete(int value) {
root = deleteRec(root, value);
searchHighlight = null;
repaint();
}
private TreeNode deleteRec(TreeNode root, int value) {
if (root == null) return null;
if (value < root.value) {
root.left = deleteRec(root.left, value);
} else if (value > root.value) {
root.right = deleteRec(root.right, value);
} else {
if (root.left == null) return root.right;
if (root.right == null) return root.left;
root.value = minValue(root.right);
root.right = deleteRec(root.right, root.value);
}
return root;
}
private int minValue(TreeNode root) {
int min = root.value;
while (root.left != null) {
root = root.left;
min = root.value;
}
return min;
}
public boolean search(int value) {
searchHighlight = searchRec(root, value);
repaint();
return searchHighlight != null;
}
private TreeNode searchRec(TreeNode root, int value) {
if (root == null || root.value == value) return root;
return value < root.value ? searchRec(root.left, value) : searchRec(root.right, value);
}
public void reverseTree() {
root = reverseRec(root);
searchHighlight = null;
repaint();
}
private TreeNode reverseRec(TreeNode node) {
if (node == null) return null;
TreeNode temp = node.left;
node.left = node.right;
node.right = temp;
reverseRec(node.left);
reverseRec(node.right);
return node;
}
public int countNodes() {
return countNodesRec(root);
}
private int countNodesRec(TreeNode node) {
return node == null ? 0 : 1 + countNodesRec(node.left) + countNodesRec(node.right);
}
public int treeHeight() {
return treeHeightRec(root);
}
private int treeHeightRec(TreeNode node) {
return node == null ? 0 : 1 + Math.max(treeHeightRec(node.left), treeHeightRec(node.right));
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
setBackground(Color.WHITE);
if (root != null) {
drawTree(g, root, getWidth() / 2, 50, getWidth() / 4);
}
}
private void drawTree(Graphics g, TreeNode node, int x, int y, int xOffset) {
if (node == null) return;
g.setColor(node == searchHighlight ? Color.RED : Color.BLACK);
g.fillOval(x - 15, y - 15, 30, 30);
g.setColor(Color.WHITE);
g.drawString(Integer.toString(node.value), x - g.getFontMetrics().stringWidth(Integer.toString(node.value)) / 2, y + 5);
if (node.left != null) {
g.setColor(Color.BLACK);
g.drawLine(x, y, x - xOffset, y + 50);
drawTree(g, node.left, x - xOffset, y + 50, xOffset / 2);
}
if (node.right != null) {
g.setColor(Color.BLACK);
g.drawLine(x, y, x + xOffset, y + 50);
drawTree(g, node.right, x + xOffset, y + 50, xOffset / 2);
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("Tree Visualization");
TreesGUI treeGUI = new TreesGUI();
JTextField inputField = new JTextField(10);
JButton insertButton = new JButton("Insert");
JButton deleteButton = new JButton("Delete");
JButton reverseButton = new JButton("Reverse");
JButton searchButton = new JButton("Search");
JButton nodeCountButton = new JButton("Node Count");
JButton treeHeightButton = new JButton("Tree Height");
insertButton.addActionListener(e -> {
try {
int value = Integer.parseInt(inputField.getText().trim());
treeGUI.insert(value);
inputField.setText("");
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(frame, "Please enter a valid number!");
}
});
deleteButton.addActionListener(e -> {
try {
int value = Integer.parseInt(inputField.getText().trim());
treeGUI.delete(value);
inputField.setText("");
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(frame, "Enter a valid number!");
}
});
reverseButton.addActionListener(e -> {
treeGUI.reverseTree();
JOptionPane.showMessageDialog(frame, "Tree reversed!");
});
searchButton.addActionListener(e -> {
try {
int value = Integer.parseInt(inputField.getText().trim());
if (treeGUI.search(value)) {
JOptionPane.showMessageDialog(frame, "Value " + value + " found in the tree!");
} else {
JOptionPane.showMessageDialog(frame, "Value " + value + " not found in the tree!");
}
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(frame, "Enter a valid number!");
}
});
nodeCountButton.addActionListener(e -> JOptionPane.showMessageDialog(frame, "Total Nodes: " + treeGUI.countNodes()));
treeHeightButton.addActionListener(e -> JOptionPane.showMessageDialog(frame, "Tree Height: " + treeGUI.treeHeight()));
JPanel controlPanel = new JPanel();
controlPanel.add(new JLabel("Value:"));
controlPanel.add(inputField);
controlPanel.add(insertButton);
controlPanel.add(deleteButton);
controlPanel.add(reverseButton);
controlPanel.add(searchButton);
controlPanel.add(nodeCountButton);
controlPanel.add(treeHeightButton);
frame.setLayout(new BorderLayout());
frame.add(treeGUI, BorderLayout.CENTER);
frame.add(controlPanel, BorderLayout.SOUTH);
frame.setSize(800, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
//done