+ 1

Comparable

How to implement the comparable interface in java in a binary search tree when the the class is generic class in java

17th Nov 2019, 12:33 AM
Safina Nganga
Safina Nganga - avatar
1 Answer
+ 2
If you have a class BinaryTree<T extends Comparable<T>>, you could then have BinaryTree<T extends Comparable<T>> extends Comparable<BinaryTree<T>>. How this works is not too difficult. BinaryTree<T extends Comparable<T>> ensures the type held by the tree is comparable (if you can't compare the type held, how would you compare the entire tree?). Extending Comparable<BinaryTree<T>> ensures that the class can only be compared with an instance of the class which holds the same type, T.
17th Nov 2019, 5:36 AM
LunarCoffee
LunarCoffee - avatar