Results 1 to 6 of 6
- 10-13-2009, 07:43 AM #1
Member
- Join Date
- Oct 2009
- Posts
- 3
- Rep Power
- 0
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.
- 10-13-2009, 08:03 AM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Well, let's see what you have.
- 10-13-2009, 12:53 PM #3
Member
- Join Date
- Oct 2009
- Posts
- 3
- Rep Power
- 0
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!" );
}
- 10-13-2009, 01:02 PM #4
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Please use code tags next time when posting code.
So does it compile? Does it run?
Does it work?
- 10-13-2009, 01:16 PM #5
Member
- Join Date
- Oct 2009
- Posts
- 3
- Rep Power
- 0
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...
- 10-13-2009, 01:35 PM #6
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Similar Threads
-
Binary Search Tree implementation
By Ceasar in forum Advanced JavaReplies: 5Last Post: 10-13-2009, 03:43 PM -
Implementing binary search tree
By javanovice in forum New To JavaReplies: 0Last Post: 08-12-2009, 05:14 AM -
Binary Search Tree
By Goo in forum New To JavaReplies: 0Last Post: 03-06-2009, 04:46 PM -
Binary Search Tree
By michael_mke in forum New To JavaReplies: 3Last Post: 12-04-2008, 02:03 AM -
Binary Search Tree Traversal
By dch414 in forum New To JavaReplies: 2Last Post: 11-07-2008, 12:01 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks