Return to Snippet

Revision: 38551
at January 10, 2011 08:58 by marsklepi


Updated Code
#include <iostream>
using namespace std;
typedef int labeltype;

struct element{
   labeltype label;
   bool iskoristen;
};

struct bs{
  element elementi[10000];
};

typedef bs stablo;
typedef int cvor;

cvor ParentB(cvor n, bs *stablo){
   if(n==1){
      return -1;
      }
   else return (int)(n/2);
};

cvor LeftChildB(cvor n, bs *stablo){
    if(stablo->elementi[2*n].iskoristen){
        return 2*n;
}
    else{
         cout<<"Nema lijevog djeteta.."<<endl;
         return(-1);
         }
};

cvor RightChildB(cvor n, bs *stablo){
    if(stablo->elementi[2*n+1].iskoristen){
                                            return (2*n+1);
                                            }
    else{
         cout<<"Nema desnog djeteta..";
         return(-1);
    }
};

labeltype LabelB(cvor n, bs *stablo){
    if(!stablo->elementi[n].iskoristen){
                                        cout<<"Nepostojeci cvor.."<<endl;
                                        return 0;
                                        }
    else return stablo->elementi[n].label;
};

void ChangeLabelB(labeltype xy, cvor n, bs *stablo){
    if(stablo->elementi[n].iskoristen){
                                       stablo->elementi[n].label=xy;
                                       return;
                                       }
    else cout<<"Nepostojeci cvor.."<<endl;
};

cvor RootB(bs *stablo){
   return 1;
};

void CreateLeftB(labeltype xy,cvor n, bs *stablo){
     if(!stablo->elementi[2*n].iskoristen){
        stablo->elementi[2*n].label=xy;
        stablo->elementi[2*n].iskoristen=true;
        }
     else cout<<"Lijevo dijete vec postoji.."<<endl;
};

void CreateRightB(labeltype xy,cvor n, bs *stablo){
    if (!stablo->elementi[2*n+1].iskoristen){
       stablo->elementi[2*n+1].label=xy;
       stablo->elementi[2*n+1].iskoristen=true;
}
   else cout<<"Desno dijete vec postoji.."<<endl;
};

void DeleteB(cvor n, bs *stablo){
   if(stablo->elementi[n*2].iskoristen) DeleteB(n*2, stablo);
   if(stablo->elementi[n*2+1].iskoristen) DeleteB(n*2+1, stablo);
   stablo->elementi[n].iskoristen=false;
   return ;
};

void InitB(bs *stablo, labeltype xy){
   stablo->elementi[1].label=xy;
   stablo->elementi[1].iskoristen=true;
   stablo->elementi[0].iskoristen=false;
   for (int i=0; i<10000; i++)
       stablo->elementi[i].iskoristen=false;
};

Revision: 38550
at January 5, 2011 01:43 by marsklepi


Initial Code
#include <iostream>
using namespace std;
typedef int labeltype;

struct element{
   labeltype label;
   bool iskoristen;
};

struct bs{
  element elementi[10000];
};

typedef bs stablo;
typedef int cvor;

cvor ParentB(cvor n, bs *stablo){
   if(n==1){
      return -1;
      }
   else return (int)(n/2);
};

cvor LeftChildB(cvor n, bs *stablo){
    if(!stablo->elementi[2*n].iskoristen){
        return 2*n;
}
    else{
         cout<<"Nema lijevog djeteta.."<<endl;
         return(-1);
         }
};

cvor RightChildB(cvor n, bs *stablo){
    if(stablo->elementi[2*n+1].iskoristen){
                                            return (2*n+1);
                                            }
    else{
         cout<<"Nema desnog djeteta..";
         return(-1);
    }
};

labeltype LabelB(cvor n, bs *stablo){
    if(!stablo->elementi[n].iskoristen){
                                        cout<<"Nepostojeci cvor.."<<endl;
                                        return 0;
                                        }
    else return stablo->elementi[n].label;
};

void ChangeLabelB(labeltype xy, cvor n, bs *stablo){
    if(stablo->elementi[n].iskoristen){
                                       stablo->elementi[n].label=xy;
                                       return;
                                       }
    else cout<<"Nepostojeci cvor.."<<endl;
};

cvor RootB(bs *stablo){
   return 1;
};

void CreateLeftB(labeltype xy,cvor n, bs *stablo){
     if(!stablo->elementi[2*n].iskoristen){
        stablo->elementi[2*n].label=xy;
        stablo->elementi[2*n].iskoristen=true;
        }
     else cout<<"Lijevo dijete vec postoji.."<<endl;
};

void CreateRightB(labeltype xy,cvor n, bs *stablo){
    if (!stablo->elementi[2*n+1].iskoristen){
       stablo->elementi[2*n+1].label=xy;
       stablo->elementi[2*n+1].iskoristen=true;
}
   else cout<<"Desno dijete vec postoji.."<<endl;
};

void DeleteB(cvor n, bs *stablo){
   if(stablo->elementi[n*2].iskoristen) DeleteB(n*2, stablo);
   if(stablo->elementi[n*2+1].iskoristen) DeleteB(n*2+1, stablo);
   stablo->elementi[n].iskoristen=false;
   return ;
};

void InitB(bs *stablo, labeltype xy){
   stablo->elementi[1].label=xy;
   stablo->elementi[1].iskoristen=true;
   stablo->elementi[0].iskoristen=false;
   for (int i=0; i<10000; i++)
       stablo->elementi[i].iskoristen=false;
};

Initial URL


Initial Description


Initial Title
binarno stablo pomocu polja

Initial Tags


Initial Language
C++