java界面化二叉排序树_层次序创建二叉树(图形界面和控制台输入实现)

1 2018.11.7

2 XT3

4 /**

5 * 功能:构造二叉树6 * 说明:7 * 1.主函数输入模式有两种,BT参数 true 图形界面,false 控制台输入8 * 2.构造树是按层次遍历结果输入的 如:ABCDE*F**GH9 */

10

11 import javax.swing.*;12 import java.awt.*;13 importjava.awt.event.ActionEvent;14 importjava.awt.event.ActionListener;15 importjava.io.BufferedReader;16 importjava.io.IOException;17 importjava.io.InputStreamReader;18

19 public class BT extends JFrame implementsActionListener {20 private BufferedReader br=null;21 privateMyPanel myPanel;22 privateJTextField jtf;23 privateJButton jb1, jb2;24 privateJLabel jl;25

26 public BT(booleanisGUIMode) {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 = newMyPanel();49 Thread t = newThread(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 voidconsoleInput() {68 br = new BufferedReader(newInputStreamReader(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, intindex) {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 = newTreeNode(arr[index]);98 node.leftChild = create(arr, 2 * index + 1);99 node.rightChild = create(arr, 2 * index + 2);100 returnnode;101 }102 }103

104 private staticString 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 staticString 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 staticString 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 @Override130 public voidactionPerformed(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 voidmain(String[] args) {146 new BT(true);147 }148

149 class MyPanel extends JPanel implementsRunnable {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 @Override166 public voidpaint(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 @Override175 public voidrun() {176 while (true) {177 try{178 Thread.sleep(500);179 repaint();180 } catch(InterruptedException e) {181 e.printStackTrace();182 }183 }184 }185 }186

187 classTreeNode {188 Character data;189 TreeNode leftChild;190 TreeNode rightChild;191 //含数据,二叉链表

192 TreeNode(chardata) {193 this.data =data;194 leftChild = null;195 rightChild = null;196 }197 }198 }

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/393311.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

web开发环境_Web开发人员的开发环境

web开发环境With all the tools and programs available, it can be challenging to figure out the best way to set up your development environment on your computer.使用所有可用的工具和程序,寻找在计算机上设置开发环境的最佳方法可能是一项挑战。 In this…

使用.net Stopwatch class 来分析你的代码

当我们在调试,优化我们的代码的时候,想知道某段代码的真正的执行时间,或者我们怀疑某段代码,或是某几段代码执行比较慢, 需要得到具体的某段代码的具体执行时间的时候。有一个很好用的类Stopwatch。 Stopwatch 类在 Sy…

Docker 部署 postgresql 与 pgadmin4

Docker快速部署PostgreSQL服务 快速开始 请新建一个目录postgresql,进入目录postgresql,将以下文件保存为docker-compose.yml,然后执行docker-compose up version: 3 services:mydb:image: postgres:11volumes:- db-data:/var/lib/postgresql…

leetcode151. 翻转字符串里的单词

给定一个字符串&#xff0c;逐个翻转字符串中的每个单词。 示例 1&#xff1a; 输入: “the sky is blue” 输出: “blue is sky the” 代码 class Solution {public String reverseWords(String s) {int ns.length(),i0;ArrayList<String> arrayListnew ArrayList<…

java衍生作用_java-如何从AffineTransform衍生的形状对象中“...

您可以使用AffineTransform.transform(Point2D, Point2D)变换多边形上的单个点.如果您不使用旋转变换来移动船,而是将船的位置保持在一个(x,y)位置,那么事情就简单得多.您可以在move()中移动飞船的位置,而不是尝试平移多边形.然后,当您想给船上油漆时,例如做&#xff1a;// Opt…

初学者设计数据库_面向初学者的完整数据库设计课程

初学者设计数据库This database design course will give you a deeper grasp of database design. Caleb Curry teaches the equivalent of an entire college course during this eight hour video.本数据库设计课程将使您更深入地了解数据库设计。 在这8个小时的视频中&…

Qt QTcpSocket使用总结

socket的连接是异步的&#xff0c;所以必须等连接建立完成才能使用&#xff0c;所以分别加入waitForConnected()和waitForBytesWritten()后调试通过1&#xff09;只有使用waitForConnected()后,QTcpSocket才真正尝试连接服务器&#xff0c;并返回是否连接的结果2&#xff09;避…

[bzoj1303][CQOI2009]中位数图

来自FallDream的博客&#xff0c;未经允许&#xff0c;请勿转载&#xff0c;谢谢。 给定一个n个数排列&#xff0c;求有多少段长度为奇数的区间&#xff0c;中位数是b. n<100000 时间限制0.1s 我一开始没看到排列&#xff0c;想着怎么还能O(n)做的啊&#xff1f;&#xff1f…

falsk 请求没有返回值报错

线上报警 5xx 错误&#xff0c;查看日志发现报这个错&#xff0c; TypeError: The view function did not return a valid response. The function either returned None or ended without a return statement. 这个方法没有有效的返回结果 页面报这个错误 Internal Server Err…

java的iterator接口_java Iterator接口和LIstIterator接口分析_java_脚本之家

java Iterator接口和LIstIterator接口分析目录1.Iterator接口2.ListIterator3.Iterator和ListIterator的区别正文在继续看ArrayList源码之前&#xff0c;先了解Iterator接口和ListIterator接口&#xff0c;下篇文章详细讲解ArrayList是如何实现它们的。我们知道&#xff0c;接…

leetcode468. 验证IP地址

编写一个函数来验证输入的字符串是否是有效的 IPv4 或 IPv6 地址。 IPv4 地址由十进制数和点来表示&#xff0c;每个地址包含4个十进制数&#xff0c;其范围为 0 - 255&#xff0c; 用(".")分割。比如&#xff0c;172.16.254.1&#xff1b; 同时&#xff0c;IPv4 地…

播客#45:迪伦·以色列

On todays episode, I interview Dylan Israel. Dylan is a software engineer, a YouTuber, and the creator of several programming courses.在今天的一集中&#xff0c;我采访了迪伦以色列。 迪伦(Dylan)是一位软件工程师&#xff0c;一名YouTuber&#xff0c;并且是几门编…

PHPstorm快捷键

当前文件搜索类&#xff1a;ctrln 全文检索文件&#xff1a;ctrlshiftn 收起侧边栏&#xff1a;alt1&#xff08;自定义&#xff09; 个人学习记录&#xff0c;持续更新中。。。 转载于:https://www.cnblogs.com/zaijiang/p/7806260.html

JS基础_强制类型转换-Number

https://www.cnblogs.com/ZHOUVIP/p/7225267.html转载于:https://www.cnblogs.com/robinunix/p/11011188.html

JavaScript变量和作用域

JavaScript有两种变量&#xff0c;全局变量和局部变量 如果在任何函数定义之外声明了一个变量&#xff0c;则该变量是全局变量&#xff0c;且该变量的值在整个持续范围内都可以访问和修改 如果在函数定义内声明来了一个变量&#xff0c;则该变量为局部变量。每次执行该函数时都…

python 微信bot_使用Python创建Twitter Bot

python 微信botHave you ever wantd to create a Twitter bot? In this tutorial John G. Fisher shows how to create a bot with Python that tweets images or status updates at a set interval.您是否曾经想创建一个Twitter机器人&#xff1f; 在本教程中&#xff0c;约翰…

leetcode1487. 保证文件名唯一

给你一个长度为 n 的字符串数组 names 。你将会在文件系统中创建 n 个文件夹&#xff1a;在第 i 分钟&#xff0c;新建名为 names[i] 的文件夹。 由于两个文件 不能 共享相同的文件名&#xff0c;因此如果新建文件夹使用的文件名已经被占用&#xff0c;系统会以 (k) 的形式为新…

提高redis cluster集群的安全性,增加密码验证

节点设置密码 1、修改配置文件 在配置文件里面增加密码选项&#xff0c;一定要加上masterauth&#xff0c;不然Redirected的时候会失败。 masterauth redispassword requirepass redispassword 修改后需要重启redis节点。 2、动态修改 连接redis节点进行配置设置&#xff0c;然…

java索引ref_java – 如何使用jgit库将git HEAD指向特定的ref?

我想以编程方式更新HEAD而不对非裸仓库执行checkout或rebase.我希望工作树和索引在操作后保持不变.编辑我需要更新HEAD的符号目标,而不是HEAD当前目标的提交ID.这更像是一个结账,而不是其他任何东西,除了我不能使用org.eclipse.jgit.api.CheckoutCommand因为它需要我更新路径,但…

【codeforces 103E】 Buying Sets

http://codeforces.com/problemset/problem/103/E (题目链接) 题意 给出$n$个数&#xff0c;每个数与一个集合相关联。从其中选出最小的若干个数&#xff0c;选出的数的个数与这些数相关联的集合的并集大小相等。 Solution 因为保证了有完全匹配&#xff0c;所以跑出一个完全匹…