Ejemplo n.º 1
0
/*
 * rb_delete - delete node from Red-black tree
 */
static void rb_delete(void *node){
    void *m, *c;
    m = RB_LEFT(node) == rb_null || RB_RIGHT(node) == rb_null ? node : rb_successor(node);
    c = RB_LEFT(m) == rb_null ? RB_RIGHT(m) : RB_LEFT(m);
    if((RB_PARENT(c) = RB_PARENT(m)) == rb_root){
        RB_LEFT(rb_root) = c;
    }else{
        if(RB_LEFT(RB_PARENT(m)) == m){
            RB_LEFT(RB_PARENT(m)) = c;
        }else{
            RB_RIGHT(RB_PARENT(m)) = c;
        }
    }
    if(m != node){
        if(!RB_RED(m)) rb_fix(c);
        RB_LEFT(m) = RB_LEFT(node);
        RB_RIGHT(m) = RB_RIGHT(node);
        RB_PARENT(m) = RB_PARENT(node);
        RB_RED(m) = RB_RED(node);
        RB_PARENT(RB_LEFT(node)) = RB_PARENT(RB_RIGHT(node)) = m;
        if(node == RB_LEFT(RB_PARENT(node))){
            RB_LEFT(RB_PARENT(node)) = m;
        }else{
            RB_RIGHT(RB_PARENT(node)) = m;
        }
    }else{
        if(!RB_RED(m)) rb_fix(c);
    }
}
Ejemplo n.º 2
0
RBTree::Node *RBTree::rb_delete(Node *nd)
{
	Node *x, *y = NIL;
	if(nd == NIL || nd == NULL)
		return NULL;
	if(nd->left == NIL || nd->right == NIL)
		y = nd;
	else
		y = rb_successor(nd);

	if(y->left != NIL)
		x = y->left;
	else
		x = y->right;

	x->parent = y->parent;
	if(y->parent == NIL)
		root = x;
	else if(y == y->parent->left)
		y->parent->left = x;
	else
		y->parent->right = x;

	if(y != nd)
		nd->value = y->value;

	if(y->color == BLACK)
		rb_delete_fixup(x);
	return y;
}
Ejemplo n.º 3
0
static const void * rb_readlist(RBLIST *rblistp) {
  const void *key=NULL;

  if (rblistp!=NULL && rblistp->nextp!=RBNULL) {
    key=rblistp->nextp->key;
    rblistp->nextp=rb_successor(rblistp->nextp);
  }

  return(key);
}
Ejemplo n.º 4
0
/* Delete the node z, and free up the space
*/
static void rb_delete(struct rbnode **rootp, struct rbnode *z) {
  struct rbnode *x, *y, *tmp;
  const void *max;
  
  if (z->left == RBNULL || z->right == RBNULL)
    y=z;
  else
    y=rb_successor(z);
  
  if (y->left != RBNULL)
    x=y->left;
  else
    x=y->right;
  
  x->up = y->up;
  
  if (y->up == RBNULL) {
    *rootp=x;
  } else {
    if (y==y->up->left)
      y->up->left = x;
    else
      y->up->right = x;
  }
  
  if (y!=z) {
    z->key = y->key;
    z->high=y->high;
    z->object=y->object;
  }
  tmp=y->up;
  while(tmp!=RBNULL) {
    max=NULL;
    if (tmp->left!=RBNULL)
      max=tmp->left->max;
    if (tmp->right!=RBNULL&&
	tmp->right->max>max)
      max=tmp->right->max;
    if (tmp->high>max)
      max=tmp->high;
    tmp->max=max;
    tmp=tmp->up;
  }
  if (y->colour == BLACK)
    rb_delete_fix(rootp, x);
  
  rb_free(y);
}
Ejemplo n.º 5
0
void RBTree::test()
{
	Node *nd = root->right->right,*p;
	p = rb_successor(nd);
	if(p == NIL)
		printf("No successor\n");
	else
		printf("%d's successor:%d\n", nd->value, p->value);
	printf("Test left rotate\n");
	left_rotate(root);
	travel();
	printf("\n");

	printf("Test right rotate\n");
	right_rotate(root);
	travel();
	printf("\n");

}
Ejemplo n.º 6
0
rb_node_t *rb_delete(rb_tree *tree, rb_node_t *z) {
    rb_node_t *x;
    rb_node_t *y;
    if (z->left == tree->nil || z->right == tree->nil) {
        y = z;
    } else {
        y = rb_successor(tree, z);
    }
    
    if (y->left != tree->nil) {
        x = y->left;
    } else {
        x = y->right;
    }
    
    x->parent = y->parent;
    
    if (y->parent == tree->nil) {
        tree->root = x;
    } else {
        if (y == y->parent->left) {
            y->parent->left = x;
        } else {
            y->parent->right = x;
        }
    }
    
    if (y != z) {
        z->key = y->key;
        z->data = y->data;
    }
    if (y->color == BLACK) {
        rb_delete_fixup(tree, x);
    }
    
    return y;
}
Ejemplo n.º 7
0
rb_node_t* rb_delete(rb_node_t* root,rb_node_t* nodez)
{
	rb_node_t* nodey;
	rb_node_t* nodex;
    if(nodez->lchild == &nil || nodez->rchild == &nil)
    {
    	nodey = nodez;
    }
    else
    	nodey = rb_successor(nodez);
    if(nodez->lchild != &nil)
    	nodex = nodey->lchild;
    else
    	nodex = nodey->rchild;

    nodex->parent = nodey->parent;
    if(nodey->parent == &nil)
    	root = nodex;
    else
    {
      if(nodey == nodey->parent->lchild)
    	  nodey->parent->lchild = nodex;
      else
    	  nodey->parent->rchild = nodex;
    }
    if(nodey != nodez)
    {
    	nodez->key = nodey->key;
    }
    if(nodey->color == BLACK)
    {
    	root = rb_delete_fixup(root,nodex);
    }
    free(nodey);
	return root;
}
Ejemplo n.º 8
0
void test_integers_multi(int factor)
{
    int i;
    intptr_t val;
    unsigned int count;
    struct rb_node *node;

    struct rb_tree *tree = rb_create(integer_cmp,
				     integer_free, integer_free,
				     integer_print, integer_print);

    assert( rb_isempty(tree) == 1 );

    for (i = 0; i < 100 * factor; i++)
    {
	val = i % factor;
	rb_insert(tree, (void*)val, (void*)val);
    }

    assert( rb_isempty(tree) == 0 );
    assert( rb_size(tree) == (unsigned int)100 * factor);

    for (i = 0; i < factor; i++)
    {
	val = i;
	node = rb_find(tree, (void*)val);
	assert(node);

	count = 0;
	while (node != rb_end(tree) && (intptr_t)node->key == i)
	{
	    ++count;
	    node = rb_successor(tree, node);
	}

	assert(count == 100);
    }

    {
	count = 0;

	for(node = rb_begin(tree); node != rb_end(tree);
	    node = rb_successor(tree, node))
	{
	    assert((intptr_t)node->key == (count++ / 100));
	}
    }

    for (i = 0; i < 100 * factor; i++)
    {
	val = i % factor;

	node = rb_find(tree, (void*)val);
	assert(node);

	rb_delete(tree, node);
    }

    assert( rb_isempty(tree) == 1 );

    rb_destroy(tree);
}