I have a splay tree i want to print to a text area. It is printing in console, but i have decided to add a GUI i want to print the tree to a textArea.

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

currently my printTree has void return type as shown above.

How can i modify my code to be able to print all the tree values and keys to a TextArea. I know the setText() takes a string type, but in this case that would not work(i think), how can can i ensure that the print method outputs all values to the text area?

有帮助吗?

解决方案

Here is the code you need. Note that your no-arg printTree() method now returns a String which you can use to print the tree elsewhere.

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

其他提示

You can use the following code. Implementing toString() allows you to forget about any additional printing of string which you get from printTree(). You will be able just print you object.
And by the way if "Value" and "Key" are implementing toString() then no need to call it, you can just use them in a string with operator "+".

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;
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top