【问题标题】:Print BST to a text area in java将BST打印到java中的文本区域
【发布时间】:2013-11-18 13:08:12
【问题描述】:

我有一个展开树,我想打印到文本区域。它正在控制台中打印,但我决定添加一个我想将树打印到 textArea 的 GUI。

public class Bst<Key extends Comparable<Key>, Value>  {
    private Node root;   

    private class Node {
        private Key phone_number;            
        private Value contact_name;        
        private Node left, right;   
        public Node(Key phone_number, Value contact_name) {
            this.phone_number   = phone_number;
            this.contact_name = contact_name;
        }
    }
    public boolean contains(Key phone_number) {
        return (get(phone_number) != null);
    }

    // return contact_name associated with the given phone_number

    public Value get(Key phone_number) {
        root = splay(root, phone_number);
        int cmp = phone_number.compareTo(root.phone_number);
        if (cmp == 0)
            return root.contact_name;
        else
            return null;
    } 


    public void printTree( )
    {
        if( isEmpty( ) )
            System.out.println( "Empty tree" );
        else
            printTree( root );           
    }

    private void printTree( Node t )
    {
        if ( t.left != null ) 
        {
            System.out.println( "Phone Number:" + t.phone_number.toString( ) + "  Contact Name : " + t.contact_name.toString( ) );
            printTree( t.left );
        } 

        if (t.right != null)
        {
                printTree( t.right );
                System.out.println( "name:" + t.phone_number.toString( ) + "  Number : " + t.contact_name.toString( )  );
        }
    }
 }

目前我的 printTree 有 void 返回类型,如上所示。

如何修改我的代码,以便能够将所有树值和键打印到 TextArea。我知道 setText() 采用字符串类型,但在这种情况下它不起作用(我认为),我怎样才能确保 print 方法将所有值输出到文本区域?

【问题讨论】:

  • 至少发布完整代码。
  • 我会传入StringBuilder,只要有System.out 的地方就调用append
  • 这似乎是实现访问者模式的好时机。
  • 感谢@Danny 的提示。

标签: java binary-search-tree settext


【解决方案1】:

这是您需要的代码。请注意,您的无参数 printTree() 方法现在返回一个字符串,您可以使用它在其他地方打印树。

public class Bst<Key extends Comparable<Key>, Value>  {
    private Node root;   

    private class Node {
        private Key phone_number;            
        private Value contact_name;        
        private Node left, right;   
        public Node(Key phone_number, Value contact_name) {
            this.phone_number   = phone_number;
            this.contact_name = contact_name;
        }
    }
    public boolean contains(Key phone_number) {
        return (get(phone_number) != null);
    }

    // return contact_name associated with the given phone_number

    public Value get(Key phone_number) {
        root = splay(root, phone_number);
        int cmp = phone_number.compareTo(root.phone_number);
        if (cmp == 0)
            return root.contact_name;
        else
            return null;
    } 


    public String printTree( )
    {
        if( isEmpty( ) )
            return "Empty tree";
        else
        {
            StringBuilder sb = new StringBuilder();
            printTree( root, sb );
            return sb.toString();
        }
    }

    private void printTree( Node t, StringBuilder sb )
    {
        if ( t.left != null ) 
        {
            sb.append( "Phone Number:" + t.phone_number.toString( ) + "  Contact Name : " + t.contact_name.toString( ) );
            printTree( t.left, sb );
        } 

        if (t.right != null)
        {
                printTree( t.right, sb );
                sb.append( "name:" + t.phone_number.toString( ) + "  Number : " + t.contact_name.toString( )  );
        }
    }
 }

【讨论】:

    【解决方案2】:

    您可以使用以下代码。实现 toString() 可以让您忘记从 printTree() 获得的任何额外的字符串打印。您将能够打印您的对象。
    顺便说一句,如果 "Value" 和 "Key" 正在实现 toString() 则无需调用它,您可以在带有运算符 "+" 的字符串中使用它们。

    public String toString() 
    {
        return printTree();
    }
    
    public String printTree( )
    {
        String str;
        if( isEmpty( ) )
            str = "Empty tree";
        else
            str += printTree( root );
    }
    
    private String printTree( Node t )
    {
        String str = "";
        if ( t.left != null ) 
        {
            str += "Phone Number:" + t.phone_number + "  Contact Name : " + t.contact_name + "\n";
            str += printTree( t.left );
        } 
        if (t.right != null)
        {
            str += printTree( t.right );
            str += "name:" + t.phone_number + "  Number : " + t.contact_name + "\n";
        }
        return str;
    }
    

    【讨论】:

      猜你喜欢
      • 2011-11-14
      • 1970-01-01
      • 2022-07-23
      • 2018-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-05
      相关资源
      最近更新 更多