SortedMap 接口的基于红黑树的实现。此类保证了Map按照升序顺序排列关键字,根据使用的构造方法不同,可能会按照键的类的自然顺序进行排序(Comparable),或者按照创建时所提供的比较(Comparator)进行排序。此实现不是同步的,多个线程同时访问一个同一个map时,一般通过对封装该map的对象进行同步操作来完成,或者使用 Map m = Collections.synchronizedMap(new TreeMap(...)) 方法来包装该map
RED-BLACK树的性质:
1、根元素永远是显色。
2、除根节点外,所有新插入的节点都是红色的。
3、Red规则:红色节点的子节点都是黑色的,不能有两个红色节点相邻。
4、Path规则:从根元素到某个子节点或是只有一个子节点的元素的所有路径中,黑色元素的个数必须相同。
- package tree.redblack;
-
- import java.util.AbstractCollection;
- import java.util.AbstractMap;
- import java.util.AbstractSet;
- import java.util.Collection;
- import java.util.Comparator;
- import java.util.ConcurrentModificationException;
- import java.util.Iterator;
- import java.util.Map;
- import java.util.NoSuchElementException;
- import java.util.Set;
- import java.util.SortedMap;
-
- public class TreeMap extends AbstractMap implements SortedMap, Cloneable,
- java.io.Serializable {
-
-
- private Comparator comparator = null;
-
- private transient Entry root = null;
-
- private transient int size = 0;
-
-
- private transient int modCount = 0;
-
-
- private void incrementSize() {
- modCount++;
- size++;
- }
-
- private void decrementSize() {
- modCount++;
- size--;
- }
-
-
- public TreeMap() {
- }
-
-
- public TreeMap(Comparator c) {
- this.comparator = c;
- }
-
-
- public TreeMap(Map m) {
- putAll(m);
- }
-
-
- public TreeMap(SortedMap m) {
- comparator = m.comparator();
-
- }
-
- public int size() {
- return size;
- }
-
- public boolean containsKey(Object key) {
- return getEntry(key) != null;
- }
-
- public boolean containsValue(Object value) {
- return (root == null ? false : (value == null ? valueSearchNull(root)
- : valueSearchNonNull(root, value)));
- }
-
-
- private boolean valueSearchNull(Entry n) {
- if (n.value == null)
- return true;
-
-
- return (n.left != null && valueSearchNull(n.left))
- || (n.right != null && valueSearchNull(n.right));
- }
-
-
- private boolean valueSearchNonNull(Entry n, Object value) {
-
- if (value.equals(n.value))
- return true;
-
-
- return (n.left != null && valueSearchNonNull(n.left, value))
- || (n.right != null && valueSearchNonNull(n.right, value));
- }
-
-
- public Object get(Object key) {
- Entry p = getEntry(key);
- return (p == null ? null : p.value);
- }
-
-
- public Object firstKey() {
- return key(firstEntry());
- }
-
-
- public Object lastKey() {
- return key(lastEntry());
- }
-
-
- private Entry getEntry(Object key) {
- Entry p = root;
- while (p != null) {
- int cmp = compare(key, p.key);
- if (cmp == 0)
- return p;
- else if (cmp < 0)
- p = p.left;
- else
- p = p.right;
- }
- return null;
- }
-
- private static Object key(Entry e) {
- if (e == null)
- throw new NoSuchElementException();
- return e.key;
- }
-
-
- public Object put(Object key, Object value) {
-
- Entry t = root;
-
- if (t == null) {
- incrementSize();
- root = new Entry(key, value, null);
- return null;
- }
-
- while (true) {
- int cmp = compare(key, t.key);
- if (cmp == 0) {
- return t.setValue(value);
- } else if (cmp < 0) {
- if (t.left != null) {
- t = t.left;
- } else {
- incrementSize();
- t.left = new Entry(key, value, t);
-
- fixAfterInsertion(t.left);
- return null;
- }
- } else {
- if (t.right != null) {
- t = t.right;
- } else {
- incrementSize();
- t.right = new Entry(key, value, t);
-
- fixAfterInsertion(t.right);
- return null;
- }
- }
- }
- }
-
-
- public Object remove(Object key) {
- Entry p = getEntry(key);
- if (p == null)
- return null;
-
- Object oldValue = p.value;
- deleteEntry(p);
- return oldValue;
- }
-
- public void clear() {
- modCount++;
- size = 0;
- root = null;
- }
-
- public Object clone() {
- TreeMap clone = null;
- try {
-
- clone = (TreeMap) super.clone();
- } catch (CloneNotSupportedException e) {
- throw new InternalError();
- }
-
-
- clone.root = null;
- clone.size = 0;
- clone.modCount = 0;
- clone.entrySet = null;
-
- return clone;
- }
-
-
-
-
- private transient volatile Set entrySet = null;
-
-
- transient volatile Set keySet = null;
-
- transient volatile Collection values = null;
-
- public Set keySet() {
- if (keySet == null) {
-
- keySet = new AbstractSet() {
- public Iterator iterator() {
-
- return new KeyIterator();
- }
-
- public int size() {
- return TreeMap.this.size();
- }
-
- };
- }
- return keySet;
- }
-
-
- public Collection values() {
- if (values == null) {
-
- values = new AbstractCollection() {
- public Iterator iterator() {
-
- return new ValueIterator();
- }
-
- public int size() {
- return TreeMap.this.size();
- }
-
- };
- }
- return values;
- }
-
-
- public Set entrySet() {
- if (entrySet == null) {
- entrySet = new AbstractSet() {
- public Iterator iterator() {
-
- return new EntryIterator();
- }
-
-
- public int size() {
- return TreeMap.this.size();
- }
-
- };
- }
- return entrySet;
- }
-
-
- private class EntryIterator implements Iterator {
- private int expectedModCount = TreeMap.this.modCount;
- private Entry lastReturned = null;
- Entry next;
-
- EntryIterator() {
- next = firstEntry();
- }
-
-
- EntryIterator(Entry first) {
- next = first;
- }
-
- public boolean hasNext() {
-
- return next != null;
- }
-
-
- final Entry nextEntry() {
- if (next == null)
- throw new NoSuchElementException();
- if (modCount != expectedModCount)
- throw new ConcurrentModificationException();
- lastReturned = next;
- next = successor(next);
- return lastReturned;
- }
-
- public Object next() {
-
- return nextEntry();
- }
-
- public void remove() {
- if (lastReturned == null)
- throw new IllegalStateException();
- if (modCount != expectedModCount)
- throw new ConcurrentModificationException();
-
-
- if (lastReturned.left != null && lastReturned.right != null)
- next = lastReturned;
- deleteEntry(lastReturned);
- expectedModCount++;
- lastReturned = null;
- }
- }
-
-
- private class KeyIterator extends EntryIterator {
- public Object next() {
- return nextEntry().key;
- }
- }
-
-
- private class ValueIterator extends EntryIterator {
- public Object next() {
- return nextEntry().value;
- }
- }
-
-
- private int compare(Object k1, Object k2) {
- return (comparator == null ? ((Comparable) k1).compareTo(k2) : comparator
- .compare(k1, k2));
- }
-
- private static boolean valEquals(Object o1, Object o2) {
- return (o1 == null ? o2 == null : o1.equals(o2));
- }
-
- private static final boolean RED = false;
- private static final boolean BLACK = true;
-
-
-
- static class Entry implements Map.Entry {
- Object key;
- Object value;
- Entry left = null;
- Entry right = null;
- Entry parent;
- boolean color = BLACK;
-
- Entry(Object key, Object value, Entry parent) {
- this.key = key;
- this.value = value;
- this.parent = parent;
- }
-
- public Object getKey() {
- return key;
- }
-
- public Object getValue() {
- return value;
- }
-
- public Object setValue(Object value) {
- Object oldValue = this.value;
- this.value = value;
- return oldValue;
- }
-
-
- public String toString() {
- return key + "=" + value;
- }
- }
-
-
- private Entry firstEntry() {
- Entry p = root;
- if (p != null)
- while (p.left != null)
- p = p.left;
- return p;
- }
-
-
- private Entry lastEntry() {
- Entry p = root;
- if (p != null)
- while (p.right != null)
- p = p.right;
- return p;
- }
-
-
- private Entry successor(Entry t) {
- if (t == null)
- return null;
- else if (t.right != null) {
- Entry p = t.right;
- while (p.left != null)
- p = p.left;
- return p;
- } else {
- Entry p = t.parent;
- Entry ch = t;
- while (p != null && ch == p.right) {
- ch = p;
- p = p.parent;
- }
- return p;
- }
- }
-
-
- private static boolean colorOf(Entry p) {
-
- return (p == null ? BLACK : p.color);
- }
-
- private static Entry parentOf(Entry p) {
- return (p == null ? null : p.parent);
- }
-
- private static void setColor(Entry p, boolean c) {
- if (p != null)
- p.color = c;
- }
-
- private static Entry leftOf(Entry p) {
- return (p == null) ? null : p.left;
- }
-
- private static Entry rightOf(Entry p) {
- return (p == null) ? null : p.right;
- }
-
-
- private void rotateLeft(Entry p) {
- Entry r = p.right;
- p.right = r.left;
- if (r.left != null)
- r.left.parent = p;
- r.parent = p.parent;
- if (p.parent == null)
- root = r;
- else if (p.parent.left == p)
- p.parent.left = r;
- else
- p.parent.right = r;
- r.left = p;
- p.parent = r;
- }
-
-
- private void rotateRight(Entry p) {
- Entry l = p.left;
- p.left = l.right;
- if (l.right != null)
- l.right.parent = p;
- l.parent = p.parent;
- if (p.parent == null)
- root = l;
- else if (p.parent.right == p)
- p.parent.right = l;
- else
- p.parent.left = l;
- l.right = p;
- p.parent = l;
- }
-
-
- private void fixAfterInsertion(Entry x) {
-
- x.color = RED;
-
-
- while (x != null && x != root && x.parent.color == RED) {
-
-
- if (parentOf(x) == leftOf(parentOf(parentOf(x)))) {
-
-
- Entry y = rightOf(parentOf(parentOf(x)));
-
- if (colorOf(y) == RED) {
-
-
-
- setColor(parentOf(x), BLACK);
-
- setColor(y, BLACK);
-
- setColor(parentOf(parentOf(x)), RED);
-
-
- x = parentOf(parentOf(x));
- }
- else {
-
- if (x == rightOf(parentOf(x))) {
-
- x = parentOf(x);
- rotateLeft(x);
- }
-
-
- setColor(parentOf(x), BLACK);
- setColor(parentOf(parentOf(x)), RED);
- if (parentOf(parentOf(x)) != null)
- rotateRight(parentOf(parentOf(x)));
- }
- }
- else {
-
- Entry y = leftOf(parentOf(parentOf(x)));
-
- if (colorOf(y) == RED) {
-
- setColor(parentOf(x), BLACK);
- setColor(y, BLACK);
- setColor(parentOf(parentOf(x)), RED);
-
- x = parentOf(parentOf(x));
- }
- else {
- if (x == leftOf(parentOf(x))) {
-
- x = parentOf(x);
- rotateRight(x);
- }
-
-
- setColor(parentOf(x), BLACK);
- setColor(parentOf(parentOf(x)), RED);
- if (parentOf(parentOf(x)) != null)
- rotateLeft(parentOf(parentOf(x)));
- }
- }
- }
- root.color = BLACK;
- }
-
-
- private void deleteEntry(Entry p) {
- decrementSize();
-
-
- if (p.left != null && p.right != null) {
- Entry s = successor(p);
- p.key = s.key;
- p.value = s.value;
- p = s;
- }
-
-
- Entry replacement = (p.left != null ? p.left : p.right);
-
-
- if (replacement != null) {
-
- replacement.parent = p.parent;
-
- if (p.parent == null)
- root = replacement;
- else if (p == p.parent.left)
-
- p.parent.left = replacement;
- else
-
- p.parent.right = replacement;
-
-
- p.left = p.right = p.parent = null;
-
-
-
- if (p.color == BLACK)
-
- fixAfterDeletion(replacement);
- } else if (p.parent == null) {
- root = null;
- } else {
-
-
- if (p.color == BLACK)
-
- fixAfterDeletion(p);
-
-
- if (p.parent != null) {
- if (p == p.parent.left)
- p.parent.left = null;
- else if (p == p.parent.right)
- p.parent.right = null;
- p.parent = null;
- }
- }
- }
-
-
- private void fixAfterDeletion(Entry x) {
-
-
- while (x != root && colorOf(x) == BLACK) {
-
- if (x == leftOf(parentOf(x))) {
-
- Entry sib = rightOf(parentOf(x));
-
- if (colorOf(sib) == RED) {
-
-
- setColor(sib, BLACK);
- setColor(parentOf(x), RED);
- rotateLeft(parentOf(x));
- sib = rightOf(parentOf(x));
- }
-
- if (colorOf(leftOf(sib)) == BLACK && colorOf(rightOf(sib)) == BLACK) {
-
- setColor(sib, RED);
- x = parentOf(x);
- } else {
- if (colorOf(rightOf(sib)) == BLACK) {
-
- setColor(leftOf(sib), BLACK);
- setColor(sib, RED);
- rotateRight(sib);
- sib = rightOf(parentOf(x));
- }
-
- setColor(sib, colorOf(parentOf(x)));
- setColor(parentOf(x), BLACK);
- setColor(rightOf(sib), BLACK);
- rotateLeft(parentOf(x));
-
-
- x = root;
- }
- } else {
- Entry sib = leftOf(parentOf(x));
-
- if (colorOf(sib) == RED) {
- setColor(sib, BLACK);
- setColor(parentOf(x), RED);
- rotateRight(parentOf(x));
- sib = leftOf(parentOf(x));
- }
-
- if (colorOf(rightOf(sib)) == BLACK && colorOf(leftOf(sib)) == BLACK) {
- setColor(sib, RED);
- x = parentOf(x);
- } else {
- if (colorOf(leftOf(sib)) == BLACK) {
- setColor(rightOf(sib), BLACK);
- setColor(sib, RED);
- rotateLeft(sib);
- sib = leftOf(parentOf(x));
- }
- setColor(sib, colorOf(parentOf(x)));
- setColor(parentOf(x), BLACK);
- setColor(leftOf(sib), BLACK);
- rotateRight(parentOf(x));
- x = root;
- }
- }
- }
-
- setColor(x, BLACK);
- }
- }
- package tree.redblack;
-
- import java.util.AbstractCollection;
- import java.util.AbstractMap;
- import java.util.AbstractSet;
- import java.util.Collection;
- import java.util.Comparator;
- import java.util.ConcurrentModificationException;
- import java.util.Iterator;
- import java.util.Map;
- import java.util.NoSuchElementException;
- import java.util.Set;
- import java.util.SortedMap;
-
- public class TreeMap extends AbstractMap implements SortedMap, Cloneable,
- java.io.Serializable {
-
-
- private Comparator comparator = null;
-
- private transient Entry root = null;
-
- private transient int size = 0;
-
-
- private transient int modCount = 0;
-
-
- private void incrementSize() {
- modCount++;
- size++;
- }
-
- private void decrementSize() {
- modCount++;
- size--;
- }
-
-
- public TreeMap() {
- }
-
-
- public TreeMap(Comparator c) {
- this.comparator = c;
- }
-
-
- public TreeMap(Map m) {
- putAll(m);
- }
-
-
- public TreeMap(SortedMap m) {
- comparator = m.comparator();
-
- }
-
- public int size() {
- return size;
- }
-
- public boolean containsKey(Object key) {
- return getEntry(key) != null;
- }
-
- public boolean containsValue(Object value) {
- return (root == null ? false : (value == null ? valueSearchNull(root)
- : valueSearchNonNull(root, value)));
- }
-
-
- private boolean valueSearchNull(Entry n) {
- if (n.value == null)
- return true;
-
-
- return (n.left != null && valueSearchNull(n.left))
- || (n.right != null && valueSearchNull(n.right));
- }
-
-
- private boolean valueSearchNonNull(Entry n, Object value) {
-
- if (value.equals(n.value))
- return true;
-
-
- return (n.left != null && valueSearchNonNull(n.left, value))
- || (n.right != null && valueSearchNonNull(n.right, value));
- }
-
-
- public Object get(Object key) {
- Entry p = getEntry(key);
- return (p == null ? null : p.value);
- }
-
-
- public Object firstKey() {
- return key(firstEntry());
- }
-
-
- public Object lastKey() {
- return key(lastEntry());
- }
-
-
- private Entry getEntry(Object key) {
- Entry p = root;
- while (p != null) {
- int cmp = compare(key, p.key);
- if (cmp == 0)
- return p;
- else if (cmp < 0)
- p = p.left;
- else
- p = p.right;
- }
- return null;
- }
-
- private static Object key(Entry e) {
- if (e == null)
- throw new NoSuchElementException();
- return e.key;
- }
-
-
- public Object put(Object key, Object value) {
-
- Entry t = root;
-
- if (t == null) {
- incrementSize();
- root = new Entry(key, value, null);
- return null;
- }
-
- while (true) {
- int cmp = compare(key, t.key);
- if (cmp == 0) {
- return t.setValue(value);
- } else if (cmp < 0) {
- if (t.left != null) {
- t = t.left;
- } else {
- incrementSize();
- t.left = new Entry(key, value, t);
-
- fixAfterInsertion(t.left);
- return null;
- }
- } else {
- if (t.right != null) {
- t = t.right;
- } else {
- incrementSize();
- t.right = new Entry(key, value, t);
-
- fixAfterInsertion(t.right);
- return null;
- }
- }
- }
- }
-
-
- public Object remove(Object key) {
- Entry p = getEntry(key);
- if (p == null)
- return null;
-
- Object oldValue = p.value;
- deleteEntry(p);
- return oldValue;
- }
-
- public void clear() {
- modCount++;
- size = 0;
- root = null;
- }
-
- public Object clone() {
- TreeMap clone = null;
- try {
-
- clone = (TreeMap) super.clone();
- } catch (CloneNotSupportedException e) {
- throw new InternalError();
- }
-
-
- clone.root = null;
- clone.size = 0;
- clone.modCount = 0;
- clone.entrySet = null;
-
- return clone;
- }
-
-
-
-
- private transient volatile Set entrySet = null;
-
-
- transient volatile Set keySet = null;
-
- transient volatile Collection values = null;
-
- public Set keySet() {
- if (keySet == null) {
-
- keySet = new AbstractSet() {
- public Iterator iterator() {
-
- return new KeyIterator();
- }
-
- public int size() {
- return TreeMap.this.size();
- }
-
- };
- }
- return keySet;
- }
-
-
- public Collection values() {
- if (values == null) {
-
- values = new AbstractCollection() {
- public Iterator iterator() {
-
- return new ValueIterator();
- }
-
- public int size() {
- return TreeMap.this.size();
- }
-
- };
- }
- return values;
- }
-
-
- public Set entrySet() {
- if (entrySet == null) {
- entrySet = new AbstractSet() {
- public Iterator iterator() {
-
- return new EntryIterator();
- }
-
-
- public int size() {
- return TreeMap.this.size();
- }
-
- };
- }
- return entrySet;
- }
-
-
- private class EntryIterator implements Iterator {
- private int expectedModCount = TreeMap.this.modCount;
- private Entry lastReturned = null;
- Entry next;
-
- EntryIterator() {
- next = firstEntry();
- }
-
-
- EntryIterator(Entry first) {
- next = first;
- }
-
- public boolean hasNext() {
-
- return next != null;
- }
-
-
- final Entry nextEntry() {
- if (next == null)
- throw new NoSuchElementException();
- if (modCount != expectedModCount)
- throw new ConcurrentModificationException();
- lastReturned = next;
- next = successor(next);
- return lastReturned;
- }
-
- public Object next() {
-
- return nextEntry();
- }
-
- public void remove() {
- if (lastReturned == null)
- throw new IllegalStateException();
- if (modCount != expectedModCount)
- throw new ConcurrentModificationException();
-
-
- if (lastReturned.left != null && lastReturned.right != null)
- next = lastReturned;
- deleteEntry(lastReturned);
- expectedModCount++;
- lastReturned = null;
- }
- }
-
-
- private class KeyIterator extends EntryIterator {
- public Object next() {
- return nextEntry().key;
- }
- }
-
-
- private class ValueIterator extends EntryIterator {
- public Object next() {
- return nextEntry().value;
- }
- }
-
-
- private int compare(Object k1, Object k2) {
- return (comparator == null ? ((Comparable) k1).compareTo(k2) : comparator
- .compare(k1, k2));
- }
-
- private static boolean valEquals(Object o1, Object o2) {
- return (o1 == null ? o2 == null : o1.equals(o2));
- }
-
- private static final boolean RED = false;
- private static final boolean BLACK = true;
-
-
-
- static class Entry implements Map.Entry {
- Object key;
- Object value;
- Entry left = null;
- Entry right = null;
- Entry parent;
- boolean color = BLACK;
-
- Entry(Object key, Object value, Entry parent) {
- this.key = key;
- this.value = value;
- this.parent = parent;
- }
-
- public Object getKey() {
- return key;
- }
-
- public Object getValue() {
- return value;
- }
-
- public Object setValue(Object value) {
- Object oldValue = this.value;
- this.value = value;
- return oldValue;
- }
-
-
- public String toString() {
- return key + "=" + value;
- }
- }
-
-
- private Entry firstEntry() {
- Entry p = root;
- if (p != null)
- while (p.left != null)
- p = p.left;
- return p;
- }
-
-
- private Entry lastEntry() {
- Entry p = root;
- if (p != null)
- while (p.right != null)
- p = p.right;
- return p;
- }
-
-
- private Entry successor(Entry t) {
- if (t == null)
- return null;
- else if (t.right != null) {
- Entry p = t.right;
- while (p.left != null)
- p = p.left;
- return p;
- } else {
- Entry p = t.parent;
- Entry ch = t;
- while (p != null && ch == p.right) {
- ch = p;
- p = p.parent;
- }
- return p;
- }
- }
-
-
- private static boolean colorOf(Entry p) {
-
- return (p == null ? BLACK : p.color);
- }
-
- private static Entry parentOf(Entry p) {
- return (p == null ? null : p.parent);
- }
-
- private static void setColor(Entry p, boolean c) {
- if (p != null)
- p.color = c;
- }
-
- private static Entry leftOf(Entry p) {
- return (p == null) ? null : p.left;
- }
-
- private static Entry rightOf(Entry p) {
- return (p == null) ? null : p.right;
- }
-
-
- private void rotateLeft(Entry p) {
- Entry r = p.right;
- p.right = r.left;
- if (r.left != null)
- r.left.parent = p;
- r.parent = p.parent;
- if (p.parent == null)
- root = r;
- else if (p.parent.left == p)
- p.parent.left = r;
- else
- p.parent.right = r;
- r.left = p;
- p.parent = r;
- }
-
-
- private void rotateRight(Entry p) {
- Entry l = p.left;
- p.left = l.right;
- if (l.right != null)
- l.right.parent = p;
- l.parent = p.parent;
- if (p.parent == null)
- root = l;
- else if (p.parent.right == p)
- p.parent.right = l;
- else
- p.parent.left = l;
- l.right = p;
- p.parent = l;
- }
-
-
- private void fixAfterInsertion(Entry x) {
-
- x.color = RED;
-
-
- while (x != null && x != root && x.parent.color == RED) {
-
-
- if (parentOf(x) == leftOf(parentOf(parentOf(x)))) {
-
-
- Entry y = rightOf(parentOf(parentOf(x)));
-
- if (colorOf(y) == RED) {
-
-
-
- setColor(parentOf(x), BLACK);
-
- setColor(y, BLACK);
-
- setColor(parentOf(parentOf(x)), RED);
-
-
- x = parentOf(parentOf(x));
- }
- else {
-
- if (x == rightOf(parentOf(x))) {
-
- x = parentOf(x);
- rotateLeft(x);
- }
-
-
- setColor(parentOf(x), BLACK);
- setColor(parentOf(parentOf(x)), RED);
- if (parentOf(parentOf(x)) != null)
- rotateRight(parentOf(parentOf(x)));
- }
- }
- else {
-
- Entry y = leftOf(parentOf(parentOf(x)));
-
- if (colorOf(y) == RED) {
-
- setColor(parentOf(x), BLACK);
- setColor(y, BLACK);
- setColor(parentOf(parentOf(x)), RED);
-
- x = parentOf(parentOf(x));
- }
- else {
- if (x == leftOf(parentOf(x))) {
-
- x = parentOf(x);
- rotateRight(x);
- }
-
-
- setColor(parentOf(x), BLACK);
- setColor(parentOf(parentOf(x)), RED);
- if (parentOf(parentOf(x)) != null)
- rotateLeft(parentOf(parentOf(x)));
- }
- }
- }
- root.color = BLACK;
- }
-
-
- private void deleteEntry(Entry p) {
- decrementSize();
-
-
- if (p.left != null && p.right != null) {
- Entry s = successor(p);
- p.key = s.key;
- p.value = s.value;
- p = s;
- }
-
-
- Entry replacement = (p.left != null ? p.left : p.right);
-
-
- if (replacement != null) {
-
- replacement.parent = p.parent;
-
- if (p.parent == null)
- root = replacement;
- else if (p == p.parent.left)
-
- p.parent.left = replacement;
- else
-
- p.parent.right = replacement;
-
-
- p.left = p.right = p.parent = null;
-
-
-
- if (p.color == BLACK)
-
- fixAfterDeletion(replacement);
- } else if (p.parent == null) {
- root = null;
- } else {
-
-
- if (p.color == BLACK)
-
- fixAfterDeletion(p);
-
-
- if (p.parent != null) {
- if (p == p.parent.left)
- p.parent.left = null;
- else if (p == p.parent.right)
- p.parent.right = null;
- p.parent = null;
- }
- }
- }
-
-
- private void fixAfterDeletion(Entry x) {
-
-
- while (x != root && colorOf(x) == BLACK) {
-
- if (x == leftOf(parentOf(x))) {
-
- Entry sib = rightOf(parentOf(x));
-
- if (colorOf(sib) == RED) {
-
-
- setColor(sib, BLACK);
- setColor(parentOf(x), RED);
- rotateLeft(parentOf(x));
- sib = rightOf(parentOf(x));
- }
-
- if (colorOf(leftOf(sib)) == BLACK && colorOf(rightOf(sib)) == BLACK) {
-
- setColor(sib, RED);
- x = parentOf(x);
- } else {
- if (colorOf(rightOf(sib)) == BLACK) {
-
- setColor(leftOf(sib), BLACK);
- setColor(sib, RED);
- rotateRight(sib);
- sib = rightOf(parentOf(x));
- }
-
- setColor(sib, colorOf(parentOf(x)));
- setColor(parentOf(x), BLACK);
- setColor(rightOf(sib), BLACK);
- rotateLeft(parentOf(x));
-
-
- x = root;
- }
- } else {
- Entry sib = leftOf(parentOf(x));
-
- if (colorOf(sib) == RED) {
- setColor(sib, BLACK);
- setColor(parentOf(x), RED);
- rotateRight(parentOf(x));
- sib = leftOf(parentOf(x));
- }
-
- if (colorOf(rightOf(sib)) == BLACK && colorOf(leftOf(sib)) == BLACK) {
- setColor(sib, RED);
- x = parentOf(x);
- } else {
- if (colorOf(leftOf(sib)) == BLACK) {
- setColor(rightOf(sib), BLACK);
- setColor(sib, RED);
- rotateLeft(sib);
- sib = leftOf(parentOf(x));
- }
- setColor(sib, colorOf(parentOf(x)));
- setColor(parentOf(x), BLACK);
- setColor(leftOf(sib), BLACK);
- rotateRight(parentOf(x));
- x = root;
- }
- }
- }
-
- setColor(x, BLACK);
- }
- }