Exemplo n.º 1
0
 void access(splay* ov) {
     for(splay *w = ov, *v = ov; w != NULL; v = w, w = w->path_up) {
         do_splay(w);
         if(w->sons[1]) w->sons[1]->path_up = w, w->sons[1]->up = NULL;
         if(w != v) w->sons[1] = v, v->up = w, v->path_up = NULL;
         else w->sons[1] = NULL;
     }
     do_splay(ov);
 }
Exemplo n.º 2
0
struct splaytree_node *splaytree_lookup(const struct splaytree_node *key,
                                        struct splaytree *tree)
{
    if (!tree->root)
        return NULL;
    if (do_splay(key, tree) != 0)
        return NULL;
    return tree->root;
}
Exemplo n.º 3
0
void splaytree_remove(struct splaytree_node *node, struct splaytree *tree)
{
    struct splaytree_node *right, *left, *prev;

    do_splay(node, tree);
    assert(tree->root == node);	/* 'node' must be present */

    right = get_right(node);
    left = get_left(node);
    if (!left) {
        tree->root = right;
        tree->first = splaytree_next(node);
        prev = NULL;
    } else {
        tree->root = left;
        do_splay(node, tree);
        set_right(right, tree->root);
        prev = tree->root;
    }
    if (right)
        set_prev(prev, get_first(right));
    else
        tree->last = prev;
}
Exemplo n.º 4
0
struct splaytree_node *splaytree_insert(struct splaytree_node *node,
                                        struct splaytree *tree)
{
    struct splaytree_node *root = tree->root;
    int res;

    if (!root) {
        INIT_NODE(node);
        tree->root = node;
        tree->first = node;
        tree->last = node;
        return NULL;
    }

    res = do_splay(node, tree);
    if (res == 0)
        return tree->root;

    root = tree->root;
    if (res < 0) {
        struct splaytree_node *left = get_left(root);

        set_left(left, node);
        set_right(root, node);
        if (left)
            set_next(node, get_last(left));
        else
            tree->first = node;
        set_prev(node, root);
    } else {
        struct splaytree_node *right = get_right(root);

        set_right(right, node);
        set_left(root, node);
        if (right)
            set_prev(node, get_first(right));
        else
            tree->last = node;
        set_next(node, root);
    }
    tree->root = node;
    return NULL;
}
Exemplo n.º 5
0
 int lca(int v, int w) {
     access(&vtxs[v]); access(&vtxs[w]); do_splay(&vtxs[v]);
     if(vtxs[v].path_up == NULL) return v;
     return vtxs[v].path_up - vtxs;
 }
Exemplo n.º 6
0
 splay* find(int v) {
     splay* s = &vtxs[v];
     access(s); while(s->sons[0]) s = s->sons[0]; do_splay(s);
     return s;
 }