LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 02-09-2004, 09:07 PM   #1
ksgill
Senior Member
 
Registered: Apr 2003
Location: Toronto, Canada
Distribution: Ubuntu Jaunty (9.04)
Posts: 1,044

Rep: Reputation: 45
Binary search tree insertion in java


This method adds a comparable object to a binary tree. Can anyone tell me what I am doing wrong in this code:
<code>
public void add (Comparable x) throws DuplicateException
{
int diff;
BST node= this;
while (node.root != null)
{
if ((diff = x.compareTo (root)) < 0)
node= node.left;

else if (diff > 0)
node= node.right;

else
throw new DuplicateException ("Duplicate value " + x);
}
node.root= x;
node.left= new BST();
node.right= new BST();
size++;
}
</code>

torch: ~/2110/a3$ java BST ape bat cat
ape
false


while correct output should be:
ape bat cat
false

thanks in advance
 
Old 02-10-2004, 05:09 AM   #2
synna
Member
 
Registered: Jan 2004
Posts: 40

Rep: Reputation: 15
Quote:
node.root= x;
node.left= new BST();
node.right= new BST();
size++;
}
here is your fault; each time you do it (without test).

A remark : never do this : BST node =this; ///This is just confusing for nothing as you can use this instead so this.root, this.left...

Last edited by synna; 02-10-2004 at 05:13 AM.
 
Old 02-10-2004, 08:36 AM   #3
ksgill
Senior Member
 
Registered: Apr 2003
Location: Toronto, Canada
Distribution: Ubuntu Jaunty (9.04)
Posts: 1,044

Original Poster
Rep: Reputation: 45
I tried putting a test on it ==>

if (node.root == null) {
root= x;
node.left= new BST();
node.right= new BST();
}
it still doesnt work. What kind of a test should i put on there? Thanks for your response
 
Old 02-10-2004, 12:38 PM   #4
synna
Member
 
Registered: Jan 2004
Posts: 40

Rep: Reputation: 15
Normaly, for this kind of things you use recursivity and a more object oriented approach.

Anyway, I missed something, sorry.
the problem is that you have written x.compareTo(root) and not x.compareTo(node.root)
 
Old 02-10-2004, 08:26 PM   #5
ksgill
Senior Member
 
Registered: Apr 2003
Location: Toronto, Canada
Distribution: Ubuntu Jaunty (9.04)
Posts: 1,044

Original Poster
Rep: Reputation: 45
We were asked to convert a recursive method into a non-recursive one. I did what you said and it still doesnt help. It is object oriented... just a different way of solving the same problem. I am still not getting correct output and also,

node.root= x;
node.left= new BST();
node.right= new BST();
size++;

has to be done each time without test because thats where you actually add the comparable. If you note closely, you dont add comparable anywhere else.

if ((diff = x.compareTo (root)) < 0)

is used to determine which side of tree we have to add the object in- and those four lines (above) actually add comparable x at the right position. Got any other ideas
 
Old 02-11-2004, 05:34 AM   #6
synna
Member
 
Registered: Jan 2004
Posts: 40

Rep: Reputation: 15
You want to know which side of the node not of the tree (I mean side of the subtree starting at node)
so it is x.compareTo(node.root)
 
Old 02-12-2004, 05:11 PM   #7
ksgill
Senior Member
 
Registered: Apr 2003
Location: Toronto, Canada
Distribution: Ubuntu Jaunty (9.04)
Posts: 1,044

Original Poster
Rep: Reputation: 45
Turns out there was nothing wrong with my code to start with. It was the toString() method that was acting up. Also, I have to increment the size inside the while loop too. By side of tree i meant the subtree.. thanks anyways
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
representation of binary tree using array sajith Programming 3 10-06-2005 10:59 PM
java tree class true_atlantis Programming 1 08-30-2005 12:03 PM
ascii pine tree: JAVA randomx Programming 1 10-09-2003 06:35 PM
Printing a binary tree in c? JMC Programming 5 09-26-2003 11:02 AM
need *fast* algorithm for binary file search Tinkster Programming 6 12-05-2002 06:25 PM


All times are GMT -5. The time now is 11:19 AM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration