Just out of curiosity, I recently had to use a tree for one of my programs, i had to build a binary tree myself, but why does the Collections API does not have a default implementation of tree(or even a binary tree)?
I think there should be some strong reason why they decided not to include it in collections API.
In Java, a tree node is implemented using a class. The data inside every node can be a string, char, integer, double, or float data type. A binary tree can be implemented in two ways: A node representation and an array representation.
Java provides two in-built classes, TreeSet and TreeMap, in Java Collection Framework that cater to the needs of the programmer to describe data elements in the aforesaid form.
Example: Java Program to Implement Binary Tree Unlike other data structures, Java doesn't provide a built-in class for trees. Here, we have created our own class of BinaryTree . To learn about the binary tree, visit Binary Tree Data Structure.
Implementation of a Binary Search Tree in Java: For implementing a Binary Search Tree in Java, first of all, you need to create a Node class that stores the data to be saved in the nodes along with right and left variables of Node type to keep a reference of each child.
I think there should be some strong reason why they decided not to include it in collections API.
I think that the reason is that nobody has come up with a good API for trees that is both
(And where do you stop? Tree? Binary tree? N-ary tree? DAG? Graph?)
It is worth noting that neither Apache Commons Collections or Google Collections (aka Guava) have a tree API. However, there is an active Guava issue on this topic - http://code.google.com/p/guava-libraries/issues/detail?id=174 - so clearly at least some people agree with your point of view.
UPDATE
As of version 15.0, Guava now has tree support in the form of the TreeTraverser
and BinaryTreeTraverser
classes. But this may not be what you expect. In truth, these classes don't actually implement the tree data structure. Instead, you have to do this in a generic type parameter. Further to that, the Traverser
classes even avoid making assumptions about the APIs of the node type. They do this by being abstract classes, and requiring the concrete traverser subtype to implement the operations that interrogate the tree; e.g. to get a node's children.
FWIW, TreeMap
and TreeSet
are not "tree APIs". They are tree-based implementations of the Map
and Set
APIs. The tree-ness is totally concealed by the public APIs, making these two classes completely unsuitable for use as general purpose trees.
IMHO, theoretically, Tree is not a type of collection, it's just a type of implementation. I mean there are abstract collections such as Set (unordered set of entries), List (ordered set of entries), Map (two sets of entries with relations between them), and there are their implementations: array, list (e.g. ArrayList and LinkedList), HashSet etc., all of them have their own advantages and disadvantages. Thus, Tree is just one of implementations (for lists, e.g.) which may give you (roughly) faster search than array but no access by index.
BTW, there is TreeMap ("Red-Black tree based NavigableMap implementation") and TreeSet ("A NavigableSet implementation based on a TreeMap.") classes in Java.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With