Java TreeSet 的底层结构是什么?
What structure is underlying Java TreeSet?
Java TreeSet
是红黑树自平衡结构。
但是存储数据的结构是什么?数组还是链表?
TreeSet
由 TreeMap
支持(以类似的方式 HashSet
由 HashMap
支持)。如果您查看 TreeSet
构造函数:
public TreeSet() {
this(new TreeMap<E,Object>());
}
TreeMap
使用 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;
...
那里没有额外的数组或列表。
Java TreeSet
是红黑树自平衡结构。
但是存储数据的结构是什么?数组还是链表?
TreeSet
由 TreeMap
支持(以类似的方式 HashSet
由 HashMap
支持)。如果您查看 TreeSet
构造函数:
public TreeSet() {
this(new TreeMap<E,Object>());
}
TreeMap
使用 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;
...
那里没有额外的数组或列表。