Results 1 to 6 of 6
  1. #1
    harvin23 is offline Member
    Join Date
    Oct 2009
    Posts
    3
    Rep Power
    0

    Default Create code for Binary Search Tree using compareTo method using I/O

    Hi guys/Girls..
    i have been trying this question for the past 3 weeks using java SE...
    i tried very hard but could get the output...
    do try to help me out...


    Question:

    Write a program for a bag of strings, where the strings are stored in binary search tree. In creating the binary search tree, you should use the string’s compareTo method.

    Once the tree is built, perform preorder, inorder, and postorder traversal.

    Note: The strings should be read from a text file that contains sentences. Create your own text file and limit your sentences to 5.

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Well, let's see what you have.

  3. #3
    harvin23 is offline Member
    Join Date
    Oct 2009
    Posts
    3
    Rep Power
    0

    Default this is wat i done but i cant compile and insert i/o

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;

    public class BinarySearchTree {

    public BinarySearchTree( ) {
    root = null;
    }

    public void insert( Comparable x ) {
    root = insert( x, root );
    }


    public void remove( Comparable x ) {
    root = remove( x, root );
    }


    public void removeMin( ) {
    root = removeMin( root );
    }


    public Comparable findMin( ) {
    return elementAt( findMin( root ) );
    }


    public Comparable findMax( ) {
    return elementAt( findMax( root ) );
    }


    public Comparable find( Comparable x ) {
    return elementAt( find( x, root ) );
    }

    public void makeEmpty( ) {
    root = null;
    }

    public boolean isEmpty( ) {
    return root == null;
    }


    private Comparable elementAt( BinaryNode t ) {
    return t == null ? null : t.element;
    }


    protected BinaryNode insert( Comparable x, BinaryNode t ) {
    if( t == null )
    t = new BinaryNode( x );
    else if( x.compareTo( t.element ) < 0 )
    t.left = insert( x, t.left );
    else if( x.compareTo( t.element ) > 0 )
    t.right = insert( x, t.right );
    else
    ;
    return t;
    }
    }


    protected BinaryNode remove( Comparable x, BinaryNode t ) {
    if( t == null )
    throw new ItemNotFoundException( x.toString( ) );
    if( x.compareTo( t.element ) < 0 )
    t.left = remove( x, t.left );
    else if( x.compareTo( t.element ) > 0 )
    t.right = remove( x, t.right );
    else if( t.left != null && t.right != null ) // Two children
    {
    t.element = findMin( t.right ).element;
    t.right = removeMin( t.right );
    } else
    t = ( t.left != null ) ? t.left : t.right;
    return t;
    }


    protected BinaryNode removeMin( BinaryNode t ) {
    if( t == null )
    throw new ItemNotFoundException( );
    else if( t.left != null ) {
    t.left = removeMin( t.left );
    return t;
    } else
    return t.right;
    }


    protected BinaryNode findMin( BinaryNode t ) {
    if( t != null )
    while( t.left != null )
    t = t.left;

    return t;
    }


    private BinaryNode findMax( BinaryNode t ) {
    if( t != null )
    while( t.right != null )
    t = t.right;

    return t;
    }


    private BinaryNode find( Comparable x, BinaryNode t ) {
    while( t != null ) {
    if( x.compareTo( t.element ) < 0 )
    t = t.left;
    else if( x.compareTo( t.element ) > 0 )
    t = t.right;
    else
    return t;
    }

    return null;
    }


    protected BinaryNode root;



    public static void main( String [ ] args ) {
    BinarySearchTree t = new BinarySearchTree( );
    final int NUMS = 4000;
    final int GAP = 37;

    System.out.println( "Checking... (no more output means success)" );

    for( int i = GAP; i != 0; i = ( i + GAP ) % NUMS )
    t.insert( new Integer( i ) );

    for( int i = 1; i < NUMS; i+= 2 )
    t.remove( new Integer( i ) );

    if( ((Integer)(t.findMin( ))).intValue( ) != 2 ||
    ((Integer)(t.findMax( ))).intValue( ) != NUMS - 2 )
    System.out.println( "FindMin or FindMax error!" );

    for( int i = 2; i < NUMS; i+=2 )
    if( ((Integer)(t.find( new Integer( i ) ))).intValue( ) != i )
    System.out.println( "Find error1!" );

    for( int i = 1; i < NUMS; i+=2 ) {
    if( t.find( new Integer( i ) ) != null )
    System.out.println( "Find error2!" );
    }

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Please use code tags next time when posting code.
    So does it compile? Does it run?
    Does it work?

  5. #5
    harvin23 is offline Member
    Join Date
    Oct 2009
    Posts
    3
    Rep Power
    0

    Default i cant get it running

    sorry for not using code tags... im need... this is wat is can find... i cant compile... im not even sure if its the right thing...

  6. #6
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Read the compilation errors that come up.
    They are giving you hints on what the problems are. They will also teach you a lot about Java.
    If you encounter one that you don't understand or can't fix then post it here together with the line reported for it.

Similar Threads

  1. Binary Search Tree implementation
    By Ceasar in forum Advanced Java
    Replies: 5
    Last Post: 10-13-2009, 03:43 PM
  2. Implementing binary search tree
    By javanovice in forum New To Java
    Replies: 0
    Last Post: 08-12-2009, 05:14 AM
  3. Binary Search Tree
    By Goo in forum New To Java
    Replies: 0
    Last Post: 03-06-2009, 04:46 PM
  4. Binary Search Tree
    By michael_mke in forum New To Java
    Replies: 3
    Last Post: 12-04-2008, 02:03 AM
  5. Binary Search Tree Traversal
    By dch414 in forum New To Java
    Replies: 2
    Last Post: 11-07-2008, 12:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •