Example #1
0
link LLRBinsert(link h, Item *item) {
    Key v = key(item);
    /* Insert a new node at the bottom*/
    
    conflict = NULL;
    
    if (h == z) {
        return NEW(item, z, z, 1, 1);
    }
    
    if (less(v, key(h->item)))
        hl = LLRBinsert(hl, item);
    
    else if (eq(v, key(h->item))) {   /* If the object we are trying to insert is already there,
                                        we opt to return a pointer to the existing item, so that
                                        the user may choose what to do (i.e. create a list of items) */
        conflict = h->item;
    }
    
    else
        hr = LLRBinsert(hr, item);
    
    
    /* Enforce left-leaning condition */
    if (hr->red && !hl->red) h = rotL(h);
    if (hl->red && hll->red) h = rotR(h);
    if (hl->red && hr->red) colorFlip(h);
    
    return fixNr(h);
}
Example #2
0
static RBTreeNodeHandle rbtree_insert_priv(RBTreeNodeHandle root, i64 key, void *value){
    /*insert the node at the bottom*/
    if (NULL == root){
        RBTreeNodeHandle node = (RBTreeNodeHandle)mag_malloc(sizeof(RBTREE_NODE_t));
        if (NULL != node){
            node->color = RBTREE_TRUE;
            node->key   = key;
            node->value = value;
            node->left  = NULL;
            node->right = NULL;
            node->parent = NULL;
        }
        return node;
    }

    /*split 4-node on the way down*/
    if (isRed(root->left) && isRed(root->right))
        colorFlip(root);
    
    /*standard binary tree insert*/
    if (root->key == key){
        root->value = value;
        ++gRepeatKeyNum;
    }else if (root->key < key){
        root->right = rbtree_insert_priv(root->right, key, value);
        root->right->parent = root;
    }else{
        root->left = rbtree_insert_priv(root->left, key, value);
        root->left->parent = root;
    }
    
    /*fix right-leaning red node:  this will assure that a 3-node is the left child*/
    if (isRed(root->right) && !isRed(root->left))
        root = rotateLeft(root);

    /*fix two reds in a row: this will rebalance a 4-node.*/
    if (isRed(root->left) && isRed(root->left->left))
        root = rotateRight(root);
    
    return root;
}
Example #3
0
static link LLRBinsert(link h, EnglishWord newWord){ 
	Key v = makeKey(newWord);
	int val = 0;

	if (compareNodes(h,z))
		return NEW(newWord, z, z, 1);
	
	val = compareKeys(v,makeKey(h->englishWord));
	if ( val < 0)
		hl = LLRBinsert(hl, newWord);
    else
		hr = LLRBinsert(hr, newWord);
    if (hr->red && !hl->red)
		h = rotL(h);
    if (hl->red && hll->red) 
		h = rotR(h);
    if (hl->red && hr->red) 
		colorFlip(h);

	return h;
}
Example #4
0
link balance(link h) {
    if (hr->red) h = rotL(h);
    if (hl->red && hll->red) h = rotR(h);
    if (hl->red && hr->red) colorFlip(h);
    return fixNr(h);
}
Example #5
0
link mvRedR(link h) {
    colorFlip(h);
    if (hll->red) { h = rotR(h); }
    return h;
}
Example #6
0
link mvRedL(link h) {
    colorFlip(h);
    if (hrl->red) { hr = rotR(hr); h = rotL(h); }
    return h;
}