Exemplo n.º 1
0
struct RBNode *insertRBNodeBinary(struct RBTree *tree, char *key, RBDATATYPE *data)
{
    if (!tree->root) {
        tree->root = createRBNode(key, data, NULL, NULL, NULL);
        return;
    }

    struct RBNode *node = tree->root, *parent = NULL;

    int height;
    for (height = 0; node; ++height) {
        if (equalStrings(key, node->key, MAXKEYSIZE)) {
            return;
        } else {
            parent = node;
            node = compare(key, node->key) ? node->left : node->right;
        }
    }

    if (height > tree->height)
        tree->height++;

    struct RBNode *n = createRBNode(key, data, NULL, NULL, NULL);
    if (!n)
        return;
    
    if (compare(key, parent->key))
        parent->left = n;
    else
        parent->right = n;

    tree->size++;

    return n;
}
Exemplo n.º 2
0
struct rb_tree *createRBTree() {
	struct rb_tree *rb = (struct rb_tree *)malloc(sizeof(struct rb_tree));
	rb->size = 0;
	rb->nil = createRBNode(0, BLACK);
	rb->nil->left = rb->nil;
	rb->nil->right = rb->nil;
	rb->nil->p = rb->nil;
	rb->root = rb->nil;
	return rb;
}
Exemplo n.º 3
0
void insertRBTree(Tree *tree, int value)
{
	if (tree->root == NULL)
	{
		tree->root = createRBNode(tree, value);
		RBInsertFixup(tree, tree->root);
		return;
	}

	Node *node = createRBNode(tree, value);
	Node *n = tree->root;
	while (1)
	{
		n->size++;
		if (value < n->value)
		{
			if (n->left == tree->nil)
			{
				n->left = node;
				node->parent = n;
				break;
			}
			n = n->left;
		}
		else
		{
			if (n->right == tree->nil)
			{
				n->right = node;
				node->parent = n;
				break;
			}
			n = n->right;
		}
	}

	RBInsertFixup(tree, node);
}
Exemplo n.º 4
0
void RBTreeInsert(struct rb_tree *rb, rb_key_t key) {
	struct rb_node *z = createRBNode(key, RED);
	struct rb_node *x, *y;
	rb->size++;
	y = rb->nil;
	x = rb->root;
	while (x != rb->nil) {
		y = x;
		if (keyCmp(x->key, key) > 0) 
			x = x->left;
		else
			x = x->right;
	}
	z->p = y;
	if (y == rb->nil)
		rb->root = z;
	else if (keyCmp(y->key, key) > 0)
		y->left = z;
	else
		y->right = z;
	z->left = rb->nil;
	z->right = rb->nil;
	RBTreeInsertFixup(rb, z);
}