/* gui to insert/delete from avl tree of integers, use with BST, BSTgraph 2017 version */ import javax.swing.*; import java.awt.*; import java.awt.Graphics; import java.awt.event.*; import java.io.*; import java.net.*; public class avlgui extends JFrame implements ActionListener { public JTextField ifd, dfd; // single line editible area public JLabel il, dl; Bst Tree; Bstgraph TG; // = new Bstgraph(1024,668); public avlgui(Bst tr) { Tree = tr; ifd = new JTextField(4); dfd = new JTextField(4); il = new JLabel("Insert:"); dl = new JLabel("Delete:"); Container pane = this.getContentPane(); pane.setLayout(new FlowLayout()); pane.add(il); pane.add(ifd); pane.add(dl); pane.add(dfd); ifd.addActionListener(this); dfd.addActionListener(this); this.setBounds(924,600,120,100); this.setVisible(true); this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); TG = new Bstgraph(1024,660); TG.requestFocus(); TG.drawtree(Tree); try{Thread.sleep(400);} catch(Exception e) {} this.ifd.requestFocus(); } // constructor public void actionPerformed(ActionEvent e) { try { String m = null; vertex.message = ""; if (e.getSource() == ifd) // insert { int x = Integer.parseInt(ifd.getText()); Tree = Tree.insert(x); TG.requestFocus(); m = vertex.message; if (m!=null && m.length()>0) { TG.display.drawString(m,20,TG.YDIM-40); try{Thread.sleep(2000);} catch(Exception e1) {} } TG.drawtree(Tree); try{Thread.sleep(150);} catch(Exception e1) {} ifd.setText(""); ifd.requestFocus(); } else if (e.getSource() == dfd) // delete { int x = Integer.parseInt(dfd.getText()); Tree = Tree.delete(x); TG.requestFocus(); m = vertex.message; if (m!=null && m.length()>0) { TG.display.drawString(m,20,TG.YDIM-40); try{Thread.sleep(2000);} catch(Exception e1) {} } TG.drawtree(Tree); try{Thread.sleep(150);} catch(Exception e1) {} dfd.setText(""); dfd.requestFocus(); } } catch (Exception ee) {} } // actionPerformed. public static void main(String[] args) { Bst T = new nil(); // if (args.length>0) // build tree from args // T = new (Integer.parseInt(args[0])); for(int i = 1; i