⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 binarytreeframe.java

📁 一个可以以图形方式直观表示的树状二叉树算法程序,可以实现生成和遍历.
💻 JAVA
字号:
package binarytreetravel;import java.awt.*;import java.util.Observer;import java.util.LinkedList;import java.awt.event.*;import javax.swing.*;import com.borland.jbcl.layout.*;/** * <p>Title: </p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2005</p> * <p>Company: </p> * @author not attributable * @version 1.0 */public class BinaryTreeFrame extends JFrame{  JPanel contentPane;  JSplitPane jSplitPane1 = new JSplitPane();  JPanel jPanel1 = new JPanel();  JLabel jLabel1 = new JLabel();  JTextField jTextField1 = new JTextField();  ButtonGroup bg = new ButtonGroup();  JRadioButton jRadioButton1 = new JRadioButton();  JRadioButton jRadioButton2 = new JRadioButton();  JButton jButton1 = new JButton();  JScrollPane jScrollPane1 = new JScrollPane();  JButton jButton2 = new JButton();  JRadioButton jRadioButton3 = new JRadioButton();  JRadioButton jRadioButton4 = new JRadioButton();  JRadioButton jRadioButton5 = new JRadioButton();  BTreeView btView = new BTreeView();  BorderLayout borderLayout1 = new BorderLayout();  GridLayout gridLayout1 = new GridLayout();  //Construct the frame  public BinaryTreeFrame() {    enableEvents(AWTEvent.WINDOW_EVENT_MASK);    try {      jbInit();    }    catch(Exception e) {      e.printStackTrace();    }  }  //Component initialization  private void jbInit() throws Exception  {    contentPane = (JPanel) this.getContentPane();    this.setResizable(true);    this.setSize(400, 300);    this.setTitle("Binary Tree Travelling");    jLabel1.setText("输入树的节点:");    jRadioButton1.setSelected(true);    jRadioButton1.setText("左节点");    jRadioButton2.setText("右节点");    jSplitPane1.setBottomComponent(btView);    jSplitPane1.setLastDividerLocation(400);    jPanel1.setLayout(gridLayout1);    jButton1.setText("确定");    jButton1.addActionListener(new BinaryTreeFrame_jButton1_actionAdapter(this));    jTextField1.setText("");    jButton2.setText("确定");    jButton2.addActionListener(new BinaryTreeFrame_jButton2_actionAdapter(this));    jRadioButton3.setSelected(true);    ButtonGroup bg2 = new ButtonGroup();    jRadioButton3.setText("先序遍历");    jRadioButton4.setText("中序遍历");    jRadioButton5.setText("后序遍历");    jScrollPane1.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);    jScrollPane1.setAutoscrolls(true);    contentPane.setLayout(borderLayout1);    contentPane.setMinimumSize(new Dimension(120, 260));    jPanel1.setBackground(SystemColor.control);    gridLayout1.setColumns(1);    gridLayout1.setRows(10);    bg2.add(jRadioButton3);    bg2.add(jRadioButton4);    bg2.add(jRadioButton5);    contentPane.add(jSplitPane1,  BorderLayout.CENTER);    jSplitPane1.add(jPanel1, JSplitPane.LEFT);    jPanel1.add(jLabel1, null);    jPanel1.add(jTextField1, null);    jPanel1.add(jRadioButton1, null);    jPanel1.add(jRadioButton2, null);    jPanel1.add(jButton1, null);    jPanel1.add(jRadioButton3, null);    jPanel1.add(jRadioButton4, null);    jPanel1.add(jRadioButton5, null);    jPanel1.add(jButton2, null);    jSplitPane1.add(jScrollPane1, JSplitPane.RIGHT);    jScrollPane1.getViewport().add(btView, null);    bg.add(jRadioButton1);    bg.add(jRadioButton2);    jSplitPane1.setDividerLocation(100);  }  //Overridden so we can exit when window is closed  protected void processWindowEvent(WindowEvent e) {    super.processWindowEvent(e);    if (e.getID() == WindowEvent.WINDOW_CLOSING) {      System.exit(0);    }  }  void jButton1_actionPerformed(ActionEvent e) {    if(btView.choiceNode == null) return;    BTreeNode current = new BTreeNode(jTextField1.getText());    btView.choiceNode.addObserver((Observer)btView);    if(jRadioButton1.isSelected())      btView.choiceNode.insertLChild(current);    else if(jRadioButton2.isSelected())      btView.choiceNode.insertRChild(current);  }  void jButton2_actionPerformed(ActionEvent e) {    btView.delay = null;    btView.repaint();    LinkedList list = new LinkedList();    if(jRadioButton3.isSelected())      list = btView.blTree.preOrder(null, 1);    else if(jRadioButton4.isSelected())      list = btView.blTree.inOrder(null, 1);    else if(jRadioButton5.isSelected())      list = btView.blTree.postOrder(null, 1);    //btView.repaint();    btView.showOrderView(list);  }}class BinaryTreeFrame_jButton1_actionAdapter implements java.awt.event.ActionListener {  BinaryTreeFrame adaptee;  BinaryTreeFrame_jButton1_actionAdapter(BinaryTreeFrame adaptee) {    this.adaptee = adaptee;  }  public void actionPerformed(ActionEvent e) {    adaptee.jButton1_actionPerformed(e);  }}class BinaryTreeFrame_jButton2_actionAdapter implements java.awt.event.ActionListener {  BinaryTreeFrame adaptee;  BinaryTreeFrame_jButton2_actionAdapter(BinaryTreeFrame adaptee) {    this.adaptee = adaptee;  }  public void actionPerformed(ActionEvent e) {    adaptee.jButton2_actionPerformed(e);  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -