Search code examples
javabinary-search-treesettext

Print BST to a text area in java


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?


Solution

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