Results 1 to 7 of 7
- 04-27-2010, 09:02 PM #1
Member
- Join Date
- Apr 2010
- Posts
- 4
- Rep Power
- 0
Generic Binary Search Tree, Allowing Only Comparable Elements
Hi, everyone. I'm new to generics, and I can't figure out exactly how to do this. I need to implement a binary search tree. This is easy enough to do if I only allow it to contain integers, but I want it to accept any object that implements Comparable. So normally, the class definition would look something like this:
I want to make it a BinarySearchTree that can, and must, be assigned to a particular Comparable type. Something like this:Java Code:public class BinarySearchTree { ... }
I know that this isn't the correct way to do it, though. How can I do this?Java Code:public class BinarySearchTree<Comparable> { ... }
- 04-27-2010, 09:05 PM #2
Senior Member
- Join Date
- Mar 2010
- Posts
- 266
- Rep Power
- 4
Java Code:public class BinarySearchTree<T extends Comparable> { ... }
- 04-27-2010, 09:28 PM #3
Member
- Join Date
- Apr 2010
- Posts
- 4
- Rep Power
- 0
I found things looking like this in searching for the answer to this problem. I must admit that I'm confused by it, though. From what I understand, extends causes the subclass to inherit all methods and variables from the superclass. Comparable is an interface, though, so doesn't that mean that anything that extends Comparable is also an interface? I'm sure that I'm missing something here, but my line of thinking is that if T extends Comparable, then T is an interface, but I want to pass a class, such as Integer, when I create a new BinarySearchTree. Something in my understanding of one or more of these ideas must be incorrect.
- 04-27-2010, 10:00 PM #4
Senior Member
- Join Date
- Mar 2010
- Posts
- 266
- Rep Power
- 4
actually, when applied to interfaces, extends behaves a bit different:
in my code snippet,Java Code:interface A { ... } interface B extends A { ... }
T extends Comparable
means you'll be able to use any type as long as its a sub-type of Comparable. Since Integer implements Comparable, you can use Integer.
- 04-27-2010, 10:20 PM #5
Member
- Join Date
- Apr 2010
- Posts
- 4
- Rep Power
- 0
Ah, well now I think I understand it better. Thankfully, my code will now compile, but I do get a warning when I do this:
The warning is this:Java Code:public class BinarySearchTree<T extends Comparable> { ... }
Comparable is a raw type. References to generic type Comparable<T> should be parameterized.
I fixed this by using BinarySearchTree<T extends Comparable<T>> instead. Will this produce the results that I'm looking for?
- 04-27-2010, 11:29 PM #6
Senior Member
- Join Date
- Mar 2010
- Posts
- 266
- Rep Power
- 4
I think so... Your general approach is correct, the syntax technicalities of Java generics though are not for mere mortals... I mean take a look here:
Enum (Java 2 Platform SE 5.0)
public abstract class Enum<E extends Enum<E>>
!!!
- 04-28-2010, 12:49 AM #7
Member
- Join Date
- Apr 2010
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
Data Structures(Binary Search Tree to AVL Tree)ASAP pls
By jfAdik in forum Forum LobbyReplies: 0Last Post: 04-04-2010, 07:40 AM -
Binary search tree search method
By chopo1980 in forum New To JavaReplies: 2Last Post: 12-10-2009, 01:42 AM -
Binary Search Tree
By anmadie in forum New To JavaReplies: 5Last Post: 11-17-2009, 02:39 AM -
Binary Search Tree
By Goo in forum New To JavaReplies: 0Last Post: 03-06-2009, 04:46 PM -
Binary Search Tree
By michael_mke in forum New To JavaReplies: 3Last Post: 12-04-2008, 02:03 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks