import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Arbore extends JApplet
{
private JPanel panouNord, panouVest, panouEst, panouSud;
private JPanel subSouthPanel;
private DeseneazaPanou drawArea;
protected JLabel statusLabel;
protected JTextField preTextField, inTextField, postTextField;
protected JButton clearButton, butonInsereaza, butonCauta, butonSterge;
private int latime = 620, inaltime = 450;
protected boolean amIanApplication;
protected String dsvInfo, llInfo;
public void setWidth(int w)
{
latime = (w >= 0 ? w : latime);
}
public void setHeight(int h)
{
inaltime = (h >= 0 ? h : inaltime);
}
public void init(boolean isApplication)
{
amIanApplication = isApplication;
init();
}
public void checkButtons()
{
boolean enabled = drawArea.hasElements();
butonCauta.setEnabled(enabled);
butonSterge.setEnabled(enabled);
preTextField.setText(drawArea.getPreOrder());
inTextField.setText(drawArea.getInOrder());
postTextField.setText(drawArea.getPostOrder());
}
public void init()
{
statusLabel = new JLabel("Arbore Binar");
panouNord = new JPanel();
panouVest = new JPanel();
panouEst = new JPanel();
panouSud = new JPanel();
drawArea = new DeseneazaPanou();
preTextField = new JTextField();
preTextField.setEditable(false);
inTextField = new JTextField();
inTextField.setEditable(false);
postTextField = new JTextField();
postTextField.setEditable(false);
clearButton = new JButton("Sterge arborele");
clearButton.setMnemonic('C');
clearButton.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
JOptionPane.showMessageDialog(Arbore.this, "Arborele este vid", "Vid",
JOptionPane.PLAIN_MESSAGE);
drawArea.clear();
Arbore.this.checkButtons();
}
}
);
butonInsereaza = new JButton("Adauga...");
butonInsereaza.setMnemonic('A');
butonInsereaza.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String value = JOptionPane.showInputDialog(Arbore.this, "Add Element");
if(value != null && !value.trim().equals(""))
{
drawArea.addElement(value);
}
Arbore.this.checkButtons();
doLayout();
}
}
);
butonCauta = new JButton("Cauta...");
butonCauta.setMnemonic('F');
butonCauta.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String value = JOptionPane.showInputDialog(Arbore.this,
"Find Element");
if(value != null && !value.trim().equals(""))
{
if(drawArea.findElement(value))
JOptionPane.showMessageDialog(Arbore.this,
"Element " + value.trim() + " Gasit",
"Sterge Element",
JOptionPane.PLAIN_MESSAGE);
else
JOptionPane.showMessageDialog(Arbore.this,
"Elementu nu a fost gasit ",
"Elementul a fost gasit", JOptionPane.PLAIN_MESSAGE);
}
}
}
);
butonCauta.setEnabled(false);
butonSterge = new JButton("Sterge...");
butonSterge.setMnemonic('D');
butonSterge.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String value = JOptionPane.showInputDialog(Arbore.this,
"Sterge Element");
if(value != null && !value.trim().equals(""))
{
if(!drawArea.deleteElement(value))
JOptionPane.showMessageDialog(Arbore.this,
"Elementul nu a fost gasit ",
"Sterge Element", JOptionPane.PLAIN_MESSAGE);
}
Arbore.this.checkButtons();
}
}
);
butonSterge.setEnabled(false);
subSouthPanel = new JPanel();
subSouthPanel.setLayout(new FlowLayout());
subSouthPanel.add(clearButton);
subSouthPanel.add(butonInsereaza);
subSouthPanel.add(butonCauta);
subSouthPanel.add(butonSterge);
panouSud.setLayout(new BorderLayout());
panouSud.add(subSouthPanel, BorderLayout.CENTER);
panouSud.add(statusLabel, BorderLayout.SOUTH);
panouVest.add(new JLabel(" "));
panouEst.setLayout(new GridLayout(10, 1));
panouEst.add(new JLabel("Traversari:"));
panouEst.add(new JLabel("preordine:"));
panouEst.add(preTextField);
panouEst.add(new JLabel("inordine:"));
panouEst.add(inTextField);
panouEst.add(new JLabel("postordine:"));
panouEst.add(postTextField);
Container myContainer = getContentPane();
myContainer.setLayout(new BorderLayout());
myContainer.add(new JScrollPane(drawArea), BorderLayout.CENTER);
myContainer.add(panouVest, BorderLayout.WEST);
myContainer.add(panouSud, BorderLayout.SOUTH);
myContainer.add(panouEst, BorderLayout.EAST);
addKeyListener(
new KeyAdapter()
{
public void keyTyped( KeyEvent e )
{
String value = "" + e.getKeyChar();
if(!value.trim().equals(""))
{
drawArea.addElement(value);
Arbore.this.checkButtons();
doLayout();
}
}
}
);
setSize(latime, inaltime);
setVisible(true);
}
public static void main(String args[])
{
int latime = 620, inaltime = 450;
JFrame myFrame = new JFrame("Aplicatie");
myFrame.addWindowListener(
new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
Arbore appletObject = new Arbore();
appletObject.setWidth(latime);
appletObject.setHeight(inaltime);
appletObject.init(true);
appletObject.start();
myFrame.getContentPane().add(appletObject);
myFrame.setSize(latime, inaltime);
myFrame.setVisible(true);
}
}
///////clasa deseneazaPanou//////////
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class DeseneazaPanou extends JPanel
{
protected Nod radacina;
boolean primaData;
private int maxx;
private int maxy;
public DeseneazaPanou()
{
setBackground(Color.white);
radacina = null;
primaData = true;
maxx = 600;
maxy = 400;
repaint();
}
public Dimension getPreferredSize()
{
return new Dimension(maxx, maxy);
}
public void paint(Graphics g)
{
super.paint(g);
int xstg = 0;
int xdrt = 0;
int yjos = 0;
boolean changesize = false;
if(primaData && radacina != null)
{
FontMetrics fm = g.getFontMetrics();
radacina.setMetrics(fm.getHeight(), fm.getMaxAdvance(), fm.getAscent());
primaData = false;
}
if(radacina != null)
{
xstg = Math.abs(radacina.getMinx(0));
xdrt = Math.abs(radacina.getMaxx(0)) + 60;
yjos = radacina.getMaxy(0) + radacina.getinaltime() * 3;
}
if(maxx < (xdrt + xstg))
{
maxx = (xdrt + xstg);
changesize = true;
}
if(maxy < yjos)
{
maxy = yjos;
changesize = true;
}
if(changesize == true)
{
setSize(maxx, maxy);
}
if(radacina != null)
radacina.inorder(g, xstg + 20, 16);
else
{
int y = 16;
xstg = 30;
g.drawString("radacina", xstg - 12, y);
g.drawLine(xstg, y + 2, xstg, y + 16);
g.drawLine(xstg, y + 16, xstg - 4, y + 16 - 4);
g.drawLine(xstg, y + 16, xstg + 4, y + 16 - 4);
g.drawString("null", xstg - 12, y + 28);
}
}
public boolean addElement(String s)
{
boolean retValue;
if(radacina == null)
radacina = new Nod(s);
retValue = radacina.add(s);
repaint();
return retValue;
}
/**
* Gets the Pre-Order traversal
* @return a <code>String</code> representation of the pre-order traversal
*/
public String getPreOrder()
{
String temp;
if(radacina == null)
return "";
temp = radacina.txtPreorder();
return temp.substring(1, temp.length());
}
/**
* Gets the In-Order traversal
* @return a <code>String</code> representation of the in-order traversal
*/
public String getInOrder()
{
String temp;
if(radacina == null)
return "";
temp = radacina.txtInorder();
return temp.substring(1, temp.length());
}
/**
* Gets the Post-Order traversal
* @return a <code>String</code> representation of the post-order traversal
*/
public String getPostOrder()
{
String temp;
if(radacina == null)
return "";
temp = radacina.txtPostorder();
return temp.substring(1, temp.length());
}
/**
* Clears the tree
*/
public void clear()
{
radacina = null;
primaData = true;
repaint();
}
public boolean findElement(String elem)
{
if(radacina == null)
return false;
return radacina.findElement(elem);
}
/**
* Deletes an element
* @param elem the element
* @return <code>true</code> if the element was deleted successfully
*/
public boolean deleteElement(String elem)
{
if(radacina == null)
return false;
Nod current = radacina;
Nod parent = radacina;
boolean isstgChild = true;
while(! current.data.equals(elem))
{
parent = current;
if(current.data.compareTo(elem) > 0)
{
isstgChild = true;
current = current.stg;
}
else
{
isstgChild = false;
current = current.drt;
}
if(current == null)
return false;
}
if(current.stg == null && current.drt == null)
{
if(current == radacina)
radacina = null;
else
if(isstgChild)
{
parent.stg = null;
parent.nrFiiStg = 0;
}
else
{
parent.drt = null;
parent.nrFiiDrt = 0;
}
}
else if(current.drt == null)
if(current == radacina)
radacina = current.stg;
else if(isstgChild)
parent.stg = current.stg;
else
parent.drt = current.stg;
else if(current.stg == null)
if(current == radacina)
radacina = current.drt;
else if(isstgChild)
parent.stg = current.drt;
else
parent.drt = current.drt;
else
{
Nod successor = getSuccessor(current);
if(current == radacina)
radacina = successor;
else if(isstgChild)
parent.stg = successor;
else
parent.drt = successor;
successor.stg = current.stg;
}
if(radacina != null)
{
radacina.setNumChildsNull();
radacina.adjustNumChilds();
}
repaint();
return true;
}
private Nod getSuccessor(Nod delNod)
{
Nod successorParent = delNod;
Nod successor = delNod;
Nod current = delNod.drt;
while(current != null)
{
successorParent = successor;
successor = current;
current = current.stg;
}
if(successor != delNod.drt)
{
successorParent.stg = successor.drt;
successor.drt = delNod.drt;
}
return successor;
}
public boolean hasElements()
{
return radacina != null;
}
}
/////////////////clasa Nod/////////////
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.util.*;
class Nod
{
protected String data;
protected int nrFiiStg;
protected int nrFiiDrt;
protected Nod stg;
protected Nod drt;
private int latime;
private int inaltime;
private int strinaltime;
private int strlatime;
private int strAscent;
private int arrow;
private int margin;
private final int rounded = 4;
private final int ptrline = 8;
private final int maxLetters = 2;
/**
* Class Nod constructor
* @param d the data
*/
public Nod(String d)
{
data = d;
stg = null;
drt = null;
nrFiiStg = 0;
nrFiiDrt = 0;
}
public Nod(String d, int h, int w, int a)
{
data = d;
stg = null;
drt = null;
setMetrics(h, w, a);
}
public void setMetrics(int h, int w, int a)
{
latime = h * maxLetters;
inaltime = 12 + h;
strinaltime = h;
strlatime = w;
strAscent = a;
arrow = ptrline;
margin = 2;
}
/**
* Adds a
* @param d the data
* @param h the inaltime
* @param w the latime
* @param a the ascent
*/
public boolean add(String o)
{
boolean retValue = true;
if(data.equals(o))
{
retValue = false;
}
else
{
if(data.compareTo(o) > 0)
{
if(stg == null)
{
stg = new Nod(o, strinaltime, strlatime, strAscent);
nrFiiStg++;
}
else
{
retValue = stg.add(o);
if(retValue)
nrFiiStg++;
}
}
else
{
if(drt == null)
{
drt = new Nod(o, strinaltime, strlatime, strAscent);
nrFiiDrt++;
}
else
{
retValue = drt.add(o);
if(retValue)
nrFiiDrt++;
}
}
}
return retValue;
}
public boolean findElement(String o)
{
boolean retValue = false;
if(data.equals(o))
return true;
if(data.compareTo(o) > 0)
{
if(stg != null)
retValue = stg.findElement(o);
}
else
{
if(drt != null)
retValue = drt.findElement(o);
}
return retValue;
}
public String txtInorder()
{
String retValue = "";
if(stg != null)
retValue += stg.txtInorder();
if(data != null)
retValue += "," + data;
if(drt != null)
retValue += drt.txtInorder();
return retValue;
}
public String txtPreorder()
{
String retValue = "";
if(data != null)
retValue += "," + data;
if(stg != null)
retValue += stg.txtPreorder();
if(drt != null)
retValue += drt.txtPreorder();
return retValue;
}
public String txtPostorder()
{
String retValue = "";
if(stg != null)
retValue += stg.txtPostorder();
if(drt != null)
retValue += drt.txtPostorder();
if(data != null)
retValue += "," + data;
return retValue;
}
public void setNumChildsNull()
{
nrFiiStg = -1;
nrFiiDrt = -1;
if(stg != null)
stg.setNumChildsNull();
if(drt != null)
drt.setNumChildsNull();
}
public void adjustNumChilds()
{
if(nrFiiStg == -1)
{
if(stg == null)
nrFiiStg = 0;
else
{
stg.adjustNumChilds();
nrFiiStg = stg.nrFiiStg + stg.nrFiiDrt + 1;
}
}
if(nrFiiDrt == -1)
{
if(drt == null)
nrFiiDrt = 0;
else
{
drt.adjustNumChilds();
nrFiiDrt = drt.nrFiiStg + drt.nrFiiDrt + 1;
}
}
}
public void inorder(Graphics g, int x, int y)
{
g.drawString("root", x - 12, y);
g.drawLine(x, y + 2, x, y + 16);
g.drawLine(x, y + 16, x - 4, y + 16 - 4);
g.drawLine(x, y + 16, x + 4, y + 16 - 4);
inorder(g, x, y + 16, false);
}
public void inorder(Graphics g, int x, int y, boolean t)
{
int strlatime, xn, yn;
String tmpStr = data.substring(0, (data.length() < maxLetters ? data.length() : maxLetters));
g.drawRoundRect(x - latime / 2, y, latime, inaltime, rounded, rounded);
g.drawLine(x - latime / 2, y + inaltime - ptrline, x + latime / 2, y + inaltime - ptrline);
g.drawLine(x, y + inaltime - ptrline, x, y + inaltime);
FontMetrics fm = g.getFontMetrics();
strlatime = fm.stringWidth(tmpStr);
g.drawString(tmpStr, x - strlatime / 2, y + inaltime - ptrline - strAscent / 2);
if(stg == null)
{
g.drawLine(x - latime / 2, y + inaltime - ptrline, x, y + inaltime);
g.drawLine(x, y + inaltime - ptrline, x - latime / 2 + 1, y + inaltime - 1);
}
else
{
xn = x - latime * (stg.nrFiiDrt + 1) - arrow;
yn = y + inaltime + ptrline;
g.drawLine(x - latime / 4, y + inaltime - ptrline / 2, xn, yn);
stg.inorder(g, xn, yn, false);
}
if(drt == null)
{
g.drawLine(x + latime / 2, y + inaltime - ptrline, x, y + inaltime);
g.drawLine(x, y + inaltime - ptrline, x + latime / 2 - 1, y + inaltime - 1);
}
else
{
xn = x + latime * (drt.nrFiiStg + 1) + arrow;
yn = y + inaltime + ptrline;
g.drawLine(x + latime / 4, y + inaltime - ptrline / 2, xn, yn);
drt.inorder(g, xn, yn, false);
}
}
public int getMinx(int x)
{
int xn;
if(stg != null)
xn = x - latime * (nrFiiStg + 1) - arrow;
else
xn = x - latime;
return Math.min(x, xn);
}
public int getMaxx(int x)
{
int xn;
if(drt != null)
xn = x + latime * (nrFiiDrt + 1) + arrow;
else
xn = x + latime;
return Math.max(x, xn);
}
public int getMaxy(int y)
{
int ystg = y, ydrt = y, yn;
if(stg != null)
{
yn = y + inaltime + ptrline;
ystg = stg.getMaxy(yn);
}
if(drt != null)
{
yn = y + inaltime + ptrline;
ydrt = drt.getMaxy(yn);
}
return Math.max(Math.max(ydrt, ystg), y);
}
public int getinaltime()
{
return inaltime;
}
}
Niciun comentariu:
Trimiteți un comentariu