public
Last active

Left Leaning Red/Black Tree implemented in Java

  • Download Gist
RBTree.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 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
package org.aspyct.going;
 
import java.util.Iterator;
import java.util.Stack;
 
/**
* This is an implementation of the Left Leaning Red/Black Tree as
* described in the course from the University of Princeton.
*
* This code contains a lot of assertions (assert ;).
* They are included only to make things as clear as possible.
* These can be safely removed.
*
* @author Antoine d'Otreppe <a.dotreppe@aspyct.org> @aspyct
*
*/
public class RBTree<K extends Comparable<K>, V> implements Iterable<RBTree<K, V>.Entry<K, V>>{
private static final boolean RED = true;
private static final boolean BLACK = false;
 
public abstract class Entry<K, V> {
public abstract K key();
public abstract V value();
}
 
private class Node<K, V> extends Entry<K, V> {
K key;
V value;
Node<K, V> left;
Node<K, V> right;
boolean color;
 
Node(K key, V value) {
this.key = key;
this.value = value;
this.color = RED;
}
 
public K key() {
return key;
}
 
public V value() {
return value;
}
}
 
private Node<K, V> root;
 
public void put(K key, V value) {
root = put(root, key, value);
root.color = BLACK;
}
 
private Node<K, V> put(Node<K, V> node, K key, V value) {
if (node == null) {
return new Node<K, V>(key, value);
}
// Inv: the current node is not a 4-node
if (isFourNode(node)) {
node = splitFourNode(node);
}
 
assert !isFourNode(node);
 
// Regular recursive BST insert
int cmp = key.compareTo(node.key);
if (cmp == 0) {
node.value = value;
}
else if (cmp < 0) {
node.left = put(node.left, key, value);
}
else /* if (cmp > 0) */ {
node.right = put(node.right, key, value);
}
 
// No right-leaning red branch please
if (isRed(node.right)) {
node = leanLeft(node);
}
 
assert !isRed(node.right);
 
if (node.left != null) {
assert node.left.key.compareTo(node.key) < 0;
}
 
if (node.right != null) {
assert node.right.key.compareTo(node.key) > 0;
}
 
assert node != null;
return node;
}
 
private boolean isFourNode(Node<K, V> node) {
return isRed(node.left) && isRed(node.left.left);
}
 
private boolean isRed(Node<K, V> node) {
if (node == null) {
return false;
}
else {
return node.color == RED;
}
}
 
private Node<K, V> splitFourNode(Node<K, V> node) {
/**
* Scenario: node is black
* node.left is red
* node.left.left is red
*/
assert !isRed(node);
assert isRed(node.left);
assert isRed(node.left.left);
 
node = rotateRight(node);
node.left.color = BLACK;
 
return node;
}
 
private Node<K, V> leanLeft(Node<K, V> node) {
/**
* Scenario: node.right is red
*/
assert isRed(node.right);
 
node = rotateLeft(node);
node.color = node.left.color;
node.left.color = RED;
 
return node;
}
 
private Node<K, V> rotateLeft(Node<K, V> node) {
Node<K, V> top = node.right;
node.right = top.left;
top.left = node;
return top;
}
 
private Node<K, V> rotateRight(Node<K, V> node) {
Node<K, V> top = node.left;
node.left = top.right;
top.right = node;
 
return top;
}
 
public Iterator<Entry<K, V>> iterator() {
return new InorderEntryIterator(root);
}
 
private class InorderEntryIterator implements Iterator<Entry<K, V>> {
Stack<Node<K, V>> stack;
 
InorderEntryIterator(Node<K, V> root) {
stack = new Stack<Node<K, V>>();
explore(root);
}
 
private void explore(Node<K, V> node) {
while (node != null) {
stack.push(node);
node = node.left;
}
}
 
public boolean hasNext() {
return !stack.empty();
}
 
public Entry<K, V> next() {
Node<K, V> node = stack.pop();
 
if (node.right != null) {
explore(node.right);
}
 
return node;
}
 
public void remove() {
throw new UnsupportedOperationException("Cannot remove nodes from here");
}
}
 
// java -ea org.aspyct.going.RBTree
public static void main(String[] args) {
RBTree<Character, Integer> tree = new RBTree<Character, Integer>();
 
/*
* c
* / \
*/
tree.put('c', 0);
 
assert tree.root != null;
assert tree.root.key == 'c';
assert tree.root.value == 0;
assert tree.root.color == BLACK;
 
/* Take a bigger key and check that the tree is rotated
*
* i
* / \
* c
* / \
*/
tree.put('i', 0);
 
assert tree.root.key == 'i';
assert tree.root.left.key == 'c';
 
assert tree.root.color == BLACK;
assert tree.root.left.color == RED;
 
assert tree.root.right == null;
assert tree.root.left.right == null;
assert tree.root.left.left == null;
 
/* Again, a bigger key, and check that the tree is still left
*
* v
* / \
* i
* / \
* c
* / \
*/
tree.put('v', 0);
 
assert tree.root.key == 'v';
assert tree.root.left.key == 'i';
assert tree.root.left.left.key == 'c';
 
assert tree.root.color == BLACK;
assert tree.root.left.color == RED;
assert tree.root.left.left.color == RED;
 
assert tree.root.right == null;
assert tree.root.left.right == null;
assert tree.root.left.left.right == null;
assert tree.root.left.left.left == null;
 
/* Last check, the four node must be split
* Try inserting something between i and v
*
*
* i
* / \
* c v
* / \ / \
* m
* / \
*/
tree.put('m', 0);
assert tree.root.key == 'i';
assert tree.root.right.key == 'v';
assert tree.root.left.key == 'c';
assert tree.root.right.left.key == 'm';
assert tree.root.color == BLACK;
assert tree.root.left.color == BLACK;
assert tree.root.right.color == BLACK;
assert tree.root.right.left.color == RED;
assert tree.root.left.left == null;
assert tree.root.left.right == null;
assert tree.root.right.left.left == null;
assert tree.root.right.left.right == null;
assert tree.root.right.right == null;
 
System.out.println("All good :)");
 
System.out.println("Inorder iterate");
for (RBTree<Character, Integer>.Entry<Character, Integer> entry: tree) {
System.out.print(entry.key().toString() + " ");
}
 
System.out.println();
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.