Пример #1
0
// FIXME: how to join these two functions into one?
void set_of_doubles_to_string (rbtree *t, strbuf *out, unsigned limit)
{
    unsigned doubles_cnt=rbtree_count(t);

    if (doubles_cnt<limit)
    {
        rbtree_node *max=rbtree_maximum(t);

        for (rbtree_node *j=rbtree_minimum(t); j; j=rbtree_succ(j))
        {
            strbuf_addf (out, "%.1f", *(double*)j->key);
            if (j!=max)
                strbuf_addstr (out, ", ");
        };
    }
    else
    {
        unsigned chunk_size=limit/2;
        int i;
        rbtree_node *j;

        for (i=0, j=rbtree_minimum(t); i<chunk_size; j=rbtree_succ(j), i++)
        {
            strbuf_addf (out, "%.1f", *(double*)j->key);
            if (i+1 != chunk_size)
                strbuf_addstr (out, ", ");
        };
        
        strbuf_addf (out, " (%d doubles skipped) ", doubles_cnt - chunk_size*2);

        rbtree_node *n=rbtree_maximum(t);
        for (int i=0; i<(chunk_size-1); i++)
            n=rbtree_pred(n);

        for (int i=0; i<chunk_size; n=rbtree_succ(n), i++)
        {
            strbuf_addf (out, "%.1f", *(double*)n->key);
            if (i+1 != chunk_size)
                strbuf_addstr (out, ", ");
        };
    };
};
Пример #2
0
int
main(void)
{
    //int         i;
    RB_TREE     *T;

    T = tree_init();

    node_insert(T, 23);
    node_insert(T, 94);
    node_insert(T, 32);
    node_insert(T, 84);
    node_insert(T, 12);
    node_insert(T, 8);
    node_insert(T, 82);
    node_insert(T, 31);
    node_insert(T, 59);
    node_insert(T, 41);
    node_insert(T, 73);

    //for ( i = 0 ; i < 1000 ; i++ ) {
    //    key = rand() % 1000;
    //    node_insert(T, key);
    //}


    inorder_tree_walk(T, T->root);
    printf("\n");

    node_delete(T, 32);
    node_delete(T, 8);
    node_delete(T, 4);

    printf("after delete 32, 8, 4:\n");
    inorder_tree_walk(T, T->root);
    printf("\n");

    printf("sucessor of 23 is %d\n", rbtree_successor(T, rbtree_search(T, T->root, 23))->key);
    printf("predecessor of 84 is %d\n", rbtree_predecessor(T, rbtree_search(T, T->root, 84))->key);
    printf("the maximun is %d\n", rbtree_maximum(T, T->root)->key);
    printf("the minimum is %d\n", rbtree_minimum(T, T->root)->key);

    node_destory(T, T->root);
    tree_destory(T);

    exit(0);
}
Пример #3
0
void set_of_string_to_string (rbtree *t, strbuf *out, unsigned limit)
{
    unsigned strings_cnt=rbtree_count(t);
    unsigned strings_dumped=0;
    rbtree_node *max=rbtree_maximum(t);
    for (rbtree_node *j=rbtree_minimum(t); j; j=rbtree_succ(j))
    {
        strbuf_addstr (out, (char*)j->key);
        if (strings_dumped++>limit)
        {
            strbuf_addf (out, " (%d strings skipped)", strings_cnt - strings_dumped);
            break;
        };
        if (j!=max)
            strbuf_addstr (out, ", ");
    };
};
Пример #4
0
RB_NODE *
rbtree_predecessor(RB_TREE *T, RB_NODE *x)
{
    /*
     * 查找先驱,与tree_successor对称
     */
    RB_NODE    *y;

    y = node_init(T);

    if (x->left != T->nil)
        return rbtree_maximum(T, x->left);

    y = x->parent;
    while (y != T->nil && x == y->left) {
        x = y;
        y = y->parent;
    }

    return(y);

}