replacement = (p.left != null ? p.left : p.right);//p鐨勫瀛愯妭鐐
-
if (replacement != null) {//涓嶄负鍙跺瓙鑺傜偣
// Link replacement to parent
// 寤虹珛琚垹鑺傜偣鐖惰妭鐐逛笌琚垹鑺傜偣瀛╁瓙鑺傜偣鐨勫叧绯伙紝琚垹鑺傜偣鍙湁涓涓瀛愯妭鐐
diff --git a/src/java/util/TreeSet.java b/src/java/util/TreeSet.java
index dba7122..43572eb 100644
--- a/src/java/util/TreeSet.java
+++ b/src/java/util/TreeSet.java
@@ -30,10 +30,10 @@
* The elements are ordered using their {@linkplain Comparable natural
* ordering}, or by a {@link Comparator} provided at set creation
* time, depending on which constructor is used.
- *
+ *
*
This implementation provides guaranteed log(n) time cost for the basic
* operations ({@code add}, {@code remove} and {@code contains}).
- *
+ *
*
Note that the ordering maintained by a set (whether or not an explicit
* comparator is provided) must be consistent with equals if it is to
* correctly implement the {@code Set} interface. (See {@code Comparable}
@@ -45,7 +45,7 @@
* are, from the standpoint of the set, equal. The behavior of a set
* is well-defined even if its ordering is inconsistent with equals; it
* just fails to obey the general contract of the {@code Set} interface.
- *
+ *
*
Note that this implementation is not synchronized.
* If multiple threads access a tree set concurrently, and at least one
* of the threads modifies the set, it must be synchronized
@@ -56,7 +56,7 @@
* method. This is best done at creation time, to prevent accidental
* unsynchronized access to the set:
* SortedSet s = Collections.synchronizedSortedSet(new TreeSet(...));
- *
+ *
*
The iterators returned by this class's {@code iterator} method are
* fail-fast: if the set is modified at any time after the iterator is
* created, in any way except through the iterator's own {@code remove}
@@ -64,7 +64,7 @@
* Thus, in the face of concurrent modification, the iterator fails quickly
* and cleanly, rather than risking arbitrary, non-deterministic behavior at
* an undetermined time in the future.
- *
+ *
*
Note that the fail-fast behavior of an iterator cannot be guaranteed
* as it is, generally speaking, impossible to make any hard guarantees in the
* presence of unsynchronized concurrent modification. Fail-fast iterators
@@ -72,30 +72,28 @@
* Therefore, it would be wrong to write a program that depended on this
* exception for its correctness: the fail-fast behavior of iterators
* should be used only to detect bugs.
- *
+ *
*
This class is a member of the
*
* Java Collections Framework.
*
* @param the type of elements maintained by this set
- *
- * @author Josh Bloch
- * @see Collection
- * @see Set
- * @see HashSet
- * @see Comparable
- * @see Comparator
- * @see TreeMap
- * @since 1.2
+ * @author Josh Bloch
+ * @see Collection
+ * @see Set
+ * @see HashSet
+ * @see Comparable
+ * @see Comparator
+ * @see TreeMap
+ * @since 1.2
*/
-
+//鍩轰簬TreeMap锛屾墍鏈夋柟娉曠殑璋冪敤閮界敤鍒板唴閮ㄧ殑map
public class TreeSet extends AbstractSet
- implements NavigableSet, Cloneable, java.io.Serializable
-{
+ implements NavigableSet, Cloneable, java.io.Serializable {
/**
* The backing map.
*/
- private transient NavigableMap m;
+ private transient NavigableMap m;
// Dummy value to associate with an Object in the backing Map
// 浣滀负姣忎釜閿殑鍊兼坊鍔犲埌NavigableMap涓
@@ -105,7 +103,7 @@ public class TreeSet extends AbstractSet
* Constructs a set backed by the specified navigable map.
*/
//涓嶆槸public鐨勶紝鏄唴閮ㄧ敤鐨
- TreeSet(NavigableMap m) {
+ TreeSet(NavigableMap m) {
this.m = m;
}
@@ -123,7 +121,7 @@ public class TreeSet extends AbstractSet
* {@code ClassCastException}.
*/
public TreeSet() {
- this(new TreeMap());
+ this(new TreeMap());
}
/**
@@ -136,8 +134,8 @@ public TreeSet() {
* {@code add} call will throw a {@code ClassCastException}.
*
* @param comparator the comparator that will be used to order this set.
- * If {@code null}, the {@linkplain Comparable natural
- * ordering} of the elements will be used.
+ * If {@code null}, the {@linkplain Comparable natural
+ * ordering} of the elements will be used.
*/
public TreeSet(Comparator super E> comparator) {
this(new TreeMap<>(comparator));
@@ -153,8 +151,8 @@ public TreeSet(Comparator super E> comparator) {
* {@code e2} in the set.
*
* @param c collection whose elements will comprise the new set
- * @throws ClassCastException if the elements in {@code c} are
- * not {@link Comparable}, or are not mutually comparable
+ * @throws ClassCastException if the elements in {@code c} are
+ * not {@link Comparable}, or are not mutually comparable
* @throws NullPointerException if the specified collection is null
*/
@@ -228,11 +226,11 @@ public boolean isEmpty() {
*
* @param o object to be checked for containment in this set
* @return {@code true} if this set contains the specified element
- * @throws ClassCastException if the specified object cannot be compared
- * with the elements currently in the set
+ * @throws ClassCastException if the specified object cannot be compared
+ * with the elements currently in the set
* @throws NullPointerException if the specified element is null
- * and this set uses natural ordering, or its comparator
- * does not permit null elements
+ * and this set uses natural ordering, or its comparator
+ * does not permit null elements
*/
public boolean contains(Object o) {
return m.containsKey(o);
@@ -248,15 +246,15 @@ public boolean contains(Object o) {
*
* @param e element to be added to this set
* @return {@code true} if this set did not already contain the specified
- * element
- * @throws ClassCastException if the specified object cannot be compared
- * with the elements currently in this set
+ * element
+ * @throws ClassCastException if the specified object cannot be compared
+ * with the elements currently in this set
* @throws NullPointerException if the specified element is null
- * and this set uses natural ordering, or its comparator
- * does not permit null elements
+ * and this set uses natural ordering, or its comparator
+ * does not permit null elements
*/
public boolean add(E e) {
- return m.put(e, PRESENT)==null;
+ return m.put(e, PRESENT) == null;
}
/**
@@ -270,14 +268,14 @@ public boolean add(E e) {
*
* @param o object to be removed from this set, if present
* @return {@code true} if this set contained the specified element
- * @throws ClassCastException if the specified object cannot be compared
- * with the elements currently in this set
+ * @throws ClassCastException if the specified object cannot be compared
+ * with the elements currently in this set
* @throws NullPointerException if the specified element is null
- * and this set uses natural ordering, or its comparator
- * does not permit null elements
+ * and this set uses natural ordering, or its comparator
+ * does not permit null elements
*/
public boolean remove(Object o) {
- return m.remove(o)==PRESENT;
+ return m.remove(o) == PRESENT;
}
/**
@@ -293,22 +291,22 @@ public void clear() {
*
* @param c collection containing elements to be added to this set
* @return {@code true} if this set changed as a result of the call
- * @throws ClassCastException if the elements provided cannot be compared
- * with the elements currently in the set
+ * @throws ClassCastException if the elements provided cannot be compared
+ * with the elements currently in the set
* @throws NullPointerException if the specified collection is null or
- * if any element is null and this set uses natural ordering, or
- * its comparator does not permit null elements
+ * if any element is null and this set uses natural ordering, or
+ * its comparator does not permit null elements
*/
- public boolean addAll(Collection extends E> c) {
+ public boolean addAll(Collection extends E> c) {
// Use linear-time version if applicable
- if (m.size()==0 && c.size() > 0 &&
- c instanceof SortedSet &&
- m instanceof TreeMap) {
+ if (m.size() == 0 && c.size() > 0 &&
+ c instanceof SortedSet &&
+ m instanceof TreeMap) {
SortedSet extends E> set = (SortedSet extends E>) c;
- TreeMap map = (TreeMap) m;
+ TreeMap map = (TreeMap) m;
Comparator super E> cc = (Comparator super E>) set.comparator();
Comparator super E> mc = map.comparator();
- if (cc==mc || (cc != null && cc.equals(mc))) {
+ if (cc == mc || (cc != null && cc.equals(mc))) {
map.addAllForTreeSet(set, PRESENT);
return true;
}
@@ -317,24 +315,24 @@ public boolean addAll(Collection extends E> c) {
}
/**
- * @throws ClassCastException {@inheritDoc}
- * @throws NullPointerException if {@code fromElement} or {@code toElement}
- * is null and this set uses natural ordering, or its comparator
- * does not permit null elements
+ * @throws ClassCastException {@inheritDoc}
+ * @throws NullPointerException if {@code fromElement} or {@code toElement}
+ * is null and this set uses natural ordering, or its comparator
+ * does not permit null elements
* @throws IllegalArgumentException {@inheritDoc}
* @since 1.6
*/
public NavigableSet subSet(E fromElement, boolean fromInclusive,
- E toElement, boolean toInclusive) {
+ E toElement, boolean toInclusive) {
return new TreeSet<>(m.subMap(fromElement, fromInclusive,
- toElement, toInclusive));
+ toElement, toInclusive));
}
/**
- * @throws ClassCastException {@inheritDoc}
- * @throws NullPointerException if {@code toElement} is null and
- * this set uses natural ordering, or its comparator does
- * not permit null elements
+ * @throws ClassCastException {@inheritDoc}
+ * @throws NullPointerException if {@code toElement} is null and
+ * this set uses natural ordering, or its comparator does
+ * not permit null elements
* @throws IllegalArgumentException {@inheritDoc}
* @since 1.6
*/
@@ -343,10 +341,10 @@ public NavigableSet headSet(E toElement, boolean inclusive) {
}
/**
- * @throws ClassCastException {@inheritDoc}
- * @throws NullPointerException if {@code fromElement} is null and
- * this set uses natural ordering, or its comparator does
- * not permit null elements
+ * @throws ClassCastException {@inheritDoc}
+ * @throws NullPointerException if {@code fromElement} is null and
+ * this set uses natural ordering, or its comparator does
+ * not permit null elements
* @throws IllegalArgumentException {@inheritDoc}
* @since 1.6
*/
@@ -355,10 +353,10 @@ public NavigableSet tailSet(E fromElement, boolean inclusive) {
}
/**
- * @throws ClassCastException {@inheritDoc}
- * @throws NullPointerException if {@code fromElement} or
- * {@code toElement} is null and this set uses natural ordering,
- * or its comparator does not permit null elements
+ * @throws ClassCastException {@inheritDoc}
+ * @throws NullPointerException if {@code fromElement} or
+ * {@code toElement} is null and this set uses natural ordering,
+ * or its comparator does not permit null elements
* @throws IllegalArgumentException {@inheritDoc}
*/
public SortedSet subSet(E fromElement, E toElement) {
@@ -366,10 +364,10 @@ public SortedSet subSet(E fromElement, E toElement) {
}
/**
- * @throws ClassCastException {@inheritDoc}
- * @throws NullPointerException if {@code toElement} is null
- * and this set uses natural ordering, or its comparator does
- * not permit null elements
+ * @throws ClassCastException {@inheritDoc}
+ * @throws NullPointerException if {@code toElement} is null
+ * and this set uses natural ordering, or its comparator does
+ * not permit null elements
* @throws IllegalArgumentException {@inheritDoc}
*/
public SortedSet headSet(E toElement) {
@@ -377,10 +375,10 @@ public SortedSet headSet(E toElement) {
}
/**
- * @throws ClassCastException {@inheritDoc}
- * @throws NullPointerException if {@code fromElement} is null
- * and this set uses natural ordering, or its comparator does
- * not permit null elements
+ * @throws ClassCastException {@inheritDoc}
+ * @throws NullPointerException if {@code fromElement} is null
+ * and this set uses natural ordering, or its comparator does
+ * not permit null elements
* @throws IllegalArgumentException {@inheritDoc}
*/
public SortedSet tailSet(E fromElement) {
@@ -408,10 +406,10 @@ public E last() {
// NavigableSet API methods
/**
- * @throws ClassCastException {@inheritDoc}
+ * @throws ClassCastException {@inheritDoc}
* @throws NullPointerException if the specified element is null
- * and this set uses natural ordering, or its comparator
- * does not permit null elements
+ * and this set uses natural ordering, or its comparator
+ * does not permit null elements
* @since 1.6
*/
public E lower(E e) {
@@ -419,10 +417,10 @@ public E lower(E e) {
}
/**
- * @throws ClassCastException {@inheritDoc}
+ * @throws ClassCastException {@inheritDoc}
* @throws NullPointerException if the specified element is null
- * and this set uses natural ordering, or its comparator
- * does not permit null elements
+ * and this set uses natural ordering, or its comparator
+ * does not permit null elements
* @since 1.6
*/
public E floor(E e) {
@@ -430,10 +428,10 @@ public E floor(E e) {
}
/**
- * @throws ClassCastException {@inheritDoc}
+ * @throws ClassCastException {@inheritDoc}
* @throws NullPointerException if the specified element is null
- * and this set uses natural ordering, or its comparator
- * does not permit null elements
+ * and this set uses natural ordering, or its comparator
+ * does not permit null elements
* @since 1.6
*/
public E ceiling(E e) {
@@ -441,10 +439,10 @@ public E ceiling(E e) {
}
/**
- * @throws ClassCastException {@inheritDoc}
+ * @throws ClassCastException {@inheritDoc}
* @throws NullPointerException if the specified element is null
- * and this set uses natural ordering, or its comparator
- * does not permit null elements
+ * and this set uses natural ordering, or its comparator
+ * does not permit null elements
* @since 1.6
*/
public E higher(E e) {
@@ -455,7 +453,7 @@ public E higher(E e) {
* @since 1.6
*/
public E pollFirst() {
- Map.Entry e = m.pollFirstEntry();
+ Map.Entry e = m.pollFirstEntry();
return (e == null) ? null : e.getKey();
}
@@ -463,7 +461,7 @@ public E pollFirst() {
* @since 1.6
*/
public E pollLast() {
- Map.Entry e = m.pollLastEntry();
+ Map.Entry e = m.pollLastEntry();
return (e == null) ? null : e.getKey();
}
@@ -490,15 +488,15 @@ public Object clone() {
* serialize it).
*
* @serialData Emits the comparator used to order this set, or
- * {@code null} if it obeys its elements' natural ordering
- * (Object), followed by the size of the set (the number of
- * elements it contains) (int), followed by all of its
- * elements (each an Object) in order (as determined by the
- * set's Comparator, or by the elements' natural ordering if
- * the set has no Comparator).
+ * {@code null} if it obeys its elements' natural ordering
+ * (Object), followed by the size of the set (the number of
+ * elements it contains) (int), followed by all of its
+ * elements (each an Object) in order (as determined by the
+ * set's Comparator, or by the elements' natural ordering if
+ * the set has no Comparator).
*/
private void writeObject(java.io.ObjectOutputStream s)
- throws java.io.IOException {
+ throws java.io.IOException {
// Write out any hidden stuff
s.defaultWriteObject();
@@ -518,7 +516,7 @@ private void writeObject(java.io.ObjectOutputStream s)
* deserialize it).
*/
private void readObject(java.io.ObjectInputStream s)
- throws java.io.IOException, ClassNotFoundException {
+ throws java.io.IOException, ClassNotFoundException {
// Read in any hidden stuff
s.defaultReadObject();
@@ -526,8 +524,8 @@ private void readObject(java.io.ObjectInputStream s)
Comparator super E> c = (Comparator super E>) s.readObject();
// Create backing TreeMap
- TreeMap tm;
- if (c==null)
+ TreeMap tm;
+ if (c == null)
tm = new TreeMap<>();
else
tm = new TreeMap<>(c);