コード例 #1
0
ファイル: rbtree.sample.cpp プロジェクト: venux021/mustard
void RBTree::remove(int v)
{
    rbnode * n = find(v);
    if (n == pNil) {
        return;
    }

    rbnode * y = pNil;
    if (n->lc == pNil || n->rc == pNil) {
        y = n;
    } else {
        y = get_successor(n);
        n->value = y->value;
    }

    rbnode * x = (y->lc == pNil ? y->rc : y->lc);

    x->parent = y->parent;

    rbnode * p = y->parent;
    if (p == pNil) {
        root = x;
    } else if (y == p->lc) {
        p->lc = x;
    } else {
        p->rc = x;
    }

    if (y->color == BLACK) {
        remove_fixup(x);
    }
}
コード例 #2
0
ファイル: rb_tree.cpp プロジェクト: kjmikkel/rb_tree
void rb_tree<T>::remove(rb_vertex<T>* out_value) {
    //out_value is y in Corman

    // this is y in Corman
    rb_vertex<T>* successor;

    // this is x in Corman
    rb_vertex<T>* child;

    if (out_value->get_left_child() == 0 or out_value->get_right_child() == 0) {
        successor = out_value;
    } else {
        successor = Tree_Successor(out_value);
    }

    if (successor->get_left_child() != 0) {
        child = successor->get_left_child();
    } else {
        child = successor->get_right_child();
    }

    child->set_parent(successor->get_parent());

    if (successor->get_parent() == 0) {
        root = child;
    } else {
        if (successor == successor->get_parent()->get_left_child()) {
            successor->get_parent()->set_left_child(child);
        } else {
            successor->get_parent()->set_right_child(child);
        }
    }

    if (child != out_value) {
        out_value->set_value(successor->get_value());
    }

    if (successor->get_colour() == BLACK) {
        remove_fixup(child);
    }
}