转自:http://blog.sina.com.cn/s/blog_537bb50a010000is.html
续二:
====红黑树操作的实现,插入、删除、查找 (二)====
private void leftRotate(Node node){
Node y;
y = node.rightChild;
node.rightChild = y.leftChild;
if(y.leftChild != nil) y.leftChild.parent = node;
y.parent = node.parent;
if (node.parent == nil) this.setRoot(y);
else if (node == node.parent.leftChild) node.parent.leftChild = y;
else node.parent.rightChild = y;
y.leftChild = node;
node.parent = y;
}
private void rightRotate(Node node){
Node y;
y = node.leftChild;
node.leftChild = y.rightChild;
if(y.rightChild != nil) y.rightChild.parent = node;
y.parent = node.parent;
if (node.parent == nil) this.setRoot(y);
else if (node == node.parent.leftChild) node.parent.leftChild = y;
else node.parent.rightChild = y;
y.rightChild = node;
node.parent = y;
}
private void rbInsertAdjust(Node node){
while(node.parent.color == false){
if(node.parent == node.parent.parent.leftChild){
Node y = node.parent.parent.rightChild;
if (y.color == false){
node.parent.color = true;
y.color = true;
node.parent.parent.color = false;
node = node.parent.parent;
}
else{
if (node == node.parent.rightChild){
node = node.parent;
leftRotate(node);
}
node.parent.color = true;
node.parent.parent.color = false;
rightRotate(node.parent.parent);
}
}
else{
Node y = node.parent.parent.leftChild;
if (y.color == false){
node.parent.color = true;
y.color = true;
node.parent.parent.color = false;
node = node.parent.parent;
}
else{
if (node == node.parent.leftChild){
node = node.parent;
rightRotate(node);
}
node.parent.color = true;
node.parent.parent.color = false;
leftRotate(node.parent.parent);
}
}
}
this.getRoot().color = true;
}
private Node getMin(Node node){
while(node.leftChild != nil)
node = node.leftChild;
return node;
}