As the name says, red-black trees created using modified binary search trees have an arbitrary restriction that all red links are left-handed. I made a basic program for red black trees (java), and I am curious how to change this, because it seems that it will be simple, but all the solutions elude me.
My code is close to what is in this PDF file:
http://www.cs.princeton.edu/~rs/talks/LLRB/LLRB.pdf
For example, here is the treeRepair method on the left side:
private TreeNode<Key, Value> treeRepair(TreeNode<Key, Value> node)
{
if (isRed(node.right) && !isRed(node.left)) node = rotateLeft(node);
if (isRed(node.left) && isRed(node.left.left)) node = rotateRight(node);
if (isRed(node.left) && isRed(node.right)) flipColours(node);
node.size = 1 + size(node.left) + size(node.right);
return node;
}
This works great. It starts at the end of each insert and delete. There is a modified version with a flip that actually works for a while.
private TreeNode<Key, Value> treeRepair(TreeNode<Key, Value> node)
{
if (isRed(node.left) && !isRed(node.right)) node = rotateRight(node);
if (isRed(node.right) && isRed(node.right.right)) node = rotateLeft(node);
if (isRed(node.left) && isRed(node.right)) flipColours(node);
node.size = 1 + size(node.left) + size(node.right);
return node;
}
, ( , , .), NPE.
, , .