I have just spent hours and hours implementing binary trees and linked list queues and stacks. Now the last part I have to implement a user interface to test these methods. The methods I have to test are inorder, poster, preorder traversals of a tree.
My problem is I have set up my tree with the insert method using an Object as the item. Now I have to insert the object as a string into my tree. But I keep getting an error that the string can not be inserted as the tree is expect an object.
Can any one point me in the right direction?
Wes Hughes
Ranch Hand
Joined: Jul 29, 2002
Posts: 31
posted
0
You're gonna need to explain it a little better or post some code cause from my understanding you have written a method which expects an Object as argument and you are passing in a String (which extends Object), therefore you shouldn't have any problem.
But I keep getting an error that the string can not be inserted as the tree is expect an object.
A string is subclass of java.lang.Object. Wherever an Object is expected a String is welcomed.
Either you have a custom class called Object like org.omg.CORBA.Object or something you observed is not right.
Tom Blough
Ranch Hand
Joined: Jul 31, 2003
Posts: 263
posted
0
Sounds like you have set your insert method up correctly, but your tree implementation is backwards. You want you tree implementation to work with Objects as well, and not Strings. You can put a String into a tree expecting objects, but you cannot put an arbritary object into a tree expecting Strings unless the object is a String, or a subclass of String.
Post your code and we'll give you some advice on what sounds like a homework problem.
Cheers,
Tom Blough<br /> <blockquote><font size="1" face="Verdana, Arial">quote:</font><hr>Cum catapultae proscriptae erunt tum soli proscripti catapultas habebunt.<hr></blockquote>
andy car
Greenhorn
Joined: Nov 10, 2005
Posts: 7
posted
0
Hey guys,
How did you know this was a homework assignment? Must be the basic nature of the question. Your advice of adding the toString did help some but ofcourse I come across another error. The toString made sence shortly after asking this question.
The new problem now is in the user interface I am trying to put together, how do I put this. In previous programs I always had to input "int"'s as input and the only other kind was adding strings objects to a lexicon of words. In that case we used
to read the inputted string that we are adding. The below code is the method I used to read the inputted int's. What would I use to read the similar input but has string as inputs.
BTNode newNode = new BTNode(); String line = null;
System.out.println ("Please enter text (type DONE to quit):");
line = ac.readLine(); while ((line != ("DONE")) || line != ("done") ) { newNode.insert(line); line = ac.readLine(); } }
this is the insert method I am using, does not have to be a search tree so no need to compare strings.
// inserts a an object into our tree. public void insert(Object obj){
((BTNode) current).setElement(root); if (isEmpty()) { addRoot(obj); } else
while( !isExternal(current) ) {
if( !hasLeft(current) ) //if no left node insert left { insertLeft(current, obj); } else if( right(current) == null ) //if no right node insert right { insertRight(current, obj); } else if( hasLeft(current) ) //if left node exsists set current node to the left { ((BTNode ) current).setElement(left); } else if ( hasRight(current) ) //if right node exsists set current node to the right { ((BTNode ) current).setElement(right); } } }
Tom Blough
Ranch Hand
Joined: Jul 31, 2003
Posts: 263
posted
0
Where's your BT node Class. The error message is telling you that the BTNode class does not have an insert method that takes a single String argument.
Also, did you check to see what the toSTring method returned for your object? What you inserted in your list by using toString is probably not what you wanted.
Cheers,
Jeff Albertson
Ranch Hand
Joined: Sep 16, 2005
Posts: 1780
posted
0
This is not your problem, but I can't help noticing you're making a common Java beginner's mistake -- if you want to compare two strings for equality, don't use operators == or !=, instead use the equals method: