Solved

polymorphic binary search tree using singleton design pattern

Posted on 2009-04-03
2
1,512 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 500 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
even odd program using while loop 3 60
Problem to picture file 20 65
ejb stateless example 2 25
Java 8 to Java 6 8 21
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The viewer will learn how to implement Singleton Design Pattern in Java.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

828 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