rb_node_t *rb_insert(key_t key, data_t data, rb_node_t *root) { rb_node_t *parent = NULL; rb_node_t *node = NULL; parent = NULL; if ((node = rb_search_auxiliary(key, root, &parent))) { return root; } 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_insert(key_t key, data_t data, rb_node_t* root) { rb_node_t *parent = NULL, *node; parent = NULL; if ((node = rb_search_parent(key, root, &parent))) //调用rb_search_auxiliary找到插入结点的地方 { return root; } 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_fixup(node, root); //插入结点后,调用rb_insert_rebalance修复红黑树的性质 }
static rb_tree *rb_new_tree() { rb_tree *tree = (rb_tree *)malloc(sizeof(struct rb_tree)); if (!tree) { printf("mallock error!"); exit(1); } rb_node_t *nil = rb_new_node(NULL, NULL); nil->color = BLACK; //nil node has BLACK color tree->nil = nil; //set tree nil tree->root = nil; //init root as nil return tree; }
int main (int argc, const char * argv[]) { int i = 0; int count = 100000; key_t key; rb_node_t *root = NULL; rb_tree *tree = rb_new_tree(); srand(time(NULL)); for (i = 1; i < count; i++) { key = rand() % count; rb_node_t *node = rb_new_node(key, i); if (rb_insert(tree,node) != tree->nil) { printf("[i = %d] insert key %d success!\n",i,key); } else { printf("[i = %d] insert key %d failed!\n",i,key); exit(1); } if (rb_search(tree, key) != tree->nil) { printf("[i = %d] search key %d success!\n", i, key); }else { exit(1); } if (!(i%10)) { if (rb_delete(tree, node)) { printf("[i = %d] delete key %d success!\n", i, key); } else { printf("[i = %d] delete key %d failed!\n", i, key); } } } printf("Hello, World!\n"); return 0; }
extern int rb_insert(node_t **rootaddr, PyObject *key, PyObject *value) { int new_node = 0; node_t *root = *rootaddr; if (root == NULL) { // case 1, root == NULL root = rb_new_node(NULL, key, value); new_node = 1; if (root == NULL) return -1; // got no memory } else { node_t head; /* False tree root */ node_t *g, *t; /* Grandparent & parent */ node_t *p, *q; /* Iterator & parent */ int dir = 0; int last = 0; /* Set up our helpers */ t = &head; g = NULL; p = NULL; RIGHT_NODE(t) = root; LEFT_NODE(t) = NULL; q = RIGHT_NODE(t); /* Search down the tree for a place to insert */ for (;;) { int cmp_res; if (q == NULL) { /* Insert a new node at the first null link */ q = rb_new_node(p, key, value); new_node = 1; p->link[dir] = q; if (q == NULL) return -1; // get no memory } else if (is_red(q->link[0]) && is_red(q->link[1])) { /* Simple red violation: color flip */ RED(q) = 1; RED(q->link[0]) = 0; RED(q->link[1]) = 0; } if (is_red(q) && is_red(p)) { /* Hard red violation: rotations necessary */ int dir2 = (t->link[1] == g); if (q == p->link[last]) { node_t * tmp = t->link[dir2] = rb_single(g, !last); PARENT_NODE(tmp) = t; } else { node_t *tmp = t->link[dir2] = rb_double(g, !last); PARENT_NODE(tmp) = t; } } /* Stop working if we inserted a new node. */ if (new_node) break; cmp_res = ct_compare(KEY(q), key); if (cmp_res == 0) { /* if key exists */ // Py_XDECREF(VALUE(q)); /* release old value object */ // VALUE(q) = value; set new value object // Py_INCREF(value); /* take new value object */ break; } last = dir; dir = (cmp_res < 0); /* Move the helpers down */ if (g != NULL) t = g; g = p; p = q; q = q->link[dir]; } /* Update the root (it may be different) */ root = head.link[1]; } /* Make the root black for simplified logic */ RED(root) = 0; (*rootaddr) = root; PARENT_NODE(root) = NULL; return new_node; }
extern int rb_insert(node_t **rootaddr, PyObject *key, PyObject *value) { node_t *root = *rootaddr; if (root == NULL) { root = rb_new_node(key, value); if (root == NULL) return -1; } else { node_t head; node_t *g, *t; node_t *p, *q; int dir = 0; int last = 0; int new_node = 0; t = &head; g = NULL; p = NULL; RIGHT_NODE(t) = root; LEFT_NODE(t) = NULL; q = RIGHT_NODE(t); for (;;) { int cmp_res; if (q == NULL) { q = rb_new_node(key, value); p->link[dir] = q; new_node = 1; if (q == NULL) return -1; } else if (is_red(q->link[0]) && is_red(q->link[1])) { RED(q) = 1; RED(q->link[0]) = 0; RED(q->link[1]) = 0; } if (is_red(q) && is_red(p)) { int dir2 = (t->link[1] == g); if (q == p->link[last]) t->link[dir2] = rb_single(g, !last); else t->link[dir2] = rb_double(g, !last); } if (new_node) break; cmp_res = ct_compare(KEY(q), key); if (cmp_res == 0) { Py_XDECREF(VALUE(q)); VALUE(q) = value; Py_INCREF(value); return 0; } last = dir; dir = (cmp_res < 0); if (g != NULL) t = g; g = p; p = q; q = q->link[dir]; } root = head.link[1]; } RED(root) = 0; (*rootaddr) = root; return 1; }