public
Last active

Left-leaning Red-Black Tree (LLRB) - http://www.cs.princeton.edu/~rs/talks/LLRB/LLRB.pdf

  • Download Gist
LLRB.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
/**
* A "Left-leaning Red-Black Tree"
*
* http://www.cs.princeton.edu/~rs/talks/LLRB/LLRB.pdf
*/
public class LLRB<K extends Comparable<K>, V> {
 
public static void main(String[] args) {
LLRB<String, String> llrb = new LLRB<String, String>();
for (int i = 0; i < 100; i++) {
llrb.insert("Hello-" + i, "World-" + i);
}
System.out.println("GET #1: " + llrb.search("Hello-55"));
System.out.println("GET #2: " + llrb.search("Hello-100"));
llrb.delete("Hello-55");
System.out.println("GET #3: " + llrb.search("Hello-55"));
System.out.println("GET #4: " + llrb.search("Hello-56"));
}
private Node root = null;
public V search(K key) {
return search(root, key);
}
private V search(Node h, K key) {
while (h != null) {
int cmp = key.compareTo(h.key);
if (cmp == 0) {
return h.value;
} else if (cmp < 0) {
h = h.left;
} else {
h = h.right;
}
}
return null;
}
public void insert(K key, V value) {
root = insert(root, key, value);
root.color = Node.BLACK;
}
private Node insert(Node h, K key, V value) {
if (h == null) {
return new Node(key, value);
}
if (isRed(h.left) && isRed(h.right)) {
colorFlip(h);
}
int cmp = key.compareTo(h.key);
if (cmp == 0) {
h.value = value;
} else if (cmp < 0) {
h.left = insert(h.left, key, value);
} else {
h.right = insert(h.right, key, value);
}
if (isRed(h.right) && !isRed(h.left)) {
h = rotateLeft(h);
}
if (isRed(h.left) && isRed(h.left.left)) {
h = rotateRight(h);
}
return h;
}
public void deleteMin() {
root = deleteMin(root);
root.color = Node.BLACK;
}
private Node deleteMin(Node h) {
if (h.left == null) {
return null;
}
if (!isRed(h.left) && !isRed(h.left.left)) {
h = moveRedLeft(h);
}
h.left = deleteMin(h.left);
return fixUp(h);
}
public void delete(K key) {
root = delete(root, key);
root.color = Node.BLACK;
}
private Node delete(Node h, K key) {
if (key.compareTo(h.key) < 0) {
if (!isRed(h.left) && !isRed(h.left.left)) {
h = moveRedLeft(h);
}
h.left = delete(h.left, key);
} else {
if (isRed(h.left)) {
h = rotateRight(h);
}
if (key.compareTo(h.key) == 0 && h.right == null) {
return null;
}
if (!isRed(h.right) && !isRed(h.right.left)) {
h = moveRedRight(h);
}
if (key.compareTo(h.key) == 0) {
h.value = search(h.right, min(h.right).key);
h.key = min(h.right).key;
h.right = deleteMin(h.right);
} else {
h.right = delete(h.right, key);
}
}
return fixUp(h);
}
private Node moveRedLeft(Node h) {
colorFlip(h);
if (isRed(h.right.left)) {
h.right = rotateRight(h.right);
h = rotateLeft(h);
colorFlip(h);
}
return h;
}
private Node moveRedRight(Node h) {
colorFlip(h);
if (isRed(h.left.left)) {
h = rotateRight(h);
colorFlip(h);
}
return h;
}
private Node rotateLeft(Node h) {
Node x = h.right;
h.right = x.left;
x.left = h;
x.color = h.color;
h.color = Node.RED;
return x;
}
private Node rotateRight(Node h) {
Node x = h.left;
h.left = x.right;
x.right = h;
x.color = h.color;
h.color = Node.RED;
return x;
}
private boolean isRed(Node h) {
return h != null && h.color == Node.RED;
}
private void colorFlip(Node h) {
h.color = !h.color;
h.left.color = !h.left.color;
h.right.color = !h.right.color;
}
private Node min(Node h) {
while (h.left != null) {
h = h.left;
}
return h;
}
private Node fixUp(Node h) {
if (isRed(h.right)) {
h = rotateLeft(h);
}
if (h.left != null && isRed(h.left) && isRed(h.left.left)) {
h = rotateRight(h);
}
if (isRed(h.left) && isRed(h.right)) {
colorFlip(h);
}
return h;
}
private class Node {
public static final boolean RED = true;
private static final boolean BLACK = false;
private K key;
private V value;
private boolean color = RED;
private Node left = null;
private Node right = null;
public Node(K key, V value) {
this.key = key;
this.value = value;
}
}
}

The insert you have here is the insert described in the paper for the 2-3-4 tree; but the deletes are the deletes described for 2-3 trees. (The paper says to move the colorflip to the end of the insert to get a 2-3 tree.)

Is this a typo in the code or in the paper?

Correct me if I'm wrong, but I think that NullPointerException will be thrown on line 97 if the root is deleted, being the last node in the tree.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.