Implementacija stabla pomocu polja





0
Date Submitted Mon. Jan. 11th, 2010 6:26 PM
Revision 1 of 1
Beginner alen89
Tags implementacija | polja | pomocu | stabla
Comments 0 comments
Implementacija stabla pomocu polja

struct element{
        labeltype label;
        struct element *lijevi,*desni;
};

typedef struct element *cvor;
typedef struct element *tstablo;

cvor ParentB(cvor n,tstablo stablo){
        nod i;
        int OK=0;
        stack S;
        MakeNullS(S)
        PushS(stablo,S)
        while((!IsEmptyS(S)) && (!OK)){
                i=PopS(S)
                if (i->lijevi!=n) && (i->desni!=n)){
                        if (i->lijevi!=NULL) PushS(i->l);             
                    if (i->desni!=NULL) PushS(i->d);       
                else OK=-1;
        }
        if(OK) return i; 
        else return NULL
}

cvor LeftChildB(cvor n,tstablo stablo){
        return n->lijevi;
}

cvor RightChildB(cvor n,tstablo stablo){
        return n->desni;
}

labeltype LabelB(cvor n,tstablo stablo){
        return n->label;
}

void ChangeLabelB(labeltype vrijednost, cvor n,tstablo stablo){
        n->label=vrijednost;
}

cvor RootB(tstablo stablo){
        return stablo;
}

void CreateLeftB(labeltype vrijednost,cvor n,tstablo stablo){
        cvor l;
        if(n->lijevi!=NULL){
                l=(struct element *)malloc(sizeof(struct element));
                l->lijevi=l->desni=NULL;
                l->label=vrijednost;
                n->lijevi=l;
        }
        else return;
}

void CreateRightB(labeltype vrijednost,cvor n,tstablo stablo){
        cvor l;
        if (n->desni!=NULL){
                l=(struct element *)malloc(sizeof(struct element));
                l->lijevi=l->desni=NULL;
                l->label=vrijednost;
                n->desni=l;
        }
        else return;
}

void DelB(cvor n, tstablo stablo){
        if (n->lijevi!=NULL) DelB((n->lijevi,stablo);   
    if (n->desni!=NULL) DelB((n->desni,stablo);
        free(n);
}

void DeleteB(cvor n,tstablo stablo){
        cvor l;
        if (n->lijevi!=NULL) DelB((n->lijevi,stablo)
        if (n->desni!=NULL) DelB((n->desni,stablo);
        l=ParentB(n,stablo)
        if(l->lijevi==n) l->lijevi=NULL;
        else l->desni=NULL;
        free(n);   
}

void InitB(cvor vrijednost, tstablo *stablo){
        cvor l;
        l=(struct element *)malloc(sizeof(struct element));
        l->lijevi=l->desni=NULL;
        l->label=vrijednost;
        (*stablo)=l;
}
 
//Razlike: Napravio sam samo implementaciju pomocu pokazivaca, vecina je radila implementaciju pomocu polja. Razlicite varijable.

Alen Vincic

Comments

There are currently no comments for this snippet.

Voting

Votes Up


Votes Down