具有多个相等键的平衡搜索树(Treap)

时间:2015-04-06 07:19:36

标签: c binary-search-tree treap

我必须创建一个具有多个相等键的平衡搜索树。我实现了我需要的功能:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

typedef struct tip_nod {
    int key, priority;
    struct tip_nod *left, *right;
} NOD;

    NOD *R,*nil;

NOD *creare_nod(int cheie, int prioritate, NOD* stanga, NOD* dreapta)
{
    NOD *v = (NOD*)malloc(sizeof(NOD));
    v->key = cheie;
    v->priority = prioritate;
    v->left = stanga;
    v->right = dreapta;
    return v;
}

void initializare(NOD **R,NOD **nil)
{
    srand((unsigned)time(0));
    *R = *nil = creare_nod(0,0,NULL,NULL);
}

int exista_cheie(NOD *v,int cheie)
{
    if(v == nil)
        return 0;
    if(cheie == v->key)
        return 1;
    if(cheie < v->key)
        return exista_cheie(v->left, cheie);
    else
        return exista_cheie(v->right, cheie);
}

void rotleft(NOD **n)
{
    NOD *t = (*n)->left;
    (*n)->left = t->right;
    t->right = *n;
    *n = t;
}

void rotright(NOD **n)
{
    NOD *t = (*n)->right;
    (*n)->right = t->left;
    t->left = *n;
    *n = t;
}

void balanced(NOD **n)
{
    if((*n)->left->priority > (*n)->priority)
       rotleft(n);
    else if((*n)->right->priority > (*n)->priority)
        rotright(n);
}

void inserare(NOD **R,int cheie,int prioritate)
{
    if(*R==nil)
        *R = creare_nod(cheie,prioritate,nil,nil);
    else
    {
        if((*R)->key > cheie)
            inserare(&(*R)->left,cheie,prioritate);
        else
            inserare(&(*R)->right,cheie,prioritate);

        balanced(R);
    }
}

void stergere(NOD **n,int key)
{
    if(*n == nil) return;
    if(key < (*n)->key)
        stergere(&(*n)->left,key);
    else
        if(key > (*n)->key)
            stergere(&(*n)->right,key);
        else
        {
            if((*n)->left == nil && (*n)->right == nil)
            {
                free(*n);
                *n = nil;
            }
            else
            {
                if((*n)->left->priority > (*n)->right->priority)
                    rotleft(n);
                else
                    rotright(n);
                stergere(n,key);
            }
        }
}

void afisare(NOD *n)
{
    if(n!=nil)
    {
        afisare(n->left);
        printf("%d ",n->key);
        afisare(n->right);
    }
}

int main()
{
    initializare(&R,&nil);

    printf("Inserari: ");
    inserare(&R,10,rand()+1);
    inserare(&R,5,rand()+1);
    inserare(&R,2,rand()+1);
    inserare(&R,7,rand()+1);
    inserare(&R,4,rand()+1);
    inserare(&R,20,rand()+1);
    afisare(R);
    printf("\n");

    printf("Inserare dublura: ");
    inserare(&R,2,rand()+1);
    afisare(R);
    printf("\n");

    printf("Stergere cheie existenta: ");
    stergere(&R,4);
    afisare(R);
    printf("\n");

    printf("Stergere cheie inexistenta: ");
    stergere(&R,3);
    afisare(R);
    printf("\n");

    return 0;
}

我遇到的问题是从树中删除(stergere)节点。如果我尝试删除一个唯一的键,它可以工作,但如果我尝试删除一个非唯一键,它会崩溃。

printf("Stergere cheie existenta: ");
stergere(&R,4);

它有效,但是:

printf("Stergere cheie existenta: ");
stergere(&R,2);

没有。

我该如何修改此功能?

stergere

0 个答案:

没有答案