Java 使用单间距字体显示树(如记事本中)
目前我有一个生成树的程序。我过去常常得到树的字符串输出。为此,我稍微修改了链接中的函数:我没有使用Java 使用单间距字体显示树(如记事本中),java,string,text,tree,monospace,Java,String,Text,Tree,Monospace,目前我有一个生成树的程序。我过去常常得到树的字符串输出。为此,我稍微修改了链接中的函数:我没有使用System.out.println而是重新编写代码以满足我的要求: public String getStringRepresentation(String prefix, boolean end, int position) { String retval = prefix + (position != -1? (end ? "└──" : "├──") + " " +
System.out.println
而是重新编写代码以满足我的要求:
public String getStringRepresentation(String prefix, boolean end, int position)
{
String retval = prefix + (position != -1? (end ? "└──" : "├──") + " " + position +(branches==null? "[" + content + "]" : ""):"") + "\r\n";
if(branches != null)
{
for (int i = 0; i < branches.size() - 1; i++) {
retval += branches.get(i).getStringRepresentation(prefix + (end ? " " : "│ "), false,i);
}
if (branches.size() >= 1) {
retval += branches.get(branches.size() - 1).getStringRepresentation(prefix + (end ?" " : "│ "), true,branches.size() - 1);
}
}
return retval;
}
我将显示以下字符串:
JTextArea textarea = new JTextArea(infoMessage);
textarea.setFont(new Font("monospaced", Font.PLAIN, 14));
JOptionPane.showMessageDialog(null, textarea);
看起来是这样的:
(如您所见,对齐方式与实际不符。)我发现了问题。显然,单间距字体不合适。如果我使用“consolas”(我的记事本使用的一个),所有内容都会正确显示。下面是打印二叉树的java代码,如下所示:
1
/ \
/ \
2 \
/ \ 3
4 5 / \
9 \
8
/ \
6 7
代码:
package com.dsalgo;
导入java.util.ArrayList;
导入java.util.List;
导入java.util.TreeMap;
公共类二进制TreeNiceprint{
公共静态void main(字符串[]args){
节点a=新节点(1);
节点b=新节点(2);
节点c=新节点(3);
节点d=新节点(4);
节点e=新节点(5);
节点f=新节点(8);
节点g=新节点(6);
节点h=新节点(7);
节点i=新节点(9);
a、 左=b;
a、 右=c;
b、 左=d;
b、 右=e;
c、 左=i;
c、 右=f;
f、 左=g;
f、 右=h;
印刷品(a);
}
公共静态打印(节点根){
Listresult=getStrings((getWidth(root)+1)/2,0,root);
TreeMap>行=新TreeMap<>();
用于(Strings点:结果){
if(lines.get(s.y)!=null){
行。获取(s.y.)。添加(s);
}否则{
列表l=new ArrayList<>();
l、 添加(s);
行。放置(s.y,l);
}
}
对于(列表l:lines.values()){
系统输出打印LN(展平(l));
}
}
私有静态字符串展平(列表l){
int x=0;
StringBuilder sb=新的StringBuilder();
用于(点s:l){
sb.append(新字符串(新字符[s.x-x])。替换('\0','');
某人附加(价值);
x=某人的长度();
}
使某人返回字符串();
}
私有静态int getWidth(节点根){
整数宽度=0;
if(root.left!=null){
宽度+=getWidth(根,左);
}
if(root.right!=null){
宽度+=getWidth(根,右);
}
宽度+=(“”+root.value).length();
返回宽度;
}
私有静态列表getStrings(intx,inty,节点根){
Listresult=new ArrayList();
结果.add(新的StringPoint(x-(“”+root.value).length()/2,y,“”)
+根值);
if(root.left!=null){
int width=getWidth(root.left);
int i=0;
对于(;i<(宽度+1)/2;++i)
结果.添加(新的StringPoint(x-i-1,y+i+1,“/”));
addAll(getStrings(x-i-1,y+i+1,root.left));
}
if(root.right!=null){
int width=getWidth(root.right);
int i=0;
对于(;i<(宽度+1)/2;++i)
结果.添加(新的StringPoint(x+i+1,y+i+1,“\\”));
addAll(getStrings(x+i+1,y+i+1,root.right));
}
返回结果;
}
静态类StringPoint{
整数x;
整数y;
字符串值;
StringPoint(整数x,整数y,字符串值){
这个.x=x;
这个。y=y;
这个值=值;
}
@凌驾
公共字符串toString(){
返回“(“+x+”、“+y+”、“+value+”)”;
}
}
静态类节点{
左淋巴结;
节点权;
int值;
公共节点(int值){
这个值=值;
}
}
}
我们是否应该对你编写的代码进行格式化,并猜测输出的样子和它应该是什么样子?考虑显示我们的代码,最好是A。请显示所需的输出和当前输出。对不起,我更新了帖子,看起来代码使用的几乎是├──代码>但输出为├───代码>那么额外的─代码>来自哪里?这真的和你在这里展示的代码一样吗?@KevinPanko:我已经检查了输出,只有两个”─". 事实上,我放在这里的代码是从图像中的输出复制的。
1
/ \
/ \
2 \
/ \ 3
4 5 / \
9 \
8
/ \
6 7
package com.dsalgo;
import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;
public class BinaryTreeNicePrint {
public static void main(String[] args) {
Node a = new Node(1);
Node b = new Node(2);
Node c = new Node(3);
Node d = new Node(4);
Node e = new Node(5);
Node f = new Node(8);
Node g = new Node(6);
Node h = new Node(7);
Node i = new Node(9);
a.left = b;
a.right = c;
b.left = d;
b.right = e;
c.left = i;
c.right = f;
f.left = g;
f.right = h;
nicePrint(a);
}
public static void nicePrint(Node root) {
List< StringPoint > result = getStrings((getWidth(root) + 1) / 2, 0, root);
TreeMap< Integer, List< StringPoint > > lines = new TreeMap< >();
for (StringPoint s : result) {
if (lines.get(s.y) != null) {
lines.get(s.y).add(s);
} else {
List< StringPoint > l = new ArrayList< >();
l.add(s);
lines.put(s.y, l);
}
}
for (List< StringPoint > l : lines.values()) {
System.out.println(flatten(l));
}
}
private static String flatten(List< StringPoint > l) {
int x = 0;
StringBuilder sb = new StringBuilder();
for (StringPoint s : l) {
sb.append(new String(new char[s.x - x]).replace('\0', ' '));
sb.append(s.value);
x = sb.length();
}
return sb.toString();
}
private static int getWidth(Node root) {
int width = 0;
if (root.left != null) {
width += getWidth(root.left);
}
if (root.right != null) {
width += getWidth(root.right);
}
width += ("" + root.value).length();
return width;
}
private static List< StringPoint > getStrings(int x, int y, Node root) {
List< StringPoint > result = new ArrayList< StringPoint >();
result.add(new StringPoint(x - ("" + root.value).length() / 2, y, ""
+ root.value));
if (root.left != null) {
int width = getWidth(root.left);
int i = 0;
for (; i < (width + 1) / 2; ++i)
result.add(new StringPoint(x - i - 1, y + i + 1, "/"));
result.addAll(getStrings(x - i - 1, y + i + 1, root.left));
}
if (root.right != null) {
int width = getWidth(root.right);
int i = 0;
for (; i < (width + 1) / 2; ++i)
result.add(new StringPoint(x + i + 1, y + i + 1, "\\"));
result.addAll(getStrings(x + i + 1, y + i + 1, root.right));
}
return result;
}
static class StringPoint {
Integer x;
Integer y;
String value;
StringPoint(int x, int y, String value) {
this.x = x;
this.y = y;
this.value = value;
}
@Override
public String toString() {
return "(" + x + "," + y + "," + value + ")";
}
}
static class Node {
Node left;
Node right;
int value;
public Node(int value) {
this.value = value;
}
}
}