12

What are the advantages of each structure?

In my program I will be performing these steps and I was wondering which data structure above I should be using:

  1. Taking in an unsorted array and adding them to a sorted structure1.
  2. Traversing through sorted data and removing the right one
  3. Adding data (never removing) and returning that structure as an array
doubleDown
  • 8,048
  • 1
  • 32
  • 48
Enf
  • 129
  • 1
  • 1
  • 3
  • 1
    I do not want to start an extra answer because some have been given already, but I want to add one more fact: You talked about adding/removing data. What about updating? Please be aware of the fact that a TreeSet wil never update its sort order if you change element objects with regard to their "sorting key". If you want to do that, use my class [UpdateableTreeSet](http://stackoverflow.com/a/11169301/1082681) or something similar. It might be a deciding factor if you have objects with changing state. – kriegaex Aug 30 '12 at 23:21

5 Answers5

13

When do you know when to use a TreeSet or LinkedList? What are the advantages of each structure?

In general, you decide on a collection type based on the structural and performance properties that you need it to have. For instance, a TreeSet is a Set, and therefore does not allow duplicates and does not preserve insertion order of elements. By contrast a LinkedList is a List and therefore does allow duplicates and does preserve insertion order. On the performance side, TreeSet gives you O(logN) insertion and deletion, whereas LinkedList gives O(1) insertion at the beginning or end, and O(N) insertion at a selected position or deletion.

The details are all spelled out in the respective class and interface javadocs, but a useful summary may be found in the Java Collections Cheatsheet.

In practice though, the choice of collection type is intimately connected to algorithm design. The two need to be done in parallel. (It is no good deciding that your algorithm requires a collection with properties X, Y and Z, and then discovering that no such collection type exists.)

In your use-case, it looks like TreeSet would be a better fit. There is no efficient way (i.e. better than O(N^2)) to sort a large LinkedList that doesn't involve turning it into some other data structure to do the sorting. There is no efficient way (i.e. better than O(N)) to insert an element into the correct position in a previously sorted LinkedList. The third part (copying to an array) works equally well with a LinkedList or TreeSet; it is an O(N) operation in both cases.

[I'm assuming that the collections are large enough that the big O complexity predicts the actual performance accurately ... ]

denvercoder9
  • 2,979
  • 3
  • 28
  • 41
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
2

The genuine power and advantage of TreeSet lies in interface it realizes - NavigableSet

Why is it so powerfull and in which case?

Navigable Set interface add for example these 3 nice methods:

    headSet(E toElement, boolean inclusive) 
    tailSet(E fromElement, boolean inclusive)
    subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive)  

These methods allow to organize effective search algorithm(very fast).

Example: we need to find all the names which start with Milla and end with Wladimir:

    TreeSet<String> authors = new TreeSet<String>();
    authors.add("Andreas Gryphius");
    authors.add("Fjodor Michailowitsch Dostojewski");
    authors.add("Alexander Puschkin");
    authors.add("Ruslana Lyzhichko");
    authors.add("Wladimir Klitschko");
    authors.add("Andrij Schewtschenko");
    authors.add("Wayne Gretzky");
    authors.add("Johann Jakob Christoffel");
    authors.add("Milla Jovovich");
    authors.add("Taras Schewtschenko");
    System.out.println(authors.subSet("Milla", "Wladimir"));

output:

    [Milla Jovovich, Ruslana Lyzhichko, Taras Schewtschenko, Wayne Gretzky] 

TreeSet doesn't go over all the elements, it finds first and last elemenets and returns a new Collection with all the elements in the range.

Sergii Shevchyk
  • 38,716
  • 12
  • 50
  • 61
1

TreeSet:

TreeSet uses Red-Black tree underlying. So the set could be thought as a dynamic search tree. When you need a structure which is operated read/write frequently and also should keep order, the TreeSet is a good choice.

卢声远 Shengyuan Lu
  • 31,208
  • 22
  • 85
  • 130
0

If you want to keep it sorted and it's append-mostly, TreeSet with a Comparator is your best bet. The JVM would have to traverse the LinkedList from the beginning to decide where to place an item. LinkedList = O(n) for any operations, TreeSet = O(log(n)) for basic stuff.

0

The most important point when choosing a data structure are its inherent limitations. For example if you use TreeSet to store objects and during run-time your algorithm changes attributes of these objects which affect equal comparisons while the object is an element of the set, get ready for some strange bugs.

The Java Doc for Set interface state that:

Note: Great care must be exercised if mutable objects are used as set elements. The behavior of a set is not specified if the value of an object is changed in a manner that affects equals comparisons while the object is an element in the set. A special case of this prohibition is that it is not permissible for a set to contain itself as an element.

Interface Set Java Doc

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92