bin_stablo_polje_sugy





0
Date Submitted Wed. Jan. 13th, 2010 2:51 PM
Revision 1 of 1
Beginner Sugy
Tags "CPlusPlus"
Comments 0 comments
Implementacija binarnog stabla pomoću polja

#include<iostream.h>
      struct elem {
             int v;
             bool koristen;
      };
      struct bst {
             struct elem elemi[40000];
      };
      typedef struct bst *bs;
      typedef int cv;
      cv InitI(cv k, bst *bs){
           int i;
           for(i=0; i<40000;i++)
                    bs->elemi[i].koristen=false;
           bs->elemi[0].v=korijen;
           bs->elemi[0].koristen=true;   
           return 0;
      };//InitI
      cv RootI(bst *bs){
           if(bs->elemi[0].koristen==true) return 0;
           else
               return -1;   
      };//RootI
      cv LeftChildI(cv ind, bst *bs){
           if(bs->elemi[(ind*2)+1].koristen==true) return (ind*2)+1;
           else
               return -1;
      };//LeftChild
      cv RightChildI(cv ind, bst *bs){
                if(bs->elemi[(inds*2)+2].koristen==true) return (ind*2)+2;
                else
                    return -1;
      };//RightChild
      cv ParentI(cv ind, bst *bs){
                if(ind < 1) return -1;
                if(bs->elemi[int((ind-1)/2)].koristen==true) return int((ind-1)/2);
                else
                    return -1;
      };//ParentI
      cv CreateLeftI(int v, cv ind, bst *bs){
           int x=(ind*2)+1;
           if(LeftChildI(ind, bs)==-1){
                                 bs->elemi[x].v=v;
                                 bs->elemi[x].koristen=true;
           }
           else{
                cout<<"Ovaj cvor ima lijevo dijete."<<endl;
           }
      };//CreateLeftI
      cv CreateRightI(int v, cv ind, bst *bs){
                int x=(ind*2)+2;
                if(RightChildI(ind, bs)==-1){
                                       bs->elemi[x].v=v;
                                       bs->elemi[x].koristen=true;                       
                }
                else{
                     cout<<"Ovaj cvor ima desno dijete."<<endl;
                }
      };//CreateRightI
       
      int LabelI(cv ind, bst *bs){
          if(bs->elemi[ind].koristen==true) return bs->elemi[ind].vr;
          else{
              cout<<"Cvor nema vrijednost."<<endl;
          }
      };//LabelI
      void ChangeLabelI(int v, cv ind, bst *bs){
           if(bs->elemi[ind].koristen==true){
                                                        bs->elemi[ind].v=v;
           }
           else{
                cout<<"Cvor nema vrijednost."<<endl;
           }
      };//ChangeLabelI
      void DeleteI(cv ind, bst *bs){
           if(bs->elemi[(ind*2)+1].koristen==true){
                                                              DeleteI((ind*2)+1, bs);
           }
           if(bs->elemi[(ind*2)+2].koristen==true){
                                                              DeleteI((ind*2)+2, bs);
           }
           bs->elemi[ind].koristen=false;
      };//DeleteI
// program je sli&#269;an sa ostalima jer se nema bas puno toga za mijenjati, samo sto koristim druge oznake i drugaciji redoslijed
 

Jurica Sugnetic

Comments

There are currently no comments for this snippet.

Voting

Votes Up


Votes Down