Data Structure - TreeSet

Brief introduction

TreeSet describes a variant of Set can be achieved sorting functions are set, it is added to the element to speak automatically according to a certain rule to compare the sequence to be inserted into the ordered collection when in fact it it is the encapsulation of a TreeMap, like HashSet, LinkedHashSet, which uses TreeMap key.

TreeSet class
public class TreeSet<E> extends AbstractSet<E>
        implements NavigableSet<E>, Cloneable, java.io.Serializable

Inheritance AbstractSet Abstract class that implements NavigableSet interface

public interface NavigableSet<E> extends SortedSet<E>

NavigableSet extended SortedSet, you can get a closest element. Method lower, floor, ceiling, respectively, and higher return less than, less than or equal, greater than or equal, greater than a given element to the element, if this element is not present, null is returned.

// 返回此set中小于给定元素的最大元素,如果没有这样的元素,则返回NULL。
E lower(E e);
// 返回此集合中小于或等于给定元素的最大元素,如果没有这样的元素,则返回NULL。
E floor(E e);
// 返回此set中大于或等于给定元素的最小元素,如果没有这样的元素,则返回NULL。
E ceiling(E e);
// 返回此set中大于给定元素的最小元素,如果没有这样的元素,则返回NULL。
E higher(E e);
// 检索并删除第一个最小元素。如果没有这样的元素,则返回null。
E pollFirst();
// 检索并删除最后一个最高元素。如果没有这样的元素,则返回null。
E pollLast();
// 迭代器
Iterator<E> iterator();
// 返回此 set 中所包含元素的逆序视图。
NavigableSet<E> descendingSet();
// 以降序返回在此 set 的元素上进行迭代的迭代器。
Iterator<E> descendingIterator();
// 返回此 set 的部分视图,其元素范围从 fromElement 到 toElement。
NavigableSet<E> subSet(E fromElement, boolean fromInclusive,
                       E toElement,   boolean toInclusive);
// 返回此 set 的部分视图,其元素小于(或等于,如果 inclusive 为 true)toElement。
NavigableSet<E> headSet(E toElement, boolean inclusive);
//  返回此 set 的部分视图,其元素大于(或等于,如果 inclusive 为 true)fromElement。
NavigableSet<E> tailSet(E fromElement, boolean inclusive);
// 返回此 set 的部分视图,其元素从 fromElement(包括)到 toElement(不包括)。
SortedSet<E> subSet(E fromElement, E toElement);
// 返回此 set 的部分视图,其元素严格小于 toElement。
SortedSet<E> headSet(E toElement);
// 返回此 set 的部分视图,其元素大于(或等于,如果 inclusive 为 true)fromElement。
SortedSet<E> tailSet(E fromElement);
SortedSet interface
public interface SortedSet<E> extends Set<E>

SortedSet sorted according to the elements of the comparator, if the comparator is empty, then the natural order

SortedSet method
// 获取比较器
Comparator<? super E> comparator();
// 获取两个元素直接的元素集
SortedSet<E> subSet(E fromElement, E toElement);
// 获取之前的元素
SortedSet<E> headSet(E toElement);
// 获取之后的元素
SortedSet<E> tailSet(E fromElement);
// 第一个元素
E first();
// 最后一个元素
E last();
TreeSet property
// NavigableMap对象(NavigableMap下就一个TreeMap实现)
private transient NavigableMap<E,Object> m;
// 固定常量作为Map值
private static final Object PRESENT = new Object();

NavigableMap can see TreeMap articles

TreeSet constructor
TreeSet(NavigableMap<E,Object> m) {
    this.m = m;
}
public TreeSet() {
    this(new TreeMap<E,Object>());
}
public TreeSet(Comparator<? super E> comparator) {
    this(new TreeMap<>(comparator));
}
public TreeSet(Collection<? extends E> c) {
    this();
    addAll(c);
}
public TreeSet(SortedSet<E> s) {
    this(s.comparator());
    addAll(s);
}

TreeSet is actually in the TreeMap, TreeMap internal structure is a red-black tree, it is only the key here, the fixed constant value

TreeSet based approach
// 长度
public int size() {
    return m.size();
}
// 是否为空
public boolean isEmpty() {
    return m.isEmpty();
}
// 是否存在
public boolean contains(Object o) {
    return m.containsKey(o);
}
// 添加
public boolean add(E e) {
    return m.put(e, PRESENT)==null;
}
// 删除
public boolean remove(Object o) {
    return m.remove(o)==PRESENT;
}
// 清空
public void clear() {
    m.clear();
}

White said the key is to use a TreeMap

TreeSet search
// 第一个元素
public E first() {
    return m.firstKey();
}
// 最后一个元素
public E last() {
    return m.lastKey();
}
// 获取最接近e的节点(小于等于)
public E lower(E e) {
    return m.lowerKey(e);
}
// 获取最接近e的节点(大于等于)
public E floor(E e) {
    return m.floorKey(e);
}

TreeMap TreeSet searches are red-black tree-based search, other methods can refer TreeMap articles.

Guess you like

Origin www.cnblogs.com/yuanjiangnan/p/12641321.html