伸展树的删除例程

摘要:对要访问的节点进行访问这样会把该节点退到根处,删除根后得到两个子树.如果访问TL的最大节点,则该树变成一个没有右儿子的树,将TR接在TL上即可.

Position Delete(Position T,int X)
{
    Position TL,TR;
    if (T == NULL)
    {
        puts("error : X does not exist in the tree");
        exit(-1);
    }
    T = FindOperation(T,X);
    if (T->Left == NULL)
    {
        TL = T->Right;
        free(T);
    }
    else
    {
    TL = T->Left;
    TR = T->Right;
    TL = FindOperation(TL,FindMax(TL));
    TL->Right = TR;
    free(T);
    }
    return TL;
}

你可能感兴趣的:(伸展树的删除例程)