虽然有点懂了,但是还是会忘记;理解的深度会与记忆的时间成正比。但是会让自己意识到在自己的“圈子”之外还有这么奇妙的设计。
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> #include <deque> #include <iostream> using namespace std; typedef int key_t; typedef int data_t; typedef enum color_t { RED = 0, BLACK = 1 }color_t; typedef struct rb_node_t { struct rb_node_t *left, *right, *parent; key_t key; data_t data; color_t color; }rb_node_t; static rb_node_t* rb_new_node(key_t key, data_t data) { rb_node_t *node = (rb_node_t*)malloc(sizeof(struct rb_node_t)); if (!node) { printf("malloc error!\n"); exit(-1); } node->key = key, node->data = data; return node; } /*----------------------------------------------------------- 左旋:在 根-左-右, 根-右-右, 根-右-左的情况下都会旋转 -----------------------------------------------------------*/ static rb_node_t* rb_rotate_left(rb_node_t* node, rb_node_t* root) { rb_node_t* right = node->right; if ((node->right = right->left)) { right->left->parent = node; } right->left = node; if ((right->parent = node->parent)) { //? 根右左变为根右右后的情况下 if (node == node->parent->right) { node->parent->right = right; } //根-左-右 情况下 else { node->parent->left = right; } } //根-右-右 根结点(顶点)被旋转情况下 else { root = right; } node->parent = right; return root; } /*----------------------------------------------------------- -----------------------------------------------------------*/ static rb_node_t* rb_rotate_right(rb_node_t* node, rb_node_t* root) { rb_node_t* left = node->left; if ((node->left = left->right)) { left->right->parent = node; } left->right = node; if ((left->parent = node->parent)) { if (node == node->parent->right) { node->parent->right = left; } else { node->parent->left = left; } } else { root = left; } node->parent = left; return root; } static rb_node_t* rb_insert_rebalance(rb_node_t *node, rb_node_t *root) { rb_node_t *parent, *gparent, *uncle, *tmp; //父结点为红时进入(父结点为黑时,不用任何操作) while ((parent = node->parent) && parent->color == RED) { gparent = parent->parent; //父结点在左 if (parent == gparent->left) { uncle = gparent->right; //叔结点为红 if (uncle && uncle->color == RED) { uncle->color = BLACK; parent->color = BLACK; gparent->color = RED; node = gparent; } //叔结点为黑 else { //根-左-右结构 if (parent->right == node) { root = rb_rotate_left(parent, root); tmp = parent; parent = node; node = tmp; } //根-左-左结构(根-左-右结构 经过左旋后) parent->color = BLACK; gparent->color = RED; root = rb_rotate_right(gparent, root); } } //父结点在右 else { uncle = gparent->left; //叔结点为红,涂色和上面一样 if (uncle && uncle->color == RED) { uncle->color = BLACK; parent->color = BLACK; gparent->color = RED; node = gparent; } //叔结点为黑 else { //根-右-左结构 if (parent->left == node) { root = rb_rotate_right(parent, root); tmp = parent; parent = node; node = tmp; } //根-右-右结构(根-右-左结构 经过右旋后) parent->color = BLACK; gparent->color = RED; root = rb_rotate_left(gparent, root); } } } root->color = BLACK; return root; } //parent是曾经被删除(取代)的父结点 //node是曾经被删除(取代)的子结点,现在的“目标”结点,取代之前的“目标”结点 //最多可能发生3次旋转 static rb_node_t* rb_erase_rebalance(rb_node_t *node, rb_node_t *parent, rb_node_t *root) { rb_node_t *other, *o_left, *o_right; //node结点为NULL或为黑色,且不是根节点 while ((!node || node->color == BLACK) && node != root) { //node为左子结点 if (parent->left == node) { //other为兄弟结点 other = parent->right; //兄弟结点为红 if (other->color == RED) { other->color = BLACK; parent->color = RED; root = rb_rotate_left(parent, root); other = parent->right; } //兄弟结点为黑,兄弟结点的左右子树为空或都是黑色的 if ((!other->left || other->left->color == BLACK) && (!other->right || other->right->color == BLACK)) { other->color = RED; //node结点和parent结点会向上移动,如果调整没有结束会继续调整 node = parent; parent = node->parent; } //兄弟结点为黑,兄弟的子结点待定 else { //兄弟的右子结点为NULL 或 为黑色(兄弟的左子树为红,上一级if已经排除了黑色或NULL) if (!other->right || other->right->color == BLACK) { //如果兄弟的左子结点有值,涂为黑色 if ((o_left = other->left)) { o_left->color = BLACK; } //兄弟结点涂为红色 other->color = RED; root = rb_rotate_right(other, root); other = parent->right; } //兄弟的右子结点为红色 (兄弟的左子结点为红色?) other->color = parent->color; parent->color = BLACK; if (other->right) { other->right->color = BLACK; } root = rb_rotate_left(parent, root); node = root; break; } } else { other = parent->left; if (other->color == RED) { other->color = BLACK; parent->color = RED; root = rb_rotate_right(parent, root); other = parent->left; } if ((!other->left || other->left->color == BLACK) && (!other->right || other->right->color == BLACK)) { other->color = RED; node = parent; parent = node->parent; } else { if (!other->left || other->left->color == BLACK) { if ((o_right = other->right)) { o_right->color = BLACK; } other->color = RED; root = rb_rotate_left(other, root); other = parent->left; } other->color = parent->color; parent->color = BLACK; if (other->left) { other->left->color = BLACK; } root = rb_rotate_right(parent, root); node = root; break; } } } if (node) { node->color = BLACK; } return root; } //save: 新(搜索)结点的parent return: 搜索到的结点指针 static rb_node_t* rb_search_auxiliary(key_t key, rb_node_t* root, rb_node_t** save) { rb_node_t *node = root, *parent = NULL; int ret; while (node) { parent = node; ret = node->key - key; if (0 < ret) { node = node->left; } else if (0 > ret) { node = node->right; } else { return node; } } if (save) { *save = parent; } return NULL; } rb_node_t* rb_insert(key_t key, data_t data, rb_node_t* root) { //parent: 新加入结点的parent; node: 新的结点 rb_node_t *parent = NULL, *node; parent = NULL; //取得新加入结点的parent;root值不会变 if ((node = rb_search_auxiliary(key, root, &parent))) { return root; } //对新结点赋值,插入(attach) node = rb_new_node(key, data); node->parent = parent; node->left = node->right = NULL; node->color = RED; if (parent) { if (parent->key > key) { parent->left = node; } else { parent->right = node; } } else { root = node; } return rb_insert_rebalance(node, root); } rb_node_t* rb_search(key_t key, rb_node_t* root) { return rb_search_auxiliary(key, root, NULL); } rb_node_t* rb_erase(key_t key, rb_node_t *root) { rb_node_t *child, *parent, *old, *left, *node; color_t color; if (!(node = rb_search_auxiliary(key, root, NULL))) { printf("key %d is not exist!\n"); return root; } old = node; if (node->left && node->right) { node = node->right; //找到node右子树的最小值 while ((left = node->left) != NULL) { node = left; } //替代值(将替代删除结点) 赋值 给child,parent,color child = node->right; parent = node->parent; color = node->color; if (child) { child->parent = parent; } //清除"取代值" if (parent) { if (parent->left == node) { parent->left = child; } else { parent->right = child; } } else { root = child; } if (node->parent == old) { parent = node; } //(完全)取代 node->parent = old->parent; node->color = old->color; node->right = old->right; node->left = old->left; if (old->parent) { if (old->parent->left == old) { old->parent->left = node; } else { old->parent->right = node; } } else { root = node; } //原左子树建立与新结点链接 old->left->parent = node; if (old->right) { old->right->parent = node; } } else { if (!node->left) { child = node->right; } else if (!node->right) { child = node->left; } parent = node->parent; color = node->color; if (child) { child->parent = parent; } if (parent) { if (parent->left == node) { parent->left = child; } else { parent->right = child; } } else { root = child; } } free(old); if (color == BLACK) { root = rb_erase_rebalance(child, parent, root); } return root; } void level_traverse(rb_node_t* root) { if(root == NULL) { return; } deque<rb_node_t *> dequesTree; dequesTree.push_back(root); while(dequesTree.size()) { rb_node_t *p = dequesTree.front(); dequesTree.pop_front(); cout << (p->key)<<" "; if(p->left) { dequesTree.push_back(p->left); } if(p->right) { dequesTree.push_back(p->right); } } cout<<endl; } int main() { int i, count = 90; key_t key; rb_node_t* root = NULL, *node = NULL; srand(time(NULL)); int array[] = {16,3,7,11,9,26,18,14,15,17}; //for (i = 1; i < count; ++i) for (i = 0; i < 10; ++i) { root = rb_insert(array[i], i, root); /* key = rand() % count; if ((root = rb_insert(key, i, root))) { printf("[i = %d] insert key %d success!\n", i, key); } else { printf("[i = %d] insert key %d error!\n", i, key); exit(-1); } if ((node = rb_search(key, root))) { printf("[i = %d] search key %d success!\n", i, key); } else { printf("[i = %d] search key %d error!\n", i, key); exit(-1); } if (!(i % 10)) { if ((root = rb_erase(key, root))) { printf("[i = %d] erase key %d success\n", i, key); } else { printf("[i = %d] erase key %d error\n", i, key); } } */ } level_traverse(root); /* for (i = 0; i < 10; ++i) { root = rb_erase(array[i],root); } */ root = rb_erase(3,root); //将发生3次旋转 level_traverse(root); return 0; }