rbtree_node_t *rbtree_insert(rbtree_t *tree, rbtree_key_t key) { rbtree_node_t *node, *parent; node = tree->root; parent = tree->sentinel; while (node != tree->sentinel) { parent = node; if (key <= node->key) node = node->left; else node = node->right; } node = rbtree_node_create(key, tree->sentinel); if (node) { node->parent = parent; if (parent == tree->sentinel) tree->root = node; else if (node->key <= parent->key) parent->left = node; else parent->right = node; rbtree_set_red(node); rbtree_insert_fixup(tree, node); } return node; }
/* * 添加节点:将节点(node)插入到红黑树中 * * 参数说明: * root 红黑树的根 * node 插入的结点 // 对应《算法导论》中的z */ static void rbtree_insert(RBRoot *root, Node *node) { Node *y = NULL; Node *x = root->node; // 1. 将红黑树当作一颗二叉查找树,将节点添加到二叉查找树中。 while (x != NULL) { y = x; if (node->key < x->key) x = x->left; else x = x->right; } rb_parent(node) = y; if (y != NULL) { if (node->key < y->key) y->left = node; // 情况2:若“node所包含的值” < “y所包含的值”,则将node设为“y的左孩子” else y->right = node; // 情况3:(“node所包含的值” >= “y所包含的值”)将node设为“y的右孩子” } else { root->node = node; // 情况1:若y是空节点,则将node设为根 } // 2. 设置节点的颜色为红色 node->color = RED; // 3. 将它重新修正为一颗二叉查找树 rbtree_insert_fixup(root, node); }
rb_node *insert_predecessor_at(rb_tree * tree, rb_node * at_node, datatype object) { rb_node *parent; rb_node *new_node; if (!(tree->root)) { /* Assign a new root node (the root is always * black) */ new_node = rbnode_construct(object, black); if (!new_node) return NULL; tree->root = new_node; tree->isize = 1; return new_node; } /* Insert the new object as a red leaf, being the predecessor * of at_node */ new_node = rbnode_construct(object, red); if (!new_node) return NULL; if (!at_node) { /* The new node should become the tree maximum. Place * is as the right child of the current maximal leaf */ parent = rbnode_maximum(tree->root); parent->right = new_node; } else { /* Make sure the insertion does not violate the tree * order In case given node has no left child, place * the new node as its left child. Otherwise, place it * at the rightmost position at the sub-tree rooted at * its left side. */ if (!(at_node->left)) { parent = at_node; parent->left = new_node; } else { parent = rbnode_maximum(at_node->left); parent->right = new_node; } } new_node->parent = parent; /* Mark that a new node was added */ tree->isize++; /* Fix the tree properties */ rbtree_insert_fixup(tree, new_node); return new_node; }
/* * Inserts a node into a red black tree. * * Returns NULL on failure or the pointer to the newly added node * otherwise. */ rbnode_t * rbtree_insert (rbtree_t *rbtree, rbnode_t *data) { /* XXX Not necessary, but keeps compiler quiet... */ int r = 0; /* We start at the root of the tree */ rbnode_t *node = rbtree->root; rbnode_t *parent = RBTREE_NULL; /* Lets find the new parent... */ while (node != RBTREE_NULL) { /* Compare two keys, do we have a duplicate? */ if ((r = rbtree->cmp(data->key, node->key)) == 0) { return NULL; } parent = node; if (r < 0) { node = node->left; } else { node = node->right; } } /* Initialize the new node */ data->parent = parent; data->left = data->right = RBTREE_NULL; data->color = RED; rbtree->count++; /* Insert it into the tree... */ if (parent != RBTREE_NULL) { if (r < 0) { parent->left = data; } else { parent->right = data; } } else { rbtree->root = data; } /* Fix up the red-black properties... */ rbtree_insert_fixup(rbtree, data); return data; }
/*Insert*/ void mln_sarbt_insert(mln_sarbt_t *t, mln_sarbt_node_t *n) { mln_sarbt_node_t *y = &(t->nil); mln_sarbt_node_t *x = t->root; while (x != &(t->nil)) { y = x; if (t->cmp(n->data, x->data) < 0) x = x->left; else x = x->right; } n->parent = y; if (y == &(t->nil)) t->root = n; else if (t->cmp(n->data, y->data) < 0) y->left = n; else y->right = n; n->left = &(t->nil); n->right = &(t->nil); n->color = M_SARB_RED; rbtree_insert_fixup(t, n); }
void util_rbtree_insert(util_rbtree_t *rbtree, util_rbtree_node_t *node) { util_rbtree_node_t *x, *y; if((rbtree==NULL) || (node==NULL) || (node==_NULL(rbtree))) { return; } /* the tree is empty */ if(rbtree->root == _NULL(rbtree)) { rbtree->root = node; node->parent = _NULL(rbtree); } else /* find the insert position */ { x = rbtree->root; while(x != _NULL(rbtree)) { y = x; if(node->key < x->key) x = x->left; else x = x->right; } /* now y is node's parent */ node->parent = y; if(node->key < y->key) y->left = node; else y->right = node; } /* initialize node's link & color */ node->left = _NULL(rbtree); node->right = _NULL(rbtree); util_rbt_red(node); /* fix up insert */ rbtree_insert_fixup(rbtree, node); rbtree->size++; }
rb_node *rbtree_insert(rb_tree * tree, datatype object) { rb_node *cur_node; rb_node *new_node; int comp_result = 0; if (!(tree->root)) { /* Assign a new root node (the root is always * black) */ new_node = rbnode_construct(object, black); if (!new_node) return NULL; tree->root = new_node; tree->isize = 1; return new_node; } /* Find a spot for the new object, insert the object as a red * leaf */ cur_node = tree->root; new_node = rbnode_construct(object, red); if (!new_node) return NULL; while (cur_node) { /* Compare inserted object with the object stored in * the current node */ comp_result = COMP_NODES(object, cur_node->object); if (comp_result == 0) { printf ("Attempted to insert duplicate node, aborting\n"); free(new_node); return NULL; } if (comp_result > 0) { if (!(cur_node->left)) { /* Insert the new leaf as the left * child of the current node */ cur_node->left = new_node; new_node->parent = cur_node; cur_node = NULL; /* Terminate the while loop */ } else { /* Go to the left subtree */ cur_node = cur_node->left; } } else { if (!(cur_node->right)) { /* Insert the new leaf as the right * child of the current node */ cur_node->right = new_node; new_node->parent = cur_node; cur_node = NULL; /* Terminate the while loop */ } else { /* Go to the right subtree */ cur_node = cur_node->right; } } } /* Mark the fact that a new node was added */ tree->isize++; /* Fix the tree properties */ rbtree_insert_fixup(tree, new_node); return new_node; }
/*! * Insert a new object to the tree as the a predecessor of a given node * * \param tree The tree * * \return The new node */ rbnode_t * rbtree_insert_predecessor_object_at ( rbtree_t * tree, rbnode_t * at_node, void * object ) { rbnode_t * parent; rbnode_t * new_node; if (!(tree->root)) { /* Assign a new root node. Notice that the root is always black */ new_node = rbnode_create(object, RB_BLACK); if (!new_node) return NULL; tree->root = new_node; tree->size = 1; new_node->tree = tree; return new_node; } /* Insert the new object as a red leaf, being the predecessor of at_node */ new_node = rbnode_create(object, RB_RED); if (!new_node) return NULL; if (!at_node) { /* The new node should become the tree maximum: Place is as the right * child of the current maximal leaf. */ parent = rbnode_maximum(tree->root); parent->right = new_node; } else { /* Make sure the insertion does not violate the tree order */ /* In case given node has no left child, place the new node as its * left child. Otherwise, place it at the rightmost position at the * sub-tree rooted at its left side. */ if (!(at_node->left)) { parent = at_node; parent->left = new_node; } else { parent = rbnode_maximum (at_node->left); parent->right = new_node; } } new_node->parent = parent; /* Mark that a new node was added */ tree->size++; /* Fix up the tree properties */ rbtree_insert_fixup(tree, new_node); new_node->tree = tree; return new_node; }
/*! * Insert an object to the tree [takes O(log n) operations] * * \param tree The tree * * \param object The object to be inserted * * \return the inserted object node */ rbnode_t * rbtree_insert_object ( rbtree_t * tree, void * object ) { rbnode_t * cur_node; rbnode_t * new_node; if (!(tree->root)) { /* * Assign a new root node. * Notice that the root is always black */ new_node = rbnode_create(object, RB_BLACK); if (!new_node) return NULL; tree->root = new_node; tree->size = 1; new_node->tree = tree; return new_node; } /* Find a place for the new object, and insert it as a red leaf */ cur_node = tree->root; new_node = rbnode_create(object, RB_RED); if (!new_node) return NULL; while (cur_node) { /* * Compare inserted object with the object stored * in the current node */ if ((tree->compare)(object, cur_node->object) > 0) { if (!(cur_node->left)) { /* * Insert the new leaf as the left * child of the current node */ cur_node->left = new_node; new_node->parent = cur_node; /* Terminate the while loop */ cur_node = NULL; } else { /* Go to the left sub-tree */ cur_node = cur_node->left; } } else { if (!(cur_node->right)) { /* * Insert the new leaf as the right * child of the current node */ cur_node->right = new_node; new_node->parent = cur_node; /* Terminate the while loop */ cur_node = NULL; } else { /* Go to the right sub-tree */ cur_node = cur_node->right; } } } /* Mark that a new node was added */ tree->size++; /* Fix up the tree properties */ rbtree_insert_fixup(tree, new_node); new_node->tree = tree; return new_node; }