Return to Snippet

Revision: 38692
at January 6, 2011 06:55 by mazorkovi


Initial Code
#include <iostream>
#include <windows.h>

#define LAMBDA                0
#define GRESKA               -1
#define MAX_EL              200
#define PUNO                  1
#define PRAZNO                0
#define MAX_DULJINA          10
#define LAMBDAO              -1
#define MAX_VELICINA_POLJA   18
#define RAZMAK_X              5
#define RAZMAK_Y              2
typedef int labeltype;

#include "bstablo_pokazivac.h"
#include "bstablo_polje.h"
#include "ostablo.h"

using namespace std;

#ifdef BSTABLO_POLJE_H
void ispis(btree t){
    if(t==NULL) return;
    node n;
    for(n=0; n<MAX_EL;n++)
       if(t->elements[n].used==1) 
           cout << n << " :: " << LabelB(n,t) << endl;
}
#endif

void Dodaj_element(labeltype vrijednost,node n,btree T){
    if(T==NULL) return; 
    if(vrijednost < LabelB(n,T)){
         if(ExistsLeftChildB(n,T))
              Dodaj_element(vrijednost,LeftChildB(n,T),T);
         else
              if(CreateLeftB(vrijednost,n,T)==GRESKA) cout << "Greska" << endl;
    }
    else if(vrijednost > LabelB(n,T)){
         if(ExistsRightChildB(n,T))
              Dodaj_element(vrijednost,RightChildB(n,T),T);
         else
              if(CreateRightB(vrijednost,n,T)==GRESKA) cout << "Greska" << endl;
    }
}
void Ispis_stabla(node n,btree T){
   if(T==NULL) return;
   cout << " " << n <<  "  :  " << LabelB(n,T) << endl;
   if(ExistsLeftChildB(n,T)){
       Ispis_stabla(LeftChildB(n,T),T);
   }
   if(ExistsRightChildB(n,T)){
       Ispis_stabla(RightChildB(n,T),T);
   }   
}
node Vrati_adresu(labeltype v,node n,btree T){
  if(T==NULL) return NULL;
  while(n != LAMBDA){
       if(v == LabelB(n,T)) return n;
       else if(v < LabelB(n,T)){
            if(ExistsLeftChildB(n,T))
                 n = LeftChildB(n,T);
            else return 0;
       }
       else if(v > LabelB(n,T)){
            if(ExistsRightChildB(n,T))
                 n = RightChildB(n,T);    
            else return 0;
       }
  }
  return 0;
}
void Obrisi_vrijednost(labeltype vrijednost,node n,btree T){
   if(T==NULL) return;
   if(vrijednost == LabelB(n,T)) DeleteB(n,T);
   else{
       if(ExistsLeftChildB(n,T)){
           Obrisi_vrijednost(vrijednost,LeftChildB(n,T),T);
       }
       if(ExistsRightChildB(n,T)){
           Obrisi_vrijednost(vrijednost,RightChildB(n,T),T);
       }   
   }
}
void Premjesti_zapis(node n,btree T,btree pom){
   if(T==NULL || pom==NULL) return;
   Dodaj_element(LabelB(n,T),RootB(pom),pom);
   if(ExistsLeftChildB(n,T))
          Premjesti_zapis(LeftChildB(n,T),T,pom);
   if(ExistsRightChildB(n,T))
          Premjesti_zapis(RightChildB(n,T),T,pom);
}

void gotoxy(int x,int y){
    COORD coord;
    coord.X=x;    
    coord.Y=y;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord); 
}

void ispisi_sve(nodao n,tree *t){
    nodao cvor = FirstChildT(n,t);
    cout << "  " << LabelT(n,t) << endl;
    while(cvor!=LAMBDAO){
        ispisi_sve(cvor,t);
        cvor=NextSiblingT(cvor,t);
    }
}
int prebroji(nodao n,tree *t){
    int br=0;
    nodao cvor = FirstChildT(n,t);
    while(cvor!=LAMBDAO){
          br++;
          cvor = NextSiblingT(cvor,t);
    }
    return br;
}
void nacrtaj(int x,int y,nodao n,tree *t){
    int pomak = prebroji(n,t);
    nodao cvor = FirstChildT(n,t);
    x-=(pomak*pomak);
    gotoxy(x,y); 
    cout << LabelT(n,t);
    y+=RAZMAK_Y;
    while(cvor!=LAMBDAO){ 
        x+=RAZMAK_X;
        nacrtaj(x-pomak*4,y,cvor,t);
        cvor = NextSiblingT(cvor,t);
    }
}


int main(){
    
    tree *t;
    t=InitT("0",t);
    
    int c, i=1, izb, minus=0;
    char str[10];
    char error[MAX_DULJINA];
    strcpy(error,"Greska ");
    
    int root,vrj,bri,mj1,mj2;
    btree stablo,pom;
    stablo = NULL;
    pom = NULL;
    node adr1,adr2;
    bool jep;
    
    do{
        system("cls");
        printf(" MENI\n\n 1. Unesi korijen BS\n 2. Dodaj element u BS\n 3. Ispis BS\n");
        printf(" 4. Obrisi cvor BS\n 5. Trazi roditelja u BS\n 6. Zamijeni vrijednost cvora BS\n");
        printf(" 7. Dodaj element u OS\n 8. Ispis OS\n");
        printf(" 9. Obrisi vrijednost OS\n 0.Exit\n");
        printf("\n -------------------------------\n > ");
        cin >> izb;
        printf("\n\n");
        switch(izb){
            case 1: if(ExistsLeftChildB(RootB(stablo),stablo) || ExistsRightChildB(RootB(stablo),stablo)) break;
                    cout << "Vrijednost korjena: "; cin >> root;
                    stablo = InitB(root,stablo);
                    break;
            case 2: printf(" ##################################################\n");
                    printf(" #    -1 prekida unos / ispisuje se svaki put     #\n");
                    printf(" ##################################################\n\n");
                    do{
                       cout << "Vrijednost: "; cin >> vrj;
                       if(vrj!=-1) {
                                   Dodaj_element(vrj,RootB(stablo),stablo);
                                   cout<<"\n*****************\n";
                                   }
                       else{
                       Ispis_stabla(RootB(stablo),stablo);
                       printf("\n*****************\n");
                       }
                    }while(vrj != -1);
                    break;
           case 3: printf(" ##################################################\n");
                   printf(" # ispis je   PREORDER                            #\n");
                   printf(" # ifdef BSTABLO_POLJE_H > ispis indeksa polja    #\n");
                   printf(" ##################################################\n\n");
                   Ispis_stabla(RootB(stablo),stablo);
                   #ifdef BSTABLO_POLJE_H
                   printf("\n\n -- polje --\n\n");
                   ispis(stablo);
                   #endif
                   break;
           case 4: printf("Brisi vrijednost: "); scanf("%d",&bri);
                   jep=false;
                   if(bri==LabelB(RootB(stablo),stablo)) jep=true;
                   Obrisi_vrijednost(bri,RootB(stablo),stablo);
                   if(jep){
                       cout << "Vrijednost korjena: "; cin >> root;
                       stablo = InitB(root,stablo);                                                              
                   }
                   Ispis_stabla(RootB(stablo),stablo);
                   break;
           case 5: cout << "Roditelj od: "; cin >> vrj;
                   adr1 = Vrati_adresu(vrj,RootB(stablo),stablo);
                   cout << "Adresa trazenog:  " << adr1 << endl;
                   adr2 = ParentB(adr1,stablo);
                   cout << "Adresa roditelja: " << adr2 << endl;
                   if(adr1 == 0){
                         printf("Nema tog elementa\n"); 
                         break;
                   }
                   if(adr2 == 0){
                         printf("Korjen\n");
                         break;
                   }
                   printf("   je ->  %d\n",LabelB(adr2,stablo));
                   break;
           case 6: if(stablo==NULL) break;
                   Ispis_stabla(RootB(stablo),stablo);
                   cout << "\nPromjeniti vrijednost: "; cin >> mj1; 
                   cout << "Na vrijednost: "; cin >> mj2;
                   adr1 = Vrati_adresu(mj1,RootB(stablo),stablo);
                   if(adr1==NULL){
                        printf("Nema te vrijednosti\n");
                        break;
                   }
                   ChangeLabelB(mj2,Vrati_adresu(mj1,RootB(stablo),stablo),stablo);
                   if(pom!=NULL) Obrisi_vrijednost(LabelB(RootB(pom),pom),RootB(pom),pom);
                   pom = InitB(LabelB(RootB(stablo),stablo),pom);
                   Premjesti_zapis(RootB(stablo),stablo,pom);
                   Obrisi_vrijednost(LabelB(RootB(stablo),stablo),RootB(stablo),stablo);
                   stablo = InitB(LabelB(RootB(pom),pom),stablo);
                   Premjesti_zapis(RootB(pom),pom,stablo);
                   break;
           case 7: 
                   system("cls");
                   do{
                         gotoxy(0,0);
                         cout << "> ";  cin >> c;
                         system("cls");
                         if(c!=-1 && c<i-minus){
                             itoa(i++,str,10);
                             if(CreateT(str,c,t)){
                                  gotoxy(1,1);
                                  strcat(error,str);
                                  cout << error << endl;
                                  strcpy(error,"Greska ");
                             }
                             nacrtaj(60,2,RootT(t),t);  
                         }          
                     }while(c!=-1);
                     nacrtaj(60,2,RootT(t),t);
                     break;
           case 8: 
                   system("cls");
                   ispisi_sve(0,t);
                   break;
           case 9:  
                    system("cls");
                    nacrtaj(60,2,RootT(t),t);
                    gotoxy(0,0); cout <<"B> "; cin >> c;
                    minus+=prebroji(c,t);
                    DeleteT(c,t);
                    system("cls");
                    nacrtaj(60,2,RootT(t),t);
        } /*switch*/
        //printf("\n\n--------------------\n");
        system("pause>NUL");
    }while(izb!=0);
}

Initial URL

                                

Initial Description

                                

Initial Title
Z4 Glavni program

Initial Tags

                                

Initial Language
C++