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

📄 tree.java

📁 本软件是使用java 开发的
💻 JAVA
字号:
package datastructure;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @author unascribed
 * @version 1.0
 */

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class Tree
    extends Panel
    implements Runnable, ActionListener {
  private Thread runner;
  private int groupSize;
  private TreePersonGroup thePersonGroup;
  private boolean runFlag = false;

  private int GPNumber;
  private boolean isNumber;

  static JButton newButton;
  static JButton runButton;
  static JButton stepButton;

  static JButton stayButton;
  // private int delay; //用来控制速度
  //控制演示速度
  private GlobalControls controlsPanel = new GlobalControls();
  private int controls;

  CodeAnimationPanel codePanel;
  Panel panel;
  Panel eastPanel;
  JTextField textfield11;
  JTextField textfield12;
  JTextField textfield21;
  JTextField textfield22;
  public Tree() {
  }

  public void init() {
    this.setBackground(Color.lightGray);

    String as[] = {
        "status preOrder(BiTree T,Status(*Visit)(TElemType e)",
        " {",
        "   InitStack(S);Push(S,T);",
        "   while(!StackEmpty(S)&&p)",
        "    {",
        "       while(GetTop(S,p)&&p)",
        "       {  if(!visit(p->data)) return Error;",
        "          Push(S,p->lchild);}",
        "       pop(S,p);",
        "       if(!StackEmpty(S)",
        "         { pop(S,p);",
        "           push(S,p->rchild); }",
        "   }",
        " return ok;}"};

    codePanel = new CodeAnimationPanel(as);
    this.setLayout(new BorderLayout());
    controlsPanel.delay = 900;

    runFlag = false;
    eastPanel = new Panel();
    eastPanel.setLayout(new BorderLayout());
    panel = new Panel();
    panel.setLayout(new GridLayout(2, 2));

    eastPanel.add(codePanel, BorderLayout.NORTH);
    eastPanel.add(panel, BorderLayout.SOUTH);
    this.add(eastPanel, BorderLayout.EAST);

    textfield11 = new JTextField("节点个数");
    textfield11.setEditable(false);
    textfield12 = new JTextField(" ");
    textfield12.setEditable(false);
    textfield21 = new JTextField("S栈中元素");
    textfield21.setEditable(false);
    textfield22 = new JTextField("");
    textfield22.setEditable(false);
    panel.add(textfield11);
    panel.add(textfield12);
    panel.add(textfield21);
    panel.add(textfield22);
    newButton = new JButton("新 建");
    newButton.addActionListener(this);
    runButton = new JButton("运 行");
    runButton.addActionListener(this);
    stepButton = new JButton("单 步");
    stepButton.addActionListener(this);
    stayButton = new JButton("暂 停");
    stayButton.addActionListener(this);
    thePersonGroup = new TreePersonGroup(codePanel);
    thePersonGroup.doFill(20);
    textfield12.setText("  20");
  }

  public void start() {
    if (runner == null) {
      runner = new Thread(this);
      runner.start();
    }
  }

  public void stop() {
    if (runner != null) {
      runner = null;
    }
  }

  public void paint(Graphics g) {
    thePersonGroup.draw(g);
  }

  public void update(Graphics g) {
    paint(g);
  }

  public void actionPerformed(ActionEvent actionevent) {
    isNumber = true;
    thePersonGroup.setDrawAll(false);
    if (actionevent.getSource() == newButton) {
      stop();
      textfield22.setText("");
      GPNumber = (int) (Math.random() * 31); //节点个数在1到31之间随机生成
      textfield12.setText(String.valueOf(GPNumber));
      thePersonGroup.fill(isNumber, GPNumber);
      textfield12.setText("" + GPNumber);
      thePersonGroup.setBegin(false); //设置doneFlag为false可以重新开始运行
      stepButton.setEnabled(true);
      runButton.setEnabled(true);
      stayButton.setEnabled(true);

    }
    if (actionevent.getSource() == stepButton) {
      thePersonGroup.traverse();
      runFlag = false;
      thePersonGroup.setDrawMode(0);
      dataupdate();
      if (thePersonGroup.isOver()) {
        runFlag = false;
        JOptionPane.showConfirmDialog(this,
                                      "    算法演示完成!\n  \n 单击相关按钮重新开始\n",
                                      "演示完成", JOptionPane.DEFAULT_OPTION);

        newButton.setEnabled(true);
        runButton.setEnabled(false);
        stepButton.setEnabled(false);
        stayButton.setEnabled(false);

        this.stop();
      }
    }
    if (actionevent.getSource() == runButton) {
      start();
      runFlag = true;

    }
    if (actionevent.getSource() == stayButton) {
      runFlag = false;

    }

    repaint();
  }

  public void dataupdate() { //更新显示栈中的数据
    String stackdata = new String();
    int a[] = thePersonGroup.getStack();

    for (int i = 0; i <= thePersonGroup.getTop(); i++)
      stackdata += a[i] + ",";
    textfield22.setText(stackdata);
  }

  public void run() {
    while (Thread.currentThread() == this.runner) {

      if (runFlag) {

        if (thePersonGroup.isOver()) { //判断是否运行结束
          runFlag = false;

          JOptionPane.showConfirmDialog(this,
                                        "    算法演示完成!\n  \n 单击相关按钮重新开始\n",
                                        "演示完成", JOptionPane.DEFAULT_OPTION);

          newButton.setEnabled(true);
          runButton.setEnabled(false);
          stepButton.setEnabled(false);
          stayButton.setEnabled(false);
          runFlag = false;
          this.stop();
        }
        thePersonGroup.setDrawMode(0);
        thePersonGroup.traverse();
        dataupdate();
        repaint();
        controls = controlsPanel.delay;
        try {
          runner.sleep(controls);
        }
        catch (InterruptedException exp) {
          exp.toString();
        }
      }
    }
  }
}

⌨️ 快捷键说明

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