33,006
社区成员
发帖
与我相关
我的任务
分享
public class RedBlackBST<Key extends Comparable<Key>,Value>{
private Node root;
private static final boolean RED=true;
private static final boolean BLACK=false;
private class Node {
Key key;
Value val;
Node left,right;
int N;
boolean color;
Node(Key key,Value val,int N,boolean color) {
this.key=key;
this.val=val;
this.N=N;
this.color=color;
}
}
//判断节点x的连接是否为共色
private boolean isRed(Node x) {
if(x==null) return false;
return x.color==RED;
}
public boolean isEmpty() {
return root.N==0;
}
private int size(Node x) {
if(x==null) return 0;
return x.N;
}
//左旋转,将指向右链接的红色进行旋转
Node rotateLeft(Node h) {
Node x=h.right;
h.right=x.left;
x.left=h;
x.color=h.color;
h.color=RED;
x.N=h.N;
h.N=1+size(h.left)+size(h.right);
return x;
}
//右旋转
Node rotateRight(Node h) {
Node x=h.left;
h.left=x.right;
x.right=h;
x.color=h.color;
h.color=RED;
x.N=h.N;
h.N=1+size(h.left)+size(h.right);
return x;
}
private void flipColors(Node h) {
h.color=RED;
h.left.color=BLACK;
h.right.color=BLACK;
}
private Node moveRedLeft(Node h) {
/**
* 下面的这一行和接下来的判断是什么意思?
* */
flipColors(h);
if(isRed(h.right.left)) {
h.right=rotateRight(h.right);
h=rotateLeft(h);
}
return h;
}
public void deleteMin() {
if(!isRed(root.left)&&!isRed(root.right))
root.color=RED;
root=deleteMin(root);
if(!isEmpty()) root.color=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 balance(h);
}
private Node balance(Node h) {
if(isRed(h.right)) h=rotateLeft(h);
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)) flipColors(h);
h.N=size(h.left)+size(h.right)+1;
return h;
}
}
请问这个算法的moveRedLeft有什么意义?看的不是很明白??