Пример #1
0
void rbtree_delete(GtkWidget *darea, rbtree t, int key) {
  node n = lookup_node(darea, t, key);
  node child;

  if (n == NULL) return;

  if (n->left != NULL && n->right != NULL) {
    node pred = maximum_node(n->left);

    n->key   = pred->key;
    n = pred;
  }

  child = n->right == NULL ? n->left : n->right;
  if (node_color(n) == BLACK) {
    n->color = node_color(child);

    delete_case1(darea, t, n);
  }

  replace_node(t, n, child);

  if (n->parent == NULL && child != NULL)
    child->color = BLACK;

  free(n);
}
Пример #2
0
void rbtree_delete(rbtree t, void* key, compare_func compare) {
    node child;
    node n = lookup_node(t, key, compare);
    if (n == NULL) return;  /* Key not found, do nothing */
    if (n->left != NULL && n->right != NULL) {
        /* Copy key/value from predecessor and then delete it instead */
        node pred = maximum_node(n->left);
        n->key   = pred->key;
        n->value = pred->value;
        n = pred;
    }

    assert(n->left == NULL || n->right == NULL);
    child = n->right == NULL ? n->left  : n->right;
    if (node_color(n) == BLACK) {
        n->color = node_color(child);
        delete_case1(t, n);
    }
    replace_node(t, n, child);
    if (n->parent == NULL && child != NULL)
        child->color = BLACK;
    free(n);

    verify_properties(t);
}
Пример #3
0
static void verify_property_1(node *n) {
    if (node_color(n) != L_RED_NODE && node_color(n) != L_BLACK_NODE) {
        L_ERROR("color neither RED nor BLACK\n", "verify_property_1");
        return;
    }
    if (n == NULL) return;
    verify_property_1(n->left);
    verify_property_1(n->right);
}
Пример #4
0
void verify_property_4(node n) {
    if (node_color(n) == RED) {
        assert (node_color(n->left)   == BLACK);
        assert (node_color(n->right)  == BLACK);
        assert (node_color(n->parent) == BLACK);
    }
    if (n == NULL) return;
    verify_property_4(n->left);
    verify_property_4(n->right);
}
Пример #5
0
/**
 * @brief If sibling is black and its both children are black, perform recoloring.
 * @param t is the tree root.
 * @param n is the node at which deletion is taking place.
 */
void delete_case4(rbtree t, node n)
{
    if (node_color(n->parent) == RED && node_color(sibling(n)) == BLACK && node_color(sibling(n)->left) == BLACK && node_color(sibling(n)->right) == BLACK)
    {
        sibling(n)->color = RED;
        n->parent->color = BLACK;
    }
    else
        delete_case5(t, n);
}
Пример #6
0
static void delete_case4(L_RBTREE *t, node *n) {
    if (node_color(n->parent) == L_RED_NODE &&
        node_color(sibling(n)) == L_BLACK_NODE &&
        node_color(sibling(n)->left) == L_BLACK_NODE &&
        node_color(sibling(n)->right) == L_BLACK_NODE) {
        sibling(n)->color = L_RED_NODE;
        n->parent->color = L_BLACK_NODE;
    } else {
        delete_case5(t, n);
    }
}
Пример #7
0
void delete_case3(GtkWidget *darea, rbtree t, node n) {
  if (node_color(n->parent) == BLACK &&
      node_color(sibling(n)) == BLACK &&
      node_color(sibling(n)->left) == BLACK &&
      node_color(sibling(n)->right) == BLACK)
    {
      sibling(n)->color = RED;
      delete_case1(darea, t, n->parent);
    }
  else
    delete_case4(darea, t, n);
}
Пример #8
0
void delete_case3(bbst tree, node n) {
    if (node_color(n->parent) == BLACK &&
        node_color(sibling(n)) == BLACK &&
        node_color(sibling(n)->left) == BLACK &&
        node_color(sibling(n)->right) == BLACK)
    {
        sibling(n)->color = RED;
        delete_case1(tree, n->parent);
    }
    else
        delete_case4(tree, n);
}
Пример #9
0
Файл: rbtree.c Проект: hgn/ospfd
void delete_case3(struct rbtree* t, struct rbtree_node* n)
{
    if (node_color(n->parent) == BLACK &&
        node_color(sibling(n)) == BLACK &&
        node_color(sibling(n)->left) == BLACK &&
        node_color(sibling(n)->right) == BLACK)
    {
        sibling(n)->color = RED;
        delete_case1(t, n->parent);
    }
    else
        delete_case4(t, n);
}
Пример #10
0
static void verify_property_4(node *n) {
    if (node_color(n) == L_RED_NODE) {
        if (node_color(n->left) != L_BLACK_NODE ||
            node_color(n->right) != L_BLACK_NODE ||
            node_color(n->parent) != L_BLACK_NODE) {
            L_ERROR("children & parent not all BLACK", "verify_property_4");
            return;
        }
    }
    if (n == NULL) return;
    verify_property_4(n->left);
    verify_property_4(n->right);
}
Пример #11
0
void delete_case6(rbtree t, node n) {
    sibling(n)->color = node_color(n->parent);
    n->parent->color = BLACK;
    if (n == n->parent->left) {
        assert (node_color(sibling(n)->right) == RED);
        sibling(n)->right->color = BLACK;
        rotate_left(t, n->parent);
    }
    else
    {
        assert (node_color(sibling(n)->left) == RED);
        sibling(n)->left->color = BLACK;
        rotate_right(t, n->parent);
    }
}
Пример #12
0
Файл: rbtree.c Проект: hgn/ospfd
void insert_case2(struct rbtree* t, struct rbtree_node* n)
{
    if (node_color(n->parent) == BLACK)
        return;
    else
        insert_case3(t, n);
}
Пример #13
0
/* when n is no root */
void insert_case2(bbst tree, node n){
    if (node_color(n->parent) == BLACK)
        return;
    else
        /* here n always have grandfather cuz he's RED */
        insert_case3(tree, n);
}
Пример #14
0
/**
 * Do following while the current node u is double black or it is not root. Let sibling of node be s.
 * @brief \n(a): If sibling s is black and at least one of sibling’s children is red, perform rotation(s). Let the red child of s be r. This case can be divided in four subcases depending upon positions of s and r.
 * \n(i) Left Left Case (s is left child of its parent and r is left child of s or both children of s are red). This is mirror of right right case shown in below diagram.
 * \n(ii) Left Right Case (s is left child of its parent and r is right child). This is mirror of right left case shown in below diagram.
 * \n(iii) Right Right Case (s is right child of its parent and r is right child of s or both children of s are red)
 * \n(iv) Right Left Case (s is right child of its parent and r is left child of s)
 * @param t is the tree root.
 * @param n is the node at which deletion is taking place.
 */
void delete_case5(rbtree t, node n)
{
    if (n == n->parent->left && node_color(sibling(n)) == BLACK && node_color(sibling(n)->left) == RED && node_color(sibling(n)->right) == BLACK)
    {
        sibling(n)->color = RED;
        sibling(n)->left->color = BLACK;
        rotate_right(t, sibling(n));
    }
    else if (n == n->parent->right && node_color(sibling(n)) == BLACK && node_color(sibling(n)->right) == RED && node_color(sibling(n)->left) == BLACK)
    {
        sibling(n)->color = RED;
        sibling(n)->right->color = BLACK;
        rotate_left(t, sibling(n));
    }
    delete_case6(t, n);
}
Пример #15
0
void insert_case3(rbtree t, node n) {
    if (node_color(uncle(n)) == RED) {
        n->parent->color = BLACK;
        uncle(n)->color = BLACK;
        grandparent(n)->color = RED;
        insert_case1(t, grandparent(n));
    } else {
        insert_case4(t, n);
    }
}
Пример #16
0
static void insert_case3(L_RBTREE *t, node *n) {
    if (node_color(uncle(n)) == L_RED_NODE) {
        n->parent->color = L_BLACK_NODE;
        uncle(n)->color = L_BLACK_NODE;
        grandparent(n)->color = L_RED_NODE;
        insert_case1(t, grandparent(n));
    } else {
        insert_case4(t, n);
    }
}
Пример #17
0
static void delete_case6(L_RBTREE *t, node *n) {
    sibling(n)->color = node_color(n->parent);
    n->parent->color = L_BLACK_NODE;
    if (n == n->parent->left) {
        if (node_color(sibling(n)->right) != L_RED_NODE) {
            L_ERROR("right sibling is not RED", "delete_case6");
            return;
        }
        sibling(n)->right->color = L_BLACK_NODE;
        rotate_left(t, n->parent);
    } else {
        if (node_color(sibling(n)->left) != L_RED_NODE) {
            L_ERROR("left sibling is not RED", "delete_case6");
            return;
        }
        sibling(n)->left->color = L_BLACK_NODE;
        rotate_right(t, n->parent);
    }
}
Пример #18
0
static void delete_case2(L_RBTREE *t, node *n) {
    if (node_color(sibling(n)) == L_RED_NODE) {
        n->parent->color = L_RED_NODE;
        sibling(n)->color = L_BLACK_NODE;
        if (n == n->parent->left)
            rotate_left(t, n->parent);
        else
            rotate_right(t, n->parent);
    }
    delete_case3(t, n);
}
Пример #19
0
void delete_case2(rbtree t, node n) {
    if (node_color(sibling(n)) == RED) {
        n->parent->color = RED;
        sibling(n)->color = BLACK;
        if (n == n->parent->left)
            rotate_left(t, n->parent);
        else
            rotate_right(t, n->parent);
    }
    delete_case3(t, n);
}
Пример #20
0
void print_tree(GtkWidget *widget, rbtree_node node) {
  if (node != NULL) {
    char *s = (char*) malloc(3 * sizeof(char));
    sprintf(s, "%d", node->key);

    print_lines(widget, node->left != NULL, node->right != NULL, node->x, node->y, node->r);

    print_tree(widget, node->left);
    print_circle(widget, s, node_color(node), node->x, node->y, 15, 16);
    print_tree(widget, node->right);
  }
}
Пример #21
0
void bbst_delete(bbst tree, void* key, compare_func compare) {
    node child;
    node n = lookup_node(tree, key, compare);
    if (n == NULL) return;
    if (n->left != NULL && n->right != NULL) {
        /* find the biggest of the lower */
        node predecessor = maximum_node(n->left);
        n->key           = predecessor->key;
        n->value         = predecessor->value;
        n                = predecessor;
    }

    assert(n->left == NULL || n->right == NULL);
    child = n->right == NULL ? n->left : n->right;
    if (node_color(n) == BLACK) {
        n->color = node_color(child);
        delete_case1(tree, n);
    }
    replace_node(tree, n, child);
    if (n->parent == NULL && child != NULL)
        child->color = BLACK;
    free(n);
}
Пример #22
0
Файл: rbtree.c Проект: hgn/ospfd
struct rbtree_node *rbtree_delete(struct rbtree* t, void* key)
{
    struct rbtree_node* child;
    struct rbtree_node* n = lookup_node(t, key);
    if (n == NULL) return NULL; /* Key not found, do nothing */
    if (n->left != NULL && n->right != NULL) {
        /* Copy key/data from predecessor and then delete it instead */
        struct rbtree_node* pred = maximum_node(n->left);
        n->key = pred->key;
        n->data = pred->data;
        n = pred;
    }

    assert(n->left == NULL || n->right == NULL);
    child = n->right == NULL ? n->left : n->right;
    if (node_color(n) == BLACK) {
        n->color = node_color(child);
        delete_case1(t, n);
    }
    replace_node(t, n, child);

    return n;
}
Пример #23
0
void delete_case6(GtkWidget *darea, rbtree t, node n) {
  sibling(n)->color = node_color(n->parent);
  n->parent->color = BLACK;

  if (n == n->parent->left) {
    sibling(n)->right->color = BLACK;

    rotate_left(darea, t, n->parent);
  }
  else {
    sibling(n)->left->color = BLACK;

    rotate_right(darea, t, n->parent);
  }
}
Пример #24
0
void verify_property_5_helper(node n, int black_count, int* path_black_count) {
    if (node_color(n) == BLACK) {
        black_count++;
    }
    if (n == NULL) {
        if (*path_black_count == -1) {
            *path_black_count = black_count;
        } else {
            assert (black_count == *path_black_count);
        }
        return;
    }
    verify_property_5_helper(n->left,  black_count, path_black_count);
    verify_property_5_helper(n->right, black_count, path_black_count);
}
Пример #25
0
/*
 *  l_rbtreeDelete()
 *
 *      Input:  t (rbtree, including root node)
 *              key (delete the node with this key)
 *      Return: void
 */
void
l_rbtreeDelete(L_RBTREE  *t,
               RB_TYPE    key)
{
node  *n, *child;

    PROCNAME("l_rbtreeDelete");

    if (!t) {
        L_ERROR("tree is null\n", procName);
        return;
    }

    n = lookup_node(t, key);
    if (n == NULL) return;  /* Key not found, do nothing */
    if (n->left != NULL && n->right != NULL) {
            /* Copy key/value from predecessor and then delete it instead */
        node *pred = maximum_node(n->left);
        n->key   = pred->key;
        n->value = pred->value;
        n = pred;
    }

        /* n->left == NULL || n->right == NULL */
    child = n->right == NULL ? n->left  : n->right;
    if (node_color(n) == L_BLACK_NODE) {
        n->color = node_color(child);
        delete_case1(t, n);
    }
    replace_node(t, n, child);
    if (n->parent == NULL && child != NULL)  /* root should be black */
        child->color = L_BLACK_NODE;
    LEPT_FREE(n);

    verify_properties(t);
}
Пример #26
0
static void verify_property_5_helper(node *n, int black_count,
                                     int* path_black_count) {
    if (node_color(n) == L_BLACK_NODE) {
        black_count++;
    }
    if (n == NULL) {
        if (*path_black_count == -1) {
            *path_black_count = black_count;
        } else if (*path_black_count != black_count) {
            L_ERROR("incorrect black count", "verify_property_5_helper");
        }
        return;
    }
    verify_property_5_helper(n->left,  black_count, path_black_count);
    verify_property_5_helper(n->right, black_count, path_black_count);
}
Пример #27
0
static void delete_case5(L_RBTREE *t, node *n) {
    if (n == n->parent->left &&
        node_color(sibling(n)) == L_BLACK_NODE &&
        node_color(sibling(n)->left) == L_RED_NODE &&
        node_color(sibling(n)->right) == L_BLACK_NODE) {
        sibling(n)->color = L_RED_NODE;
        sibling(n)->left->color = L_BLACK_NODE;
        rotate_right(t, sibling(n));
    } else if (n == n->parent->right &&
               node_color(sibling(n)) == L_BLACK_NODE &&
               node_color(sibling(n)->right) == L_RED_NODE &&
               node_color(sibling(n)->left) == L_BLACK_NODE) {
        sibling(n)->color = L_RED_NODE;
        sibling(n)->right->color = L_BLACK_NODE;
        rotate_left(t, sibling(n));
    }
    delete_case6(t, n);
}
Пример #28
0
static void verify_property_2(node *root) {
    if (node_color(root) != L_BLACK_NODE)
        L_ERROR("root is not black!\n", "verify_property_2");
}
Пример #29
0
static void insert_case2(L_RBTREE *t, node *n) {
    if (node_color(n->parent) == L_BLACK_NODE)
        return; /* Tree is still valid */
    else
        insert_case3(t, n);
}
Пример #30
0
void insert_case2(rbtree t, node n) {
    if (node_color(n->parent) == BLACK)
        return; /* Tree is still valid */
    else
        insert_case3(t, n);
}