void rbtree_insert(struct rbtree * tree, void * key, void * data) { struct rbtree_node * new_node = malloc(sizeof(struct rbtree_node)); new_node->left = tree->nil; new_node->right = tree->nil; new_node->color = RBTREE_RED; new_node->key = tree->dup_key(key); new_node->data = data; struct rbtree_node * current = tree->root, * parent = tree->nil; while(current != tree->nil) { parent = current; if(tree->compare_key(key, current->key) < 0) current = current->left; else current = current->right; } new_node->parent = parent; if(parent == tree->nil) tree->root = new_node; else if(tree->compare_key(key, parent->key) < 0) parent->left = new_node; else parent->right = new_node; insert_fixup(tree, new_node); }
void insert(node **root_ref,int key) { node *y = nil; node *x = *root_ref; node *z = new_node(key); while(x!=nil) { y = x; if(z->key < x->key) { x = x->left; } else { x = x->right; } } z->p = y; if(y==nil) { *root_ref = z; } else if(z->key < y->key) { y->left = z; } else { y->right = z; } z->left = nil; z->right = nil; z->color = 'R'; insert_fixup(root_ref,z); }
int insert_node(struct rbtree *rbt, struct rbnode *nd) { struct rbnode *tmp = &rbt->nil, *itor = rbt->root; //struct rbnode *nd = malloc(sizeof(struct rbnode)); nd->left = nd->right = nd->parent = NULL; //nd->key = key; while (itor != &rbt->nil) { tmp = itor; if ((rbt->c) (itor->key, nd->key) > 0) itor = itor->left; else itor = itor->right; } nd->parent = tmp; if (tmp == &rbt->nil) rbt->root = nd; else { if ((rbt->c) (tmp->key, nd->key) > 0) tmp->left = nd; else tmp->right = nd; } nd->left = nd->right = &rbt->nil; nd->color = RED; insert_fixup(rbt, nd); rbt->size++; return 0; }
int insert_node(struct rbtree *rbt, struct rbnode *pnd) { struct rbnode *tmp = &rbt->nil, *itor = rbt->root; struct rbnode *nd = malloc(sizeof(struct rbnode)); struct douint *p = pnd->key; if (nd == NULL) return -1; *nd = *pnd; //pthread_mutex_lock(&(rbt->lock)); while (itor != &rbt->nil) { tmp = itor; if ((rbt->c) (itor->key, nd->key, rbt->argv) > 0) itor = itor->left; else itor = itor->right; } nd->parent = tmp; if (tmp == &rbt->nil) rbt->root = nd; else { if ((rbt->c) (tmp->key, nd->key, rbt->argv) > 0) tmp->left = nd; else tmp->right = nd; } nd->left = nd->right = &rbt->nil; nd->color = RED; insert_fixup(rbt, nd); rbt->size++; //pthread_mutex_unlock(&(rbt->lock)); return 0; }
rbt_status rbt_insert( rbt_tree *tree, void *key, void *val ) { if ( tree == NULL ) { return RBT_STATUS_TREE_NOT_FOUND; } node_type *current, *parent, *x; tag_type *rbt = ( tag_type * )tree; // Allocate node for data and insert in tree, then find future parent current = rbt->root; parent = 0; while ( current != &rbt->sentinel ) { int rc = rbt->compare( key, current->key ); if ( rc == 0 ) { return RBT_STATUS_DUPLICATE_KEY; } parent = current; current = ( rc < 0 ) ? current->left : current->right; } // Setup new node if ( ( x = ( node_type * )malloc( sizeof( *x ) ) ) == 0 ) { return RBT_STATUS_MEM_EXHAUSTED; } x->parent = parent; x->left = &rbt->sentinel; x->right = &rbt->sentinel; x->color = RED; x->key = key; x->val = val; // Insert node in tree if ( parent ) { if ( rbt->compare( key, parent->key ) < 0 ) { parent->left = x; } else { parent->right = x; } } else { rbt->root = x; } insert_fixup( rbt, x ); return RBT_STATUS_OK; }
int tree_insert (TREE **root, void *tree, TREE_COMPARE *comp, Bool allow_duplicates) { TREE *current, *parent; int cmp = 0; /* find where node belongs */ current = *root; parent = NULL; while (current != TREE_NULL) { parent = current; cmp = (comp) (current, tree); if (cmp < 0) current = current-> right; else if (cmp > 0) current = current-> left; else { if (allow_duplicates) current = current-> left; else return TREE_DUPLICATE; } } /* setup new node */ ((TREE *) tree)-> parent = parent; ((TREE *) tree)-> left = TREE_NULL; ((TREE *) tree)-> right = TREE_NULL; ((TREE *) tree)-> colour = RED; /* insert node in tree */ if (parent) { if (cmp > 0) parent-> right = tree; else parent-> left = tree; } else *root = tree; insert_fixup (root, tree); return (TREE_OK); }
void balanced_pst::insert(const point &v) { node* z = new node(v); node* y = sentinel; node* x = root; while (x != sentinel) { y = x; if (z->key < x->key) x = x->left; else x = x->right; } z->p = y; if (y == sentinel) root = z; else if (z->key < y->key) y->left = z; else y->right = z; z->left = sentinel; z->right = sentinel; z->color = RED; insert_fixup(z); _size++; }
/* * tree_insert -- * Insert the item into the tree. * See section 13.3, "Introduction to Algorithms", 2/e for details * on the algorithm. */ void tree_insert(const T tree, Item item) { assert(tree); Node *x, *y, *z; /* * z is the inserted node */ z = malloc(sizeof *z); assert(z); z->item = item; /* * Traverse the tree from the root down to find a place for the * new node. */ y = NIL(tree); x = tree->root; while (x != NIL(tree)) { x->size++; // maintain the size field of each node y = x; if (LESS(KEY(item), KEY(x->item))) x = x->left; else x = x->right; } /* * now y is the parent of new inserted node z */ z->parent = y; if (y == NIL(tree)) tree->root = z; else if (LESS(KEY(z->item), KEY(y->item))) y->left = z; else y->right = z; z->left = z->right = NIL(tree); z->color = RED; z->size = 1; insert_fixup(tree, z); }
void insert(struct vertex **tree, int data) { struct vertex *new_vertex = malloc(sizeof(struct vertex)); new_vertex->data = data; new_vertex->height = 0; new_vertex->l = NULL; new_vertex->r = NULL; new_vertex->p = NULL; if (tree == NULL){ free(new_vertex); return; }else if (*tree == NULL) { *tree = new_vertex; return; } struct vertex *root = *tree; while (1) { if (root->data < data) { if (root->r != NULL) root = root->r; else { root->r = new_vertex; new_vertex->p = root; break; } } else { if (root->l != NULL) root = root->l; else { root->l = new_vertex; new_vertex->p = root; break; } } } insert_fixup(new_vertex); while ((*tree)->p != NULL) { (*tree) = (*tree)->p; } }
int pj_rbtree_insert( pj_rbtree *tree, pj_rbtree_node *element ) { int rv = 0; pj_rbtree_node *node, *parent = tree->null, *null = tree->null; pj_rbtree_comp *comp = tree->comp; PJ_CHECK_STACK(); node = tree->root; while (node != null) { rv = (*comp)(element->key, node->key); if (rv == 0) { /* found match, i.e. entry with equal key already exist */ return -1; } parent = node; node = rv < 0 ? node->left : node->right; } element->color = PJ_RBCOLOR_RED; element->left = element->right = null; node = element; if (parent != null) { node->parent = parent; if (rv < 0) parent->left = node; else parent->right = node; insert_fixup( tree, node); } else { tree->root = node; node->parent = null; node->color = PJ_RBCOLOR_BLACK; } ++tree->size; return 0; }
void RBTree::insert(int v) { if (root == pNil) { root = new_node(v); return; } rbnode * n = find_insert_pos(root, v); if (n->value == v) { return; } else { rbnode * c = new_node(v, RED); c->parent = n; if (v < n->value) { n->lc = c; } else { n->rc = c; } insert_fixup(c); } }
/** Insert an element into the tree * */ rbnode_t *rbtree_insert_node(rbtree_t *tree, void *data) { rbnode_t *current, *parent, *x; PTHREAD_MUTEX_LOCK(tree); /* find where node belongs */ current = tree->root; parent = NULL; while (current != NIL) { int result; /* * See if two entries are identical. */ result = tree->compare(data, current->data); if (result == 0) { /* * Don't replace the entry. */ if (!tree->replace) { PTHREAD_MUTEX_UNLOCK(tree); return NULL; } /* * Do replace the entry. */ if (tree->free) tree->free(current->data); current->data = data; PTHREAD_MUTEX_UNLOCK(tree); return current; } parent = current; current = (result < 0) ? current->left : current->right; } /* setup new node */ x = talloc_zero(tree, rbnode_t); if (!x) { fr_strerror_printf("No memory for new rbtree node"); PTHREAD_MUTEX_UNLOCK(tree); return NULL; } x->data = data; x->parent = parent; x->left = NIL; x->right = NIL; x->colour = RED; /* insert node in tree */ if (parent) { if (tree->compare(data, parent->data) <= 0) { parent->left = x; } else { parent->right = x; } } else { tree->root = x; } insert_fixup(tree, x); tree->num_elements++; PTHREAD_MUTEX_UNLOCK(tree); return x; }
/* * tree_join -- * Return a new tree with items from tree1, item and tree2 assuming * that * key[any item in tree1] <= key[item] <= key[any item in tree2]. */ T tree_join(T tree1, Item item, T tree2) { assert(tree1 != NULL && tree2 != NULL); Node *y; int cur_bh; if (tree1->bh >= tree2->bh) { /* * Find a black node y in tree1 with the largest key and whose * black-height is tree2->bh. */ cur_bh = tree1->bh; y = tree1->root; while (y != NIL(tree1)) { if (cur_bh == tree2->bh && y->color == BLACK) break; y->size += tree2->root->size; y = y->right; if (y->color == BLACK) cur_bh--; } /* * Create a new node x to replace y. y becomes the left * child of x and tree2 becomes the right child of x. */ Node *x = malloc(sizeof(*x)); assert(x); x->color = RED; x->item = item; x->size = y->size + tree2->root->size + 1; x->left = y; x->right = tree2->root; y->parent->right = x; x->parent = y->parent; if (y->parent == NIL(tree1)) { tree1->root = x; x->color = BLACK; tree1->bh++; } y->parent = x; tree2->root->parent = x; free(tree2); insert_fixup(tree1, x); return tree1; } else { /* * Find a black node y in tree2 with the smallest key and whose * black-height is tree1->bh. */ cur_bh = tree2->bh; y = tree2->root; while (y != NIL(tree2)) { if (cur_bh == tree1->bh && y->color == BLACK) break; y->size += tree1->root->size; y = y->left; if (y->color == BLACK) cur_bh--; } /* * Create a new node x to replace y. y becomes the right; * child of x and tree1 becomes the left child of x. */ Node *x = malloc(sizeof(*x)); assert(x); x->color = RED; x->item = item; x->size = y->size + tree1->root->size + 1; x->left = tree1->root; x->right = y; y->parent->left = x; x->parent = y->parent; if (y->parent == NIL(tree2)) { tree2->root = x; x->color = BLACK; tree2->bh++; } y->parent = x; tree1->root->parent = x; free(tree1); insert_fixup(tree2, x); return tree2; } }