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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

objectsCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.