?
Solved

polymorphic binary search tree using singleton design pattern

Posted on 2009-04-03
2
Medium Priority
?
1,980 Views
Last Modified: 2012-05-06
I'm implementing a polymorphic binary search tree and I'm having a problem with my insert method. The code only inserts items into the right side of the tree. I have to implement the method recursively without a helper method.
public NonEmptyTree<K, V> insert(K key, V value) {
int result = key.compareTo(this.key);
		if (result == 0) {
			--size;
			this.val= value;
			return this;
		}
		if (result > 0) { 
			right = right.insert(key, value);  
		} else if (result < 0) {
			left = left.insert(key, value);
		}
		return this;

Open in new window

0
Comment
Question by:dalydude
  • 2
2 Comments
 
LVL 16

Expert Comment

by:imladris
ID: 24060672
That method looks fine. Certainly, it appears to clearly distinguish between a result of 0, less than 0 and greater than zero, and the corresponding insertion here, on the right or the left.

So, if your tree is always inserting on the right, my guess would be that the flaw lies in the compareTo method always returning a result greater than 0.....
0
 
LVL 16

Accepted Solution

by:
imladris earned 2000 total points
ID: 24098701
Did that comment help?

If so, it is now time to close and grade the question.

If not, perhaps a clarifying question would help.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When there is a disconnect between the intentions of their creator and the recipient, when algorithms go awry, they can have disastrous consequences.
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
This video teaches viewers about errors in exception handling.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month15 days, 4 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question