Solved

polymorphic binary search tree using singleton design pattern

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Unexpected HttpURLConnection connection behavior 2 84
STDEVP in SQL 2 53
eclipse formatting 6 70
Re-position the objects 7 95
Iteration: Iteration is repetition of a process. A student who goes to school repeats the process of going to school everyday until graduation. We go to grocery store at least once or twice a month to buy products. We repeat this process every mont…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

930 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now