Left-leaning Red-Black Trees
June 2, 2014 Leave a comment
I think this is the easiest algorithm to learn and code. Robert Sedgewick’s Java code is always succinct. I have learnt how to code Tries based on his code. Now his Red-black tree code is easy to remember.
http://www.cs.princeton.edu/~rs/talks/LLRB/LLRB.pdf
Though the properties of this type of tree are different from the original Red-black tree I think it is sufficient to remember this condensed code.
These are the three lines and some small methods that we should remember mainly.
if ( isRed(h.right) && !isRed(h.left)) h = rotateLeft(h); if ( isRed(h.left) && isRed(h.left.left)) h = rotateRight(h); if ( isRed(h.left) && isRed(h.right)) colorFlip(h);
Original binary tree before rotation
Final tree
I think the fundamental rules are these apart from the other Red-black tree rules.
- Disallow right-leaning red nodes.
- Disallow more than two consecutive red nodes.