Estructura de Datos - TreeSet

breve introducción

TreeSet describe una variante de Set se puede lograr la clasificación funciones se ajustan, se añade al elemento de hablar automáticamente de acuerdo con una cierta regla para comparar la secuencia para ser insertado en la colección ordenada cuando de hecho es la encapsulación de un TreeMap, como HashSet, LinkedHashSet, que utiliza la clave TreeMap.

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

La herencia AbstractSet clase abstracta que implementa NavigableSet interfaz

public interface NavigableSet<E> extends SortedSet<E>

NavigableSet extendió SortedSet, se puede obtener un elemento más próximo. Método inferior, suelo, techo, respectivamente, y un mayor retorno menor que, menor que o igual, mayor o igual, mayor que un elemento dado al elemento, si este elemento no está presente, se devuelve nulo.

// 返回此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 interfaz
public interface SortedSet<E> extends Set<E>

SortedSet ordenadas de acuerdo con los elementos del comparador, si el comparador está vacío, entonces el orden natural

SortedSet método
// 获取比较器
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();
propiedad TreeSet
// NavigableMap对象(NavigableMap下就一个TreeMap实现)
private transient NavigableMap<E,Object> m;
// 固定常量作为Map值
private static final Object PRESENT = new Object();

NavigableMap puede ver artículos TreeMap

constructor TreeSet
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 es en realidad en el TreeMap, estructura interna TreeMap es un árbol rojo-negro, es sólo la clave aquí, el valor constante fijo

enfoque basado TreeSet
// 长度
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 dijo que la clave es usar un TreeMap

Búsqueda TreeSet
// 第一个元素
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);
}

búsquedas TreeMap TreeSet son de color rojo-negro de búsqueda basado en árboles, otros métodos pueden remitir los artículos TreeMap.

Supongo que te gusta

Origin www.cnblogs.com/yuanjiangnan/p/12641321.html
Recomendado
Clasificación