Array to binary tree and hashtable

Hey,

Is it possible to pass the elements of an array to a binary tree and to a hashtable ??

i mean the array has the array.add method which passes the new string to it
is there anything similar to it for the binary tree/ hashtable ??
or is it possible to write the elements of the array to the binary tree and to the hashtable ??


Thanks in advance !


//BINARY TREE

public class BinaryTreeString {

    TreeNode root = null;    
    int numItems = 0;        

    public void add (String data)
    {

        if (root == null) {
            root = new TreeNode ();
            root.data = data;
            numItems ++;
            return;
        }
       

        if ( contains (data) ) {

            return;
        }
       

        recursiveInsert (root, data);
       
        numItems ++;
    }

   
    void recursiveInsert (TreeNode node, String data)
    {

        if ( data.compareTo (node.data) < 0 ) {

            if (node.left != null) {
                recursiveInsert (node.left, data);
            }
            else {
                node.left = new TreeNode ();
                node.left.data = data;
            }
           
        }

        else {

            if (node.right != null) {
                recursiveInsert (node.right, data);
            }
            else {
                node.right = new TreeNode ();
                node.right.data = data;
            }
        }
       
    }
   



import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;
 
import java.util.*;
 
public class ReadStringsWithArrayList
{
    public static void main( String [ ] args )
    {
        ArrayList array = getStrings( );
        for( int i = 0; i < array.size( ); i++ )
            System.out.println( array.get( i ) );
        
        HashMap readHashmap = new HashMap();
        
    }
 
    // Read an unlimited number of String; return an ArrayList
    public static ArrayList getStrings( )
    {
        BufferedReader in = new BufferedReader( new InputStreamReader( System.in ) );
        ArrayList array = new ArrayList( );
        String oneLine;
 
        System.out.println( "Enter any number of strings, one per line; " );
        System.out.println( "Terminate with empty line: " );
 
        try
        {
            while( ( oneLine = in.readLine( ) ) != null && !oneLine.equals( "" ) )
                array.add( oneLine );
 
        }
        catch( IOException e )
        {
            System.out.println( "Unexpected IO Exception has shortened amount read" );
        }
        
        System.out.println( "Done reading" );
        return array;
    }
}

Open in new window

perdoname_Asked:
Who is Participating?
 
objectsConnect With a Mentor Commented:
yes thats possible, you can add elements to as many different collections as you want.
As far as adding an array, just write a utility method that loops thru the array adding each string from  to the required collections
0
 
perdoname_Author Commented:
actually the instruction says "...uses binary search tree for sorting . "
so its a heap sort then ??
0
 
Ajay-SinghCommented:
binary search tree is always sorted!
0
All Courses

From novice to tech pro — start learning today.