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, "输入错误!
请重试!","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 }