示例#1
0
rb_node_t *rb_insert(rb_tree *tree, rb_node_t *z) {
    rb_node_t *y = tree->nil;
    rb_node_t *x = tree->root;
    while (x != tree->nil) {
        y = x;
        if (z->key < x->key) {
            x = x->left;
        } else {
            x = x->right;
        }
    }
    z->parent = y;
    
    if (y == tree->nil) {
        tree->root = z;
    } else {
        if (z->key < y->key) {
            y->left = z;
        } else {
            y->right = z;
        }
    }
    z->left = tree->nil;
    z->right = tree->nil;
    z->color = RED;
    rb_insert_fixup(tree, z);
    return tree->root;
}
示例#2
0
void
rb_insert(RB_TREE *T, RB_NODE *z)
{
    RB_NODE     *x, *y;

    y = T->nil;
    x = T->root;
    while (x != T->nil) {
        y = x;
        if (z->key < x->key)
            x = x->left;
        else
            x = x->right;
    }
    z->parent = y;
    if (y == T->nil)
        T->root = z;
    else if (z->key < y->key)
        y->left = z;
    else
        y->right = z;
    z->left = T->nil;
    z->right = T->nil;
    z->color = RED;
    rb_insert_fixup(T, z);
}
示例#3
0
文件: RBTree.c 项目: mrmign/algorithm
void rb_insert(RBTreeNode **root, RBTreeNode *z)
{
	RBTreeNode *y = NULL;
	RBTreeNode *x = *root;
	while (x)
	{
		y = x;
		if (z->key < x->key)
			x = x->left;
		else
			x = x->right;
	}

	z->parent = y;
	if (!y)
	{
		*root = z;
	}
	else if (z->key < y->key)
		y->left = z;
	else
		y->right = z;

	z->left = NULL;
	z->right = NULL;
	z->color = 'R';
	rb_insert_fixup(root, &z);
}
示例#4
0
	iterator insert(Type value){
		rb_node *y=_nil;
		rb_node *z=new rb_node;            //create a node by the value
		//needn't set the z's color ,because red is rb_node's default color
		z->_value=value;
		z->_left=_nil;
		z->_right=_nil;

		rb_node* x=_root;                                      //x iterator from _root
		while(x !=_nil )
		{
			y=x;
			if(x->_value< z->_value)
				x=x->_right;
			else
				x=x->_left;
		}
		z->_parent=y;
		if(y==_nil)       //determine z should be y's left or right
			_root=z;
		else
			if(y->_value < z->_value)
				y->_right=z;
			else
				y->_left=z;
		
		rb_insert_fixup(z);   //restore the red black properties
		return z;
	}
示例#5
0
static struct rb_node *rb_insert_node(struct rb_node **root, struct rb_node *n)
{
	struct rb_node *p, *t;

	p = *root;
	t = NIL;

	/* find the place to insert */
	while (p != NIL) {
		t = p;
		if (p->key < n->key)
			p = p->rc;
		else
			p = p->lc;
	}
	
	/* set up parent */
	n->p = t;

	/*  insert as root */
	if (t == NIL) {
		*root = n;
	}
	/* insert as right child */
	else if (t->key < n->key) {
		t->rc = n;
	}
	else  /* insert as left child */
		t->lc = n;

	/* make color to RED */
	n->c = 'R';

	rb_insert_fixup(root, n);
}
void Red_Black_Tree::rb_insert(int val) {
  Node* y = _NIL;
  Node* x = _root;
  Node* z = new Node;
  z->val = val;

  while (x != _NIL && x != 0) {
    y = x;
    if (x->val <= val)
      x = x->right;
    else
      x = x->left;
  }
  z->parent = y;
  if (y == _NIL)
    _root = z;
  else if (y->val < z->val)
    y->right = z;
  else
    y->left = z;
  z->left = _NIL;
  z->right = _NIL;
  z->color = 1;
  rb_insert_fixup(z);
}
示例#7
0
文件: main.c 项目: gqqh/algorithms
//四、红黑树的插入  
//---------------------------------------------------------  
//红黑树的插入结点  
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修复红黑树的性质  
}  
示例#8
0
void Insert(nodelist root,nodelist x)
{
	node_list nlist;
	node_list par_list;
	nlist=par=root;
	while(nlist!=NULL)
	{
		par=nlist;
		if(nlist->data>x->data)
		{
			nlist=nlist->left;
		}
		else
		{
			nlist=nlist->right;
		}
	}
	if(nlist==root)
	{
		root=x;
	}
	else if(nlist==par->left)
	{
		x=par->left;
	}
	else
	{
		x=par->right;
	}	
	x->left=NULL;
	x->right=NULL;
	x->color=RED;
	x->p=par;
	rb_insert_fixup(x);
}
示例#9
0
void rb_insert(rbt *T, rbn *z)
{
	rbn *y = T->nil;
	rbn *x = T->root;
	while (x != T->nil)
	{
		y = x;
		if (z->key < x->key)
			x = x->left;
		else
			x = x->right;
		x->size++;
	}
	z->p = y;
	if (y == T->nil)
		T->root = z;
	else if (z->key < y->key)
		y->left = z;
	else
		y->right = z;
	z->left = T->nil;
	z->right = T->nil;
	z->color = RED;
	rb_insert_fixup(T, z);
}
示例#10
0
int RBTree::rb_insert(Node *nd)
{
	Node *x, *y;
	y = NIL;
	x = root;

	while(x != NIL)
	{
		y = x;
		if(nd->value < x->value)
			x = x->left;
		else
			x = x->right;
	}
	
	nd->parent = y;
	if(y == NIL)
		root = nd;
	else if(nd->value < y->value)
			y->left = nd;
		 else
		 	y->right = nd;

	nd->left = NIL;
	nd->right = NIL;
	nd->color = RED;
	rb_insert_fixup(nd);
	return 0;
}
示例#11
0
文件: redblacktree.c 项目: laumann/C
void tree_insert(struct redblack_tree *T, int key)
{
	struct rb_node *z = ALLOC_NODE; 
	struct rb_node *y = nil;
	struct rb_node *x = T->root;

	z->key = key;
	
	while (x != nil) {
		y = x;
		x = (z->key < x->key) ? x->left	: x->right;
	}
	z->parent = y;

	if (y == nil)
		T->root = z;
	else if (z->key < y->key)
		y->left = z;
	else
		y->right = z;

	z->left = nil;
	z->right = nil;
	z->color = RED;

	T->size++;

	rb_insert_fixup(T, z);
}
示例#12
0
文件: rbtree.c 项目: 4179e1/misc
void rb_tree_insert (RbTree *t, void *data)
{
	TreeNode *x;
	TreeNode *y;
	TreeNode *z;
	
	assert (t != NULL);

	x = tree_node_get_right (t->sent);
	y = t->sent;

	z = tree_node_new_full (data, NULL, y, y);
	if (z == NULL)
	{
		return;
	}

	while (x != t->sent)
	{
		y = x;
		if (t->cmp_f (data, tree_node_get_content (x)) < 0)
		{
			x = tree_node_get_left (x);
		}
		else
		{
			x = tree_node_get_right (x);
		}
	}

	tree_node_set_parent (z, y);

	if (y == t->sent)	/* root */
	{
		tree_node_set_right (t->sent, z);
	}
	else
	{
		if (t->cmp_f (data, tree_node_get_content (y)) < 0)
		{
			tree_node_set_left (y, z);
		}
		else
		{
			tree_node_set_right (y, z);
		}
	}

	tree_node_set_red (z);

	rb_insert_fixup (t, z);

	(t->card)++;
}
示例#13
0
Node* tree_insert(Node* &T, Interval key)
{
	// 分配新节点
	Node *n = malloc_node(key);

	// 如果当前是空树,则n节点作为根,刷成黑色即满足红黑性质
	if (NIL == T){
		SET_BLACK(n);
		T = n;
		return n;
	}
	
	// 如果当前不是空树,则先找到插入位置
	Node *p = T;
	Node *q;
	while(NIL != p) {
		// 调整路径上节点的max值
		if (MAX(p) < HIGH(key))
			MAX(p) = HIGH(key);

		q = p;
		if (LT(key,KEY(p)))
			p = LEFT(p);
		else
			p = RIGHT(p);
	}

	// 找到了插入位置,n节点的父亲为q
	PARENT(n) = q;
	if (LT(key, KEY(q))) {
		LEFT(q) = n;
	} else {
		RIGHT(q) = n;
	}

	// 设置n节点为红色
	// 这样只可能违背红黑性质4(性质4的调整应该比性质5简单)
	SET_RED(n);

	// 从n节点开始调整,使之符合红黑性质4
	// 调整的每一步,都不会破坏其他红黑性质
	rb_insert_fixup(T, n);
	return n;
}
示例#14
0
Node* tree_insert(Node* &T, int key)
{
	// 分配新节点
	Node *n = malloc_node(key);

	// 如果当前是空树,则n节点作为根,刷成黑色即满足红黑性质
	if (NIL == T){
		SET_BLACK(n);
		T = n;
		return n;
	}
	
	// 如果当前不是空树,则先找到插入位置
	Node *p = T;
	Node *q;
	while(NIL != p) {
		// 每经过一个节点,其size加一
		SIZE(p)++;
		q = p;
		if (key < KEY(p))
			p = LEFT(p);
		else
			p = RIGHT(p);
	}

	// 找到了插入位置,n节点的父亲为q
	PARENT(n) = q;
	if (key < KEY(q)) {
		LEFT(q) = n;
	} else {
		RIGHT(q) = n;
	}

	// 设置n节点为红色
	// 这样只可能违背红黑性质4(性质4的调整应该比性质5简单)
	SET_RED(n);

	// 从n节点开始调整,使之符合红黑性质4
	// 调整的每一步,都不会破坏其他红黑性质
	rb_insert_fixup(T, n);
	return n;
}
示例#15
0
void rb_insert (struct rb_tree *t, void *data)
{
	struct rb_node *z;
	struct rb_node *y = &rb_null;
	struct rb_node *x = t->root;

	assert (t != NULL);
	assert (t->root != NULL);

	z = (struct rb_node *)xmalloc (sizeof(struct rb_node));

	z->data = data;
	
	while (x != &rb_null) {
		int cmp = t->cmp_func(z->data, x->data, t->adata);
	
		y = x;
		if (cmp < 0)
			x = x->left;
		else if (cmp > 0)
			x = x->right;
		else
			abort ();
	}

	z->parent = y;
	if (y == &rb_null)
		t->root = z;
	else {
		if (t->cmp_func(z->data, y->data, t->adata) < 0)
			y->left = z;
		else
			y->right = z;
	}
	
	z->left = &rb_null;
	z->right = &rb_null;
	z->color = RB_RED;

	rb_insert_fixup (&t->root, z);
}
示例#16
0
rb_node_t* rb_insert(rb_node_t* root, int key)
{
	rb_node_t* nodey = &nil;
	rb_node_t* nodex = root;
	rb_node_t* nodez = rb_newnode(key);
    while(nodex != &nil)
    {
    	nodey = nodex;
    	if(nodez->key < nodex->key) nodex = nodex->lchild;
    	else                        nodex = nodex->rchild;
    }
    nodez->parent = nodey;
    if(nodey == &nil)  root = nodez;
    else
    {
    	if(nodez->key < nodey->key)      nodey->lchild = nodez;
        else                             nodey->rchild = nodez;
    }
    nodez->lchild = &nil;
    nodez->rchild = &nil;
    nodez->color = RED;
    root = rb_insert_fixup(root,nodez);
	return root;
}
示例#17
0
/*
 * rb_insert: insert a new value into the tree.
 *
 * data represents the value to insert.  Its RBNode fields need not
 * be valid, it's the extra data in the larger struct that is of interest.
 *
 * If the value represented by "data" is not present in the tree, then
 * we copy "data" into a new tree entry and return that node, setting *isNew
 * to true.
 *
 * If the value represented by "data" is already present, then we call the
 * combiner function to merge data into the existing node, and return the
 * existing node, setting *isNew to false.
 *
 * "data" is unmodified in either case; it's typically just a local
 * variable in the caller.
 */
RBNode *
rb_insert(RBTree *rb, const RBNode *data, bool *isNew)
{
	RBNode	   *current,
			   *parent,
			   *x;
	int			cmp;

	/* find where node belongs */
	current = rb->root;
	parent = NULL;
	cmp = 0;					/* just to prevent compiler warning */

	while (current != RBNIL)
	{
		cmp = rb->comparator(data, current, rb->arg);
		if (cmp == 0)
		{
			/*
			 * Found node with given key.  Apply combiner.
			 */
			rb->combiner(current, data, rb->arg);
			*isNew = false;
			return current;
		}
		parent = current;
		current = (cmp < 0) ? current->left : current->right;
	}

	/*
	 * Value is not present, so create a new node containing data.
	 */
	*isNew = true;

	x = rb->allocfunc (rb->arg);

	x->iteratorState = InitialState;
	x->color = RBRED;

	x->left = RBNIL;
	x->right = RBNIL;
	x->parent = parent;
	rb_copy_data(rb, x, data);

	/* insert node in tree */
	if (parent)
	{
		if (cmp < 0)
			parent->left = x;
		else
			parent->right = x;
	}
	else
	{
		rb->root = x;
	}

	rb_insert_fixup(rb, x);

	return x;
}
示例#18
0
/*
 * Allocate node for data and insert in tree.
 *
 * Return old data (or result of appendator method) if it exists and NULL
 * otherwise.
 */
void *
rb_insert(RBTree *rb, void *data)
{
	RBNode	   *current,
			   *parent,
			   *x;
	int			cmp;

	/* find where node belongs */
	current = rb->root;
	parent = NULL;
	cmp = 0;
	while (current != RBNIL)
	{
		cmp = rb->comparator(data, current->data, rb->arg);
		if (cmp == 0)
		{
			/*
			 * Found node with given key.  If appendator method is provided,
			 * call it to join old and new data; else, new data replaces old
			 * data.
			 */
			if (rb->appendator)
			{
				current->data = rb->appendator(current->data, data, rb->arg);
				return current->data;
			}
			else
			{
				void	   *old = current->data;

				current->data = data;
				return old;
			}
		}
		parent = current;
		current = (cmp < 0) ? current->left : current->right;
	}

	/* setup new node in tree */
	x = palloc(sizeof(RBNode));
	x->data = data;
	x->parent = parent;
	x->left = RBNIL;
	x->right = RBNIL;
	x->color = RBRED;

	x->iteratorState = InitialState;

	/* insert node in tree */
	if (parent)
	{
		if (cmp < 0)
			parent->left = x;
		else
			parent->right = x;
	}
	else
	{
		rb->root = x;
	}

	rb_insert_fixup(rb, x);
	return NULL;
}