1. 程式人生 > >層次序創建二叉樹(圖形界面和控制臺輸入實現)

層次序創建二叉樹(圖形界面和控制臺輸入實現)

ride repaint 註冊 read public ria span performed ttext

  1 2018.11.7
  2 XT
  3 
  4 /**
  5  * 功能:構造二叉樹
  6  * 說明:
  7  * 1.主函數輸入模式有兩種,BT參數 true 圖形界面,false  控制臺輸入
  8  * 2.構造樹是按層次遍歷結果輸入的 如:ABCDE*F**GH
  9  */
 10 
 11 import javax.swing.*;
 12 import java.awt.*;
 13 import java.awt.event.ActionEvent;
 14 import java.awt.event.ActionListener;
 15 import java.io.BufferedReader;
16 import java.io.IOException; 17 import java.io.InputStreamReader; 18 19 public class BT extends JFrame implements ActionListener { 20 private BufferedReader br=null; 21 private MyPanel myPanel; 22 private JTextField jtf; 23 private JButton jb1, jb2; 24 private JLabel jl;
25 26 public BT(boolean isGUIMode) { 27 if (isGUIMode) { 28 this.setLayout(null); //自定義布局 29 jtf = new JTextField(""); 30 jtf.setFont(new Font("宋體", Font.BOLD, 16));//Arial 31 jtf.setColumns(40); 32 jb1 = new JButton("確定"); 33
jb2 = new JButton("重置"); 34 jb1.setFont(new Font("宋體",Font.PLAIN,16)); 35 jb2.setFont(new Font("宋體",Font.PLAIN,16));//設置按鈕的字體 36 jl = new JLabel("輸入"); 37 jl.setFont(new Font("華文行楷",Font.PLAIN,20));//設置標簽的字體樣式 38 39 jl.setBounds(35, 20, 50, 50); //如果設置了絕對布局,那麽要通過setBounds()來設置絕對位置與絕對大小 40 jtf.setBounds(80, 30, 350, 30); 41 jb1.setBounds(120, 100, 100, 25); 42 jb2.setBounds(270, 100, 100, 25); 43 this.add(jl); 44 this.add(jtf); 45 this.add(jb1); 46 this.add(jb2); 47 48 myPanel = new MyPanel(); 49 Thread t = new Thread(myPanel); 50 t.start();//啟動線程 51 myPanel.setBounds(0, 150, 3000, 200);//如果沒擋住了的話是不會調用paint方法的 52 this.add(myPanel); 53 this.setTitle("BuildTree"); 54 this.setVisible(true); 55 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 56 this.setSize(500, 380); 57 this.setLocation(500, 200); 58 59 jb1.addActionListener(this);//註冊監聽 60 jb2.addActionListener(this); 61 62 } else { 63 consoleInput(); 64 } 65 } 66 67 private void consoleInput() { 68 br = new BufferedReader(new InputStreamReader(System.in)); 69 System.out.print("Input:"); 70 try { 71 String input = br.readLine(); 72 while (!input.equals("q")) { 73 char[] nodes = input.toCharArray(); 74 TreeNode treeNode = create(nodes, 0); 75 System.out.println("前序:" + displayPreOrder(treeNode)); 76 System.out.println("中序:" + displayInOrder(treeNode)); 77 System.out.println("後序:" + displayPostOrder(treeNode)); 78 System.out.print("Input:"); 79 input = br.readLine(); 80 } 81 } catch (IOException e) { 82 try { 83 br.close(); 84 } catch (IOException e1) { 85 e1.printStackTrace(); 86 } 87 e.printStackTrace(); 88 } 89 } 90 91 private TreeNode create(char[] arr, int index) { 92 if (index >= arr.length) // 可以不需要,但是所有的值必須要寫滿,任一個#都要寫,不然會越界 93 return null; 94 else if (String.valueOf(arr[index]).equals("#")||String.valueOf(arr[index]).equals("*")) { 95 return null; 96 } else { 97 TreeNode node = new TreeNode(arr[index]); 98 node.leftChild = create(arr, 2 * index + 1); 99 node.rightChild = create(arr, 2 * index + 2); 100 return node; 101 } 102 } 103 104 private static String displayInOrder(TreeNode treeNode) { 105 //中序 106 if (treeNode != null) { 107 return displayInOrder(treeNode.leftChild) + (treeNode.data == ‘*‘ ? "" : treeNode.data) + 108 displayInOrder(treeNode.rightChild); 109 } 110 return ""; 111 } 112 113 private static String displayPreOrder(TreeNode treeNode) { 114 //前序 115 if (treeNode != null) { 116 return (treeNode.data == ‘*‘ ? "" : treeNode.data) + displayPreOrder(treeNode.leftChild) + displayPreOrder(treeNode.rightChild); 117 } 118 return ""; 119 } 120 121 private static String displayPostOrder(TreeNode treeNode) { 122 //中序 123 if (treeNode != null) { 124 return displayPostOrder(treeNode.leftChild) + displayPostOrder(treeNode.rightChild) + (treeNode.data == ‘*‘ ? "" : treeNode.data); 125 } 126 return ""; 127 } 128 129 @Override 130 public void actionPerformed(ActionEvent e) { 131 if (e.getSource() == jb1) { 132 String s = jtf.getText(); 133 if (s.matches("[A-Za-z]([A-Za-z]|\\*|#)*")) { //註意:是w就可以了,不要用/w,就代表字母下劃線 134 myPanel.display(s.toCharArray()); 135 } else { 136 JOptionPane.showMessageDialog(null, "輸入錯誤!\n請重試!","ERROR",JOptionPane.ERROR_MESSAGE); 137 jtf.setText(""); 138 } 139 } 140 else if (e.getSource() == jb2) { 141 jtf.setText(""); 142 } 143 } 144 145 public static void main(String[] args) { 146 new BT(true); 147 } 148 149 class MyPanel extends JPanel implements Runnable { 150 String[] strings; 151 152 MyPanel() { 153 strings = new String[]{"", "", ""}; 154 // this.setSize(100, 1000);//看來絕對布局裏的setBounds()方法設置的大小具有更高優先級,可以覆蓋這條設置 155 this.setBackground(new Color(125, 134, 234)); 156 } 157 158 public void display(char[] chars) { 159 TreeNode treeNode = create(chars, 0); 160 strings[0] = displayPreOrder(treeNode); 161 strings[1] = displayInOrder(treeNode); 162 strings[2] = displayPostOrder(treeNode); 163 } 164 165 @Override 166 public void paint(Graphics g) { 167 super.paint(g);// 168 g.setFont(new Font("宋體", Font.BOLD, 24)); 169 g.drawString("前序: " + strings[0], 10, 50); 170 g.drawString("中序: " + strings[1], 10, 100); 171 g.drawString("後序: " + strings[2], 10, 150); 172 } 173 174 @Override 175 public void run() { 176 while (true) { 177 try { 178 Thread.sleep(500); 179 repaint(); 180 } catch (InterruptedException e) { 181 e.printStackTrace(); 182 } 183 } 184 } 185 } 186 187 class TreeNode { 188 Character data; 189 TreeNode leftChild; 190 TreeNode rightChild; 191 //含數據,二叉鏈表 192 TreeNode(char data) { 193 this.data = data; 194 leftChild = null; 195 rightChild = null; 196 } 197 } 198 }

層次序創建二叉樹(圖形界面和控制臺輸入實現)