首页 > 代码库 > 算法:伸展树的实现

算法:伸展树的实现

splaytree.h

#ifndef _SPLAYTREE_H
#define _SPLAYTREE_H

struct Node;
typedef int ElementType;
typedef struct Node *PtrToNode;
typedef PtrToNode SplayTree;
typedef PtrToNode Position;

SplayTree MakeEmpty(SplayTree T);
Position Find(ElementType X,SplayTree T);
Position FindMax(SplayTree T);
Position FindMin(SplayTree T);
SplayTree Insert(ElementType X,SplayTree T);
SplayTree Delete(ElementType X,SplayTree T);

#endif

struct Node
{
    ElementType element;
    PtrToNode Left;
    PtrToNode Right;
    PtrToNode Parent;
};

splaytree.c

#include <stdlib.h>
#include <stdio.h>
#include "SplayTree.h"

SplayTree MakeEmpty(SplayTree T)
{
    if(T!=NULL){
        MakeEmpty(T->Left);
        MakeEmpaty(T->Right);
        free(T);
    }
    return NULL;
}
Position Find_Position(ElementType X,SplayTree T)
{
    if(T==NULL){
        return T;
    }else if(X<T->element){
        return Find_Position(X,T->Left);
    }else if(X>T->element){
        return Find_Position(X,T->Right);
    }
    return T;
}
SplayTree SingleRotateWithLeft(SplayTree T)
{
    Position P=T->Left,Parent=T->Parent;
    T->Left=P->Right;
    P->Right=T;
    P->Parent=Parent;
    T->Parent=P;
    if(Parent!=NULL){
        if(Parent->Left==T){
            Parent->Left=P;
        }else if(Parent->Right==T){
            Parent->Right=P;
        }
    }
    return P;
}
SplayTree SingleRotateWithRight(SplayTree T)
{
    Position P=T->Right,Parent=T->Parent;
    T->Right=P->Left;
    P->Left=T;
    P->Parent=Parent;
    T->Parent=P;
    if(Parent!=NULL){
        if(Parent->Left==T){
            Parent->Left=P;
        }else if(Parent->Right==T){
            Parent->Right=P;
        }
    }
    return P;
}
SplayTree DoubleRotateWithLeft(SplayTree T)
{
    SingleRotateWithRight(T->Left);
    return SingleRotateWithLeft(T);
}
SplayTree DoubleRotateWithRight(SplayTree T)
{
    SingleRotateWithLeft(T->Right);
    return SingleRotateWithRight(T);
}
SplayTree Left_Zig_Zig(SplayTree T)
{
    Position P;
    
    P=SingleRotateWithLeft(T);
    return SingleRotateWithLeft(P);
}
SplayTree Right_Zag_Zag(SplayTree T)
{
    Position P;
    P=SingleRotateWithRight(T);
    return SingleRotateWithRight(P);
}
Position SingleWithGrandPa(SplayTree T,Position P)
{
    Position Parent=P->Parent,GrandPa=P->Parent->Parent;
    
    if(GrandPa->Left==Parent){
        if(Parent->Left==P){
            Left_Zig_Zig(GrandPa);
        }else if(Parent->Right==P){
            DoubleRotateWithLeft(GrandPa);
        }
    }else if(GrandPa->Right==Parent){
        if(Parent->Left==P){
            DoubleRotateWithRight(GrandPa);
        }else if(Parent->Right==P){
            Right_Zag_Zag(GrandPa);
        }
    }
}

void Splay_Tree(SplayTree T,Position P)
{
    while(T->Left!=P&&T->Right!=P){
        SingleWithGrandPa(T,P);
    }
    if(T->Left==P){
        SigleRotateWithLeft(T);
    }else if(T->Right==P){
        SingleRotateWithRight(T);
    }
}
Position Find(ElementType X,SplayTree T)
{
    Position P;
    P=Find_Position(X,T);
    if(P!=NULL&&P!=T){
        Splay_Tree(T,P);        
    }
    return P;
}
SplayTree Insert(ElementType X,SplayTree T)
{
    Position temp;
    
    if(T==NULL){
        T=malloc(sizeof(struct Node));
        if(T==NULL){
            printf("out of space");
        }else{
            temp=T->Right=T->Parent=NULL;
            T->Left=temp;
            temp->Parent=T;
        }
    }else if(X<T->element){
        temp=Insert(X,T->Left);
        temp->Parent=T;
        T->Right=temp;
    }else if(X>T->element){
        T->Right=Insert(X,T->Right);
    }
    return T;
}
Position FindMax(SplayTree T)
{
    Posiiton P=T;
    if(P!=NULL){
        while(P->Right!=NULL){
            P=P->Right;
        }
    }
    return P;
}
Position FindMin(SplayTree T)
{
    Position P=T;
    if(P!=NULL){
        while(P->Left!=NULL){
            P=P->Left;
        }
    }
    return P;
}
SplayTree Delete(ElementType X,SplayTree T)
{
    Position P,temp;
    P=Find(X,T);
    if(P!=NULL){
        temp=FindMin(P->Right);
        P->element=temp->element;
        temp->Parent->Left==NULL;
        free(temp);
    }
    return P;
}

 

算法:伸展树的实现