My teacher told us that there is a default class in Java for binary tree. I found this BinaryTree class and I tried to declare a BinaryTree object, but it didn’t work.
How can I declare a BinaryTree using this class and which library should I include?
I wrote something like this:
private BinaryTree<Integer> c=new BinaryTree<Integer>();
but it says that BinaryTree cannot be resolved to a type.
Well that class is certainly not the “default java one”. I guess your teacher might be talking about
TreeSetwhich is based on a form of self-balancing binary tree.I’m not sure how old the class the link you give is but it’s not using generics so it’s probably Java 1.4-. Also, it’s using the default package – which is a bit of a Java no-no, suggesting it might not be production ready. The constructor expects a
Comparatorwhich can compare the instances that the tree will store. For example if you want to store Integers in order, you could declare a binary tree as:The code above declares an anonymous class which implements the
Comparatorinterface. Alternatively you can use a regular class to do the same thing:Where you have the code..