今天星期二,参加培训的第二十五天,一如往常,早起,到教室看书,最近在二叉树,还有图,看的挺没有心情的,就是学不下去,也无所谓了,老师今天好像是只讲了swing,嗯,今天,加班到现在,太晚了,贴点代码,回去睡觉了。
package am;
import java.util.Iterator;
public interface Tree <E extends Comparable<E>> {
public boolean search(E a);
public boolean insert(E a);
public boolean delete(E a );
public void inorder();
public void postorder();
public void preorder();
public int getSize();
public boolean isEmpty();
public Iterator iterator();
}
package am;
import java.util.Iterator;
public abstract class AbstractTree<E extends Comparable<E>> implements Tree<E>{
public void inorder() {}
public void postorder() {}
public void preorder() {}
public boolean isEmpty() {
return getSize()==0;
}
public Iterator iterator() {
return null;
}
}
package am;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class BinaryTree<E extends Comparable<E>> extends AbstractTree<E> {
protected TreeNode<E> root;
protected int size=0;
public BinaryTree() {
// TODO Auto-generated constructor stub
}
public BinaryTree(E[] objects) {
for(int i = 0;i<objects.length;i++) {
insert(objects[i]);
}
}
@Override
public boolean search(E a) {
// TODO Auto-generated method stub
TreeNode<E> current = root;
while(current!=null) {
if(a.compareTo(current.element)<0) {
current=current.left;
} else if(a.compareTo(current.element)>0) {
current=current.right;
} else {
return true;
}
}
return false;
}
@Override
public boolean insert(E a) {
// TODO Auto-generated method stub
if(root==null) {
root=createNewNode(a);
} else {
TreeNode<E> parent = null;
TreeNode<E> current = root;
while (current!=null) {
if(a.compareTo(current.element)<0) {
parent = current;
current = current.left;
} else if(a.compareTo(current.element)>0) {
parent = current;
current = current.right;
} else {
return false;
}
}
if(a.compareTo(parent.element)<0) {
parent.left=createNewNode(a);
} else {
parent.right=createNewNode(a);
}
}
size++;
return true;
}
protected TreeNode<E> createNewNode(E e){
return new TreeNode<E> (e);
}
public void inorder() {
inorder(root);
}
protected void inorder(TreeNode<E> root) {
if(root==null) return ;
inorder(root.left);
System.out.println(root.element);
inorder(root.right);
}
public void postorder() {
postorder(root);
}
protected void postorder(TreeNode<E> root) {
if(root==null) {
return ;
}
postorder(root.left);
postorder(root.right);
System.out.println(root.element);
}
public void preorder() {
preorder(root);
}
protected void preorder(TreeNode<E> root) {
if(root==null) {
return;
}
System.out.println(root.element);
preorder(root.left);
preorder(root.right);
}
@Override
public int getSize() {
// TODO Auto-generated method stub
return size;
}
public TreeNode<E> getRoot() {
return root;
}
public ArrayList<TreeNode<E>> path(E a){
ArrayList<TreeNode<E>> list = new ArrayList<TreeNode<E>>();
TreeNode<E> current=root;
while(current!=null) {
list.add(current);
if(a.compareTo(current.element)<0) {
current=current.left;
} else if (a.compareTo(current.element)>0){
current=current.right;
}
else {
break;
}
}
return list;
}
@Override
public boolean delete(E a) {
// TODO Auto-generated method stub
TreeNode<E> parent = null;
TreeNode<E> current = root;
while(current!=null) {
if(a.compareTo(current.element)<0) {
parent=current;
current=current.left;
} else if(a.compareTo(current.element)>0) {
parent = current;
current= current.right;
} else {
break;
}
}
if(current==null) {
return false;
}
if(current.left==null) {
if(parent==null) {
root = current.right;
}
else {
if(a.compareTo(parent.element)<0) {
parent.left=current.right;
} else {
parent.right=current.right;
}
}
} else {
TreeNode<E> parentOfRightMost = current;
TreeNode<E> rightMost = current.left;
while (rightMost.right!=null) {
parentOfRightMost=rightMost;
rightMost = rightMost.right;
}
current.element = rightMost.element;
if(parentOfRightMost.right==rightMost) {
parentOfRightMost.right=rightMost.left;
} else {
parentOfRightMost.left=rightMost.left;
}
}
size--;
return true;
}
public Iterator iterator() {
return inorderIterator();
}
public Iterator inorderIterator() {
return new InorderIterator();
}
class InorderIterator implements Iterator{
private ArrayList<E> list = new ArrayList<E>();
private int current = 0;
public InorderIterator() {
// TODO Auto-generated constructor stub
inorder();
}
private void inorder() {
inorder(root);
}
private void inorder(TreeNode<E> root) {
if(root == null) {
return ;
}
inorder(root.left);
list.add(root.element);
inorder(root.right);
}
public boolean hasNext() {
if(current<list.size()) {
return true;
}
return false;
}
@Override
public Object next() {
// TODO Auto-generated method stub
return list.get(current++);
}
public void remove() {
delete(list.get(current));
list.clear();
inorder();
}
public void clear() {
root = null;
size=0;
}
}
}
package am;
public class TreeNode<E> {
E element;
TreeNode<E> left;
TreeNode<E> right;
public TreeNode(E e) {
// TODO Auto-generated constructor stub
element = e;
}
}
package am;
import javax.swing.JApplet;
import javax.swing.JFrame;
public class DisplayBinaryTree extends JApplet {
public DisplayBinaryTree() {
add(new TreeControl(new BinaryTree<Integer>()));
}
public static void main(String[] args) {
DisplayBinaryTree applet = new DisplayBinaryTree();
JFrame frame = new JFrame("");
frame.setSize(400,300);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(applet);
}
}
package am;
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class TreeControl extends JPanel {
private BinaryTree<Integer> tree;
private JTextField jtfKey = new JTextField(5);
private TreeView View = new TreeView();
private JButton jbtInsert = new JButton("Insert");
private JButton jbtDelete = new JButton("Delete");
public TreeControl(BinaryTree<Integer> tree) {
// TODO Auto-generated constructor stub
this.tree = tree;
setUI();
}
private void setUI() {
this.setLayout(new BorderLayout());
add(View,BorderLayout.CENTER);
JPanel panel = new JPanel();
panel.add(new JLabel("Enter a key"));
panel.add(jtfKey);
panel.add(jbtInsert);
panel.add(jbtDelete);
add(panel, BorderLayout.SOUTH);
jbtInsert.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
int key = Integer.parseInt(jtfKey.getText());
if(tree.search(key)) {
JOptionPane.showMessageDialog(null, key+" is already in the tree");
}
else {
tree.insert(key);
View.repaint();
}
}
});
jbtDelete.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
int key = Integer.parseInt(jtfKey.getText());
if(!tree.search(key)) {
JOptionPane.showMessageDialog(null, key+" is not in the tree");
}
else {
tree.delete(key);
View.repaint();
}
}
});
}
class TreeView extends JPanel{
private int radius = 10;
private int vGap = 20;
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if(tree.getRoot()!=null) {
displayTree(g,tree.getRoot(),getWidth()/2,30,60);
}
}
private void displayTree(Graphics g,TreeNode<Integer> root,int x,int y,int hGap) {
g.drawOval(x-radius, y-radius, 2*radius, 2*radius);
g.drawString(root.element+"", x-6, y+4);
if(root.left!=null) {
connectLeftChild(g,x-hGap,y+vGap,x,y);
displayTree(g, root.left, x-hGap, y+vGap, hGap/2);
}
if(root.right!=null) {
connectRightChild(g,x+hGap,y+vGap,x,y);
displayTree(g, root.right, 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*(x1-x2)/d);
int y11 = (int)(y1-radius*vGap/d);
int x12 =(int)(x2-radius*(x1-x2)/d);
int y12= (int)(y2+radius*vGap/d);
g.drawLine(x11, y11, x12, y12);
}
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 x12 =(int)(x2+radius*(x1-x2)/d);
int y12= (int)(y2+radius*vGap/d);
g.drawLine(x11, y11, x12, y12);
}
}
}
反正最近就是快放假了,很是烦燥,行吧,别的不说了,的确有点晚了,回去睡觉了,就这样,结束。