Example #1
0
// remove (one) edge between node u,v
// note that when the node becomes standalone, also rip its color
bool ConstraintGraph::remove_edge(const GNode &u,const GNode &v){
	GEdge &uv = access_edge(u,v);
	GEdge &vu = access_edge(v,u);
	GNode &unode = access_node(u);
	GNode &vnode = access_node(v);
	assert( uv.count == vu.count );

	// edge not exist, 
	if( uv.count == 0 ) return false; 

	uv.count--;
	if( uv.count == 0 ) uv.type = NOEDGE;

	unode.ecount--;
	if( unode.ecount == 0 ) erase_color(u);

	vu.count--; // symmetric
	if( vu.count == 0 ) vu.type = NOEDGE;

	vnode.ecount--;
	if( vnode.ecount == 0 ) erase_color(v);

	return true;
}
Example #2
0
void rbtree::remove(node *n) noexcept {
    node * child,*parent;
    unsigned color;

    if (!n->_left) {
        child = n->_right;
    }
    else if (!n->_right) {
            child = n->_left;
        }
        else {
            node *old = n, *left;

            n = n->_right;
            while ((left = n->_left) != 0) {
                n = left;
            }
            child = n->_right;
            parent = n->parent();
            color = n->color();

            if (child) {
                child->parent(parent);
            }
            if (parent == old) {
                parent->_right = child;
                parent = n;
            }
            else {
                parent->_left = child;
            }

            n->_parent_color = old->_parent_color;
            n->_right = old->_right;
            n->_left = old->_left;

            if (old->parent()) {
                if (old->parent()->_left == old) {
                    old->parent()->_left = n;
                }
                else {
                    old->parent()->_right = n;
                }
            }
            else {
                _root = n;
            }

            old->_left->parent(n);
            if (old->_right) {
                old->_right->parent(n);
            }
            goto color;
        }

    parent = n->parent();
    color = n->color();

    if (child) {
        child->parent(parent);
    }
    if (parent) {
        if (parent->_left == n) {
            parent->_left = child;
        }
        else {
            parent->_right = child;
        }
    }
    else {
        _root = child;
    }

color:
    if (color == GV_RB_BLACK) {
        erase_color(child, parent);
    }
}