提交 ffa58120 编写于 作者: H Hongze Cheng

more code

上级 c1462d72
...@@ -44,8 +44,9 @@ SRBTreeNode *tRBTreeGet(SRBTree *pTree, void *pKey); ...@@ -44,8 +44,9 @@ SRBTreeNode *tRBTreeGet(SRBTree *pTree, void *pKey);
SRBTreeNode *tRBTreeIterNext(SRBTreeIter *pIter); SRBTreeNode *tRBTreeIterNext(SRBTreeIter *pIter);
// STRUCT ============================================= // STRUCT =============================================
typedef enum { RED, BLACK } ECOLOR;
struct SRBTreeNode { struct SRBTreeNode {
enum { RED, BLACK } color; ECOLOR color;
SRBTreeNode *parent; SRBTreeNode *parent;
SRBTreeNode *left; SRBTreeNode *left;
SRBTreeNode *right; SRBTreeNode *right;
......
...@@ -18,46 +18,40 @@ ...@@ -18,46 +18,40 @@
#define RBTREE_NODE_COLOR(N) ((N) ? (N)->color : BLACK) #define RBTREE_NODE_COLOR(N) ((N) ? (N)->color : BLACK)
// SRBTree ================================================ // SRBTree ================================================
static void tRBTreeRotateLeft(SRBTree *pTree, SRBTreeNode *pNode) { static void tRBTreeRotateLeft(SRBTree *pTree, SRBTreeNode *x) {
SRBTreeNode *right = pNode->right; SRBTreeNode *y = x->right;
x->right = y->left;
pNode->right = right->left; if (y->left) {
if (pNode->right) { y->left->parent = x;
pNode->right->parent = pNode; }
} y->parent = x->parent;
if (x->parent == NULL) {
right->parent = pNode->parent; pTree->rootNode = y;
if (pNode->parent == NULL) { } else if (x == x->parent->left) {
pTree->rootNode = right; x->parent->left = y;
} else if (pNode == pNode->parent->left) {
pNode->parent->left = right;
} else { } else {
pNode->parent->right = right; x->parent->right = y;
} }
y->left = x;
right->left = pNode; x->parent = y;
pNode->parent = right;
} }
static void tRBTreeRotateRight(SRBTree *pTree, SRBTreeNode *pNode) { static void tRBTreeRotateRight(SRBTree *pTree, SRBTreeNode *x) {
SRBTreeNode *left = pNode->left; SRBTreeNode *y = x->left;
x->left = y->right;
pNode->left = left->right; if (y->right) {
if (pNode->left) { y->right->parent = x;
pNode->left->parent = pNode; }
} y->parent = x->parent;
if (x->parent == NULL) {
left->parent = pNode->parent; pTree->rootNode = y;
if (pNode->parent == NULL) { } else if (x == x->parent->left) {
pTree->rootNode = left; x->parent->left = y;
} else if (pNode == pNode->parent->left) {
pNode->parent->left = left;
} else { } else {
pNode->parent->right = left; x->parent->right = y;
} }
y->right = x;
left->right = pNode; x->parent = y;
pNode->parent = left;
} }
static SRBTreeNode *tRBTreeSuccessor(SRBTreeNode *pNode) { static SRBTreeNode *tRBTreeSuccessor(SRBTreeNode *pNode) {
...@@ -200,50 +194,114 @@ SRBTreeNode *tRBTreePut(SRBTree *pTree, SRBTreeNode *pNew) { ...@@ -200,50 +194,114 @@ SRBTreeNode *tRBTreePut(SRBTree *pTree, SRBTreeNode *pNew) {
return pNew; return pNew;
} }
void tRBTreeDrop(SRBTree *pTree, SRBTreeNode *pNode) { static void tRBTreeTransplant(SRBTree *pTree, SRBTreeNode *u, SRBTreeNode *v) {
// update min/max node if (u->parent == NULL) {
if (pTree->minNode == pNode) { pTree->rootNode = v;
pTree->minNode = tRBTreeSuccessor(pTree->minNode); } else if (u == u->parent->left) {
u->parent->left = v;
} else {
u->parent->right = v;
} }
if (pTree->maxNode == pNode) { if (v) {
pTree->maxNode = tRBTreePredecessor(pTree->maxNode); v->parent = u->parent;
} }
}
// drop impl static void tRBTreeDropFixup(SRBTree *t, SRBTreeNode *x) {
if (pNode->left == NULL) { while (x != t->rootNode && x->color == BLACK) {
// transplant right if (x == x->parent->left) {
if (pNode->parent == NULL) { SRBTreeNode *w = x->parent->right;
pTree->rootNode = pNode->right; if (RBTREE_NODE_COLOR(w) == RED) {
} else if (pNode == pNode->parent->left) { w->color = BLACK;
pNode->parent->left = pNode->right; x->parent->color = RED;
tRBTreeRotateLeft(t, x->parent);
w = x->parent->right;
}
if (RBTREE_NODE_COLOR(w->left) == BLACK && RBTREE_NODE_COLOR(w->right) == BLACK) {
w->color = RED;
x = x->parent;
} else { } else {
pNode->parent->right = pNode->right; if (RBTREE_NODE_COLOR(w->right) == BLACK) {
w->left->color = BLACK;
w->color = RED;
tRBTreeRotateRight(t, w);
w = x->parent->right;
}
w->color = x->parent->color;
x->parent->color = BLACK;
w->right->color = BLACK;
tRBTreeRotateLeft(t, x->parent);
x = t->rootNode;
} }
if (pNode->right) {
pNode->right->parent = pNode->parent;
}
} else if (pNode->right == NULL) {
// transplant left
if (pNode->parent == NULL) {
pTree->rootNode = pNode->left;
} else if (pNode == pNode->parent->left) {
pNode->parent->left = pNode->left;
} else { } else {
pNode->parent->right = pNode->left; SRBTreeNode *w = x->parent->left;
if (RBTREE_NODE_COLOR(w) == RED) {
w->color = BLACK;
x->parent->color = RED;
tRBTreeRotateRight(t, x->parent);
w = x->parent->left;
}
if (RBTREE_NODE_COLOR(w->right) == BLACK && RBTREE_NODE_COLOR(w->left) == BLACK) {
w->color = RED;
x = x->parent;
} else {
if (RBTREE_NODE_COLOR(w->left) == BLACK) {
w->right->color = BLACK;
w->color = RED;
tRBTreeRotateLeft(t, w);
w = x->parent->left;
}
w->color = x->parent->color;
x->parent->color = BLACK;
w->left->color = BLACK;
tRBTreeRotateRight(t, x->parent);
x = t->rootNode;
} }
}
}
x->color = BLACK;
}
if (pNode->left) { void tRBTreeDrop(SRBTree *t, SRBTreeNode *z) {
pNode->left->parent = pNode->parent; // update min/max node
if (t->minNode == z) {
t->minNode = tRBTreeSuccessor(t->minNode);
} }
if (t->maxNode == z) {
t->maxNode = tRBTreePredecessor(t->maxNode);
}
// drop impl
SRBTreeNode *y = z;
SRBTreeNode *x;
ECOLOR oColor = y->color;
if (z->left == NULL) {
x = z->right;
tRBTreeTransplant(t, z, z->right);
} else if (z->right == NULL) {
x = z->left;
tRBTreeTransplant(t, z, z->left);
} else {
y = tRBTreeSuccessor(z);
oColor = y->color;
x = y->right;
if (y->parent == z) {
x->parent = z;
} else { } else {
SRBTreeNode *y = tRBTreeSuccessor(pNode); tRBTreeTransplant(t, y, y->right);
pNode->color = RBTREE_NODE_COLOR(y); y->right = z->right;
y->right->parent = y;
}
tRBTreeTransplant(t, z, y);
y->left = z->left;
y->left->parent = y;
y->color = z->color;
} }
// fix // fix
if (pNode->color == BLACK) { if (oColor == BLACK) {
// TODO tRBTreeDropFixup(t, x);
} }
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册