-2

Java TreeSet is a red-black tree self balancing structure.

But what is the structure to store the data? Array or linked list?

Karol Dowbecki
  • 43,645
  • 9
  • 78
  • 111
arminvanbuuren
  • 957
  • 1
  • 9
  • 16

1 Answers1

2

TreeSet is backed by a TreeMap (in similar way HashSet is backed by HashMap). If you look at TreeSet constructor:

public TreeSet() {
    this(new TreeMap<E,Object>());
}

TreeMap internally stores data using nodes represented with TreeMap.Entry class:

static final class Entry<K,V> implements Map.Entry<K,V> {
    K key;
    V value;
    Entry<K,V> left;
    Entry<K,V> right;
    Entry<K,V> parent;
    boolean color = BLACK;
...

There is no additional array or list there.

Karol Dowbecki
  • 43,645
  • 9
  • 78
  • 111