Implementacija binarnog stabla





0
Date Submitted Sun. Jan. 10th, 2010 2:08 PM
Revision 1 of 1
Beginner bullet888
Tags C
Comments 1 comments
implementacija binarnog stabla pomoću polja i pomoću pokazivača

//pomoću polja


typedef int labeltype;

struct cvor{
        labeltype label;
        int kor;
};
struct BS{
        cvor elements[10000];
};
typedef BS BStablo;
typedef int node;
node ParentB(node n, BStablo *T){
     node index = n / 2;
     if (n = 1){
           return 0;
        }       
         else{
          return index;
          }
}
node LeftChildB(node n,BStablo *T){
     node index = 2 * n;
     if (T->elements[index].kor){
                                  return index;
    }
         else {
          return 0;
    }
}
node RightChildB(node n,BStablo *T){
     node index = 2 * n + 1;
     if (T->elements[index].kor){
                                  return index;
    }
         else{
          return 0;
    }
}
labeltype LabelB(node n,BStablo *T){
        if (T->elements[n].kor){
                            return T->elements[n].label;
    }
        else {
                return 0;
        }
}
void ChangeLabelB(labeltype x, node n, BStablo *T){     
        if (T->elements[n].kor){
                            T->elements[n].label = x;
    }
        else{
                cout << "Cvor ne postoji!" << endl;
    }
}
node RootB(BStablo *T){
        if (!T->elements[1].kor){
                             cout << "Stablo je prazno!" << endl;
                                     return 0;
        }
    else{
                return 1;
    }
}
void CreateLeftB(labeltype x, node n, BStablo *T){
        node index = 2 * n;
        if (!T->elements[index].kor){
                                 T->elements[index].kor = 1;
                                         T->elements[index].label = x;
        }
    else{
                cout << "Lijevo dijete vec postoji!" << endl;
    }
}
void CreateRightB(labeltype x, node n, BStablo *T){
        node index = 2 * n + 1;
        if (!T->elements[index].kor){
                                 T->elements[index].kor = 1;
                                         T->elements[index].label = x;
        }
    else{
                cout << "Desno dijete vec postoji!" << endl;
    }
}

void DeleteB(node n, BStablo *T){
        node i; 
        if (T->elements[n].kor)
    {
                if (T->elements[2 * n].kor);
                if (T->elements[2 * n + 1].kor);
                T->elements[n].kor = 0;
        }
    else{
                cout << "Cvor ne postoji!" << endl;
    }
 }
void InitB(node x, BStablo *T) {       
        for (int i = 0; i <= 9999; i++){     
                T->elements[i].kor = 0;
    }      
        T->elements[1].label = x;
        T->elements[1].kor = 1;

}

//pomo&#263;u pokaziva&#269;a


struct cvor {
        labeltype label;
        struct cvor *left, *right;
};
typedef struct cvor *node;
typedef struct cvor BStablo;
node ParentB(node n, BStablo *T){
        node x=T;
        if(n==T){
             cout<<"Ne postoji roditelj!"<<endl;
                     return 0;
        }
    else if(x==NULL){
                return NULL;
        }
    else if(n==x->left || n==x->right) {
                return x;
        }
    else{
                x=ParentB(n,x->left);
                if(x==false){
                        x=ParentB(n,T->right);
        }     
        }
        return x;
}
node LeftChildB(node n, BStablo *T){
        return n->left;
}
node RightChildB(node n, BStablo *T){
        return n->right;
}
labeltype LabelB(node n, BStablo *T){
        return n->label;
}
void ChangeLabelB(labeltype x, node n, BStablo *T){
        n->label=x;
}
node RootB(BStablo *T){
        return T;
}
void CreateLeftB(labeltype x, node n, BStablo *T){
        if(LeftChildB(n,T)){
                        cout<<endl;               
                                cout<<"Vec postoji lijevo dijete!"<<endl;
                                return 0;
        }
    else {
                node novi=new cvor;
                novi->label=x;
                novi->left=NULL;
                novi->right=NULL;
                n->left=novi;
        }
}
void CreateRightB(labeltype x, node n, BStablo *T){
        if(RightChildB(n,T)){
                         cout<<endl;
                         cout<<"Vec postoji desno dijete!"<<endl;
                                 return 0;
        }
    else {
                node novi=new cvor;
                novi->label=x;
                novi->left=NULL;
                novi->right=NULL;
                n->right=novi;
        }
}
void DeleteB(node n, BStablo *T){
        if (n==T){
              cout<<endl;     
                      cout<<"Ne moete obrisati korijen stabla!";
                      return 0;
        }
        if (n->left){
                 DeleteB(n->left, T);
        }
        if (n->right){
                  DeleteB(n->right, T);
        }
        if (n->left==NULL && n->right==NULL){
                      node x=ParentB(n,T);
                      if(x->left==n){
                                     x->left=NULL;
                      }
                else{
                        x->right=NULL;
        }
                delete n;
        }
}
void InitB(labeltype x, BStablo *T){
        T->label=x;
        T->left=NULL;
        T->right=NULL;
}
 

matej bulić

Comments

Comments komentar
Mon. Jan. 11th, 2010 12:26 PM    Beginner bullet888

Voting

Votes Up


Votes Down