Solved

polymorphic binary search tree using singleton design pattern

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

763 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

12 Experts available now in Live!

Get 1:1 Help Now