【问题标题】:Display a tree with monospaced font (as in notepad)用等宽字体显示树(如在记事本中)
【发布时间】:2014-01-11 14:07:03
【问题描述】:

目前我有一个生成树的程序。我使用https://stackoverflow.com/a/8948691 来获取树的字符串输出。为此,我稍微更改了链接中的函数:我没有使用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;   
    }

一个示例文本输出将是

├── 0
│   ├── 0[F]
│   └── 1
│       ├── 0[V]
│       └── 1[A]
└── 1
    ├── 0[D]
    └── 1[S]

这个字符串我显示如下:

JTextArea textarea = new JTextArea(infoMessage);
        textarea.setFont(new Font("monospaced", Font.PLAIN, 14));
        JOptionPane.showMessageDialog(null, textarea);

看起来像这样:

(如您所见,对齐方式不应该如此。)

【问题讨论】:

  • 我们是否应该猜测您编写的代码,并猜测输出的外观和外观?
  • 考虑向我们展示代码,最好是minimal complete valid example。请显示所需的输出和当前输出。
  • 对不起,我更新了帖子
  • 看起来代码几乎使用了├──,但输出是├───,那么额外的 是从哪里来的?这真的与您在此处显示的代码相同吗?
  • @KevinPanko:我检查了输出,只有两个“─”。其实我这里放的代码就是从图片中的输出复制过来的。

标签: java string text tree monospace


【解决方案1】:

我发现了问题。显然等宽字体不是正确的字体。如果我使用“consolas”(我的记事本使用的那个),一切都会正确显示。

【讨论】:

    【解决方案2】:

    下面是打印二叉树的 java 代码,如下所示:

         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;
      }
     }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-10-16
      • 2013-03-06
      • 1970-01-01
      • 2020-12-17
      • 2014-10-10
      • 1970-01-01
      • 2021-01-20
      • 2022-01-15
      • 1970-01-01
      相关资源
      最近更新 更多