Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is TreeSet<T> an internal type in .NET?

So, I was just digging around Reflector trying to find the implementation details of HashSet (out of sheer curiosity based on the answer to another question here) and noticed the following:

internal class TreeSet<T> : ICollection<T>, IEnumerable<T>, ICollection,
    IEnumerable, ISerializable, IDeserializationCallback

Without looking too deep into the details, it looks like a Self-Balancing Binary Search Tree.

My question is, is there anybody out there with the insight as to why this class is internal? Is it simply because the other collection types use it internally and hide the complexities of a BST from the general masses...or am I way off base?

like image 283
Justin Niessner Avatar asked Mar 16 '10 14:03

Justin Niessner


People also ask

How does TreeSet work internally in Java?

When we implement a TreeSet, it creates a TreeMap to store the elements. It sorts the elements either naturally or using the user define comparator. When the object of a TreeSet is created, it automatically invokes the default constructor and creates an object of TreeMap and assigns comparator as null.

Which internal structure does a tree set used to store elements?

TreeSet(Collection): This constructor is used to build a TreeSet object containing all the elements from the given collection in which elements will get stored in default natural sorting order.

What data structure does TreeSet use?

TreeSet uses tree data structure for storage. Objects are stored in sorted, ascending order. But we can iterate in descending order using method TreeSet.

Is TreeSet a interface in Java util package?

Java TreeSet class implements the Set interface that uses a tree for storage. It inherits AbstractSet class and implements the NavigableSet interface.


1 Answers

Exposing a type publicly involves a lot more work than only exposing it internally - it means you've got to be absolutely sure that you don't want to make significant changes to the API later, you've got to document it thoroughly etc.

I wouldn't be surprised to find a TreeSet<T> exposed in a future release, but it makes sense for MS to be cautious before making something public.

(I believe that SortedSet<T> in .NET 4 is basically a tree set, btw.)

like image 55
Jon Skeet Avatar answered Sep 22 '22 13:09

Jon Skeet