[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

polymorphic binary search tree using singleton design pattern

Posted on 2009-04-03
2
Medium Priority
?
1,836 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

649 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