-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwoThreeTreePanel.java
More file actions
249 lines (233 loc) · 8.11 KB
/
TwoThreeTreePanel.java
File metadata and controls
249 lines (233 loc) · 8.11 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
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
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
public class TwoThreeTreePanel extends JPanel{
//the twoThree tree
private TwoThreeTree tree;
//labels
private JLabel jlbInput = new JLabel("Input:");
//all buttons
private JButton jbtInsert = new JButton("Insert");
private JButton jbtDelete = new JButton("Delete");
private JButton jbtSearch = new JButton("Search");
private JButton jbtSucc = new JButton("Successor");
private JButton jbtMin = new JButton("Min");
private JButton jbtSort = new JButton("Sort");
private JButton jbtOutput = new JButton("Output");
private JButton jbtExit = new JButton("Exit");
//the text field associated with the buttons
private JTextField jtfInput = new JTextField(5);
private JTextArea jta = new JTextArea();
//Panels
private PaintTree jspPanel = new PaintTree();
private JPanel jplButton0 = new JPanel();
private JPanel jplButton1 = new JPanel(new GridLayout(2,3,5,5));
private JPanel jplButton2 = new JPanel(new GridLayout(2,2,5,5));
private JPanel jplText = new JPanel(new BorderLayout());
public TwoThreeTreePanel(){
tree = new TwoThreeTree();
//button area0
jplButton0.setLayout(new FlowLayout(FlowLayout.LEFT,10,0));
jplButton0.add(jlbInput);
jtfInput.setBackground(Color.lightGray);
jplButton0.add(jtfInput);
//button area1
jplButton1.setBorder(new TitledBorder(""));
jplButton1.add(jbtInsert);
jplButton1.add(jbtDelete);
jplButton1.add(jbtSearch);
jplButton1.add(jbtSucc);
//button area2
jplButton2.setBorder(new TitledBorder(""));
jplButton2.add(jbtMin);
jplButton2.add(jbtSort);
jplButton2.add(jbtOutput);
jplButton2.add(jbtExit);
//hold control area
JPanel p = new JPanel(new BorderLayout(5,10));
p.setBorder(new TitledBorder("Control Panel"));
p.add(jplButton0, BorderLayout.NORTH);
p.add(jplButton1, BorderLayout.CENTER);
p.add(jplButton2, BorderLayout.EAST);
JPanel p1 = new JPanel(new BorderLayout());
jspPanel.setBorder(new TitledBorder("Display"));
p1.add(jspPanel, BorderLayout.CENTER);
p1.add(p, BorderLayout.SOUTH);
this.setLayout(new BorderLayout());
add(p1, BorderLayout.CENTER);
jplText.setBorder(new TitledBorder("Txt Output"));
jta.setBackground(Color.lightGray);
jta.setPreferredSize(new Dimension(100,440));
jplText.add(jta, BorderLayout.CENTER);
add(jplText, BorderLayout.EAST);
//add listeners
jbtInsert.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int key = Integer.parseInt(jtfInput.getText());
if (key < 0)
JOptionPane.showMessageDialog(null, "Input must be a positive integer!");
else{
tree.insert(key);
jspPanel.repaint();
}
}
});
jbtDelete.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int key = Integer.parseInt(jtfInput.getText());
if (key < 0)
JOptionPane.showMessageDialog(null, "Input must be a positive integer!");
else if (tree.search(key) == null){
JOptionPane.showMessageDialog(null,
key + " is not in the tree!");
}
else{
tree.delete(key);
jspPanel.repaint();
}
}
});
jbtSearch.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int key = Integer.parseInt(jtfInput.getText());
if (key < 0)
JOptionPane.showMessageDialog(null, "Input must be a positive integer!");
else{
TreeNode node = tree.search(key);
if (node == null){
JOptionPane.showMessageDialog(null,
key + " is not in the tree!");
}
else
jta.setText("Found the node : \n" + node + "\n");
}
}
});
jbtSucc.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int key = Integer.parseInt(jtfInput.getText());
if (key < 0)
JOptionPane.showMessageDialog(null, "Input must be a positive integer!");
else{
TreeNode node = tree.successor2(key);
if (node != null ){
if (tree.getSpecial())
jta.setText("The successor of " + key +
"\n" + node.getKey2()+"\n");
else
jta.setText("The successor of " + key +
"\n" + node.getKey1()+"\n");
tree.setSpecial(false);
}
}
}
});
jbtMin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (tree.getRoot() != null)
jta.setText("The minimum key \n" +
tree.minimum()+"\n");
else
JOptionPane.showMessageDialog(null, "Tree is empty!");
}
});
jbtSort.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (tree.getRoot() != null){
tree.sort();
jta.setText(tree.getSort() + "\n");
}
else
JOptionPane.showMessageDialog(null, "Tree is empty!");
}
});
jbtOutput.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (tree.getRoot() != null){
tree.output();
jta.setText(tree.getOutput() + "\n");
}
else
JOptionPane.showMessageDialog(null, "Tree is empty!");
}
});
jbtExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(-1);
}
});
}
//inner class paintTree for displaying a tree on a scroll panel
class PaintTree extends JPanel{
private int radius = 20; //tree node radius
private int vGap = 50;//gap between two levels in the tree
protected void paintComponent(Graphics g){
super.paintComponent(g);
if (tree.getRoot() != null){
//draw the tree recursively
drawTree(g, tree.getRoot(), getWidth()/2, 30, getWidth()/4);
}
}
private void drawTree(Graphics g, TreeNode root, int x, int y, int hGap){
//draw the root---------
g.drawOval(x-radius, y-radius, 2*radius, 2*radius);
if (root.getDegree() == 2){
if (root.getKey1() < 10)
g.drawString(root.getKey1() + "", x-3, y+4);
else
g.drawString(root.getKey1() + "", x-6, y+4);
}
else{
if (root.getKey1() < 10)
g.drawString(root.getKey1() + " | " + root.getKey2(), x-15, y+4);
else
g.drawString(root.getKey1() + " | " + root.getKey2(), x-18, y+4);
}
//----------------------
if(root.getLeftChild() != null){
//draw a line to the left node
connectLeftChild(g, x - hGap, y + vGap, x, y);
//draw the left subtree recursively
drawTree(g, root.getLeftChild(), x-hGap, y+vGap, hGap/2);
}
if(root.getMidChild() != null){
//draw a line to the left node
connectMidChild(g, y + vGap, x, y);
//draw the left subtree recursively
drawTree(g, root.getMidChild(), x, y+vGap, hGap/2);
}
if(root.getRightChild() != null){
//draw a line to the left node
connectRightChild(g, x + hGap, y + vGap, x, y);
//draw the left subtree recursively
drawTree(g, root.getRightChild(), x+hGap, y+vGap, hGap/2);
}
}
private void connectLeftChild(Graphics g, int x1, int y1, int x2, int y2){
double d = Math.sqrt(vGap * vGap + (x2 - x1) * (x2 - x1));
int x11 = (int)(x1 + radius * (x2 - x1) / d);
int y11 = (int)(y1 - radius * vGap / d);
int x21 = (int)(x2 - radius * (x2 - x1) / d);
int y21 = (int)(y2 + radius * vGap / d);
g.drawLine(x11, y11, x21, y21);
}
private void connectMidChild(Graphics g, int y1, int x2, int y2) {
int x11 = x2;
int y11 = y1 - radius;
int x21 = x2;
int y21 = y2 + radius;
g.drawLine(x11, y11, x21, y21);
}
private void connectRightChild(Graphics g, int x1, int y1, int x2, int y2) {
double d = Math.sqrt(vGap * vGap + (x2 - x1) * (x2 - x1));
int x11 = (int)(x1 - radius * (x1 - x2) / d);
int y11 = (int)(y1 - radius * vGap / d);
int x21 = (int)(x2 + radius * (x1 - x2) / d);
int y21 = (int)(y2 + radius * vGap / d);
g.drawLine(x11, y11, x21, y21);
}
}
}