Treceți la conținutul principal

arbore binar clasa cpp

#include <iostream.h>
#include <conio.h>
#include <stdio.h>
#include <malloc.h>
#include <string.h>
int m[1000][1000];  //pentru salvarea arborelui intr-un fisier


struct nod
 {
  int info;
  char cuv[20];
  nod *stg,*drt;
 };

class arbbin
 {
   nod *rad;
   int stergere(nod *&);
   void stergere_nod(nod*&,int);
  public:
   arbbin()
    {rad=NULL;};
   ~arbbin()
    {rad=NULL;};
   void traversare_srd(nod*);
   void srd();
   void traversare_rsd(nod *);
   void rsd();
   void traversare_sdr(nod *);
   void sdr();
   int sumaFrunze(nod*);
   int sFrunza();
   int numara(nod *);
   int numara_nod();
   void print(nod *);
   void tiparire();
   void salvare();
   nod *inserare_nod(nod *,int );
   void operator + (int);
   void operator - (int);
   arbbin &operator >(FILE *);
   arbbin &operator <(FILE *);
   void inserare_cuv(nod *& ,char*);
   void insert(char *);
   friend ostream &operator <<(ostream &,arbbin&);
   friend istream &operator >>(istream &,arbbin&);
   nod *operator [] (int);
  
   };




nod *arbbin::inserare_nod(nod *rad,int k)
 {
  if (rad)
   {
    if (k<rad->info) rad->stg=inserare_nod(rad->stg,k);
    else
     if (k>rad->info) rad->drt=inserare_nod(rad->drt,k);
      else  printf("\nNodul exista in arbore!");
    return rad;
   }
  else 
   {
    nod *p=new nod;
    p->stg=NULL;
    p->drt=NULL;
    p->info=k;
    return p;
   }
 }

void arbbin::operator +(int k)
 {
  rad=inserare_nod(rad,k);
 }

void arbbin::traversare_srd(nod *rad)
 {
  if (rad)
   {
    traversare_srd(rad->stg);
    printf("  %d",rad->info);
    traversare_srd(rad->drt);
   }
 }

void arbbin::srd()
 {
  nod *p;
  p=rad;
  traversare_srd(p); 
 }


void arbbin::traversare_rsd(nod *rad)
 {
  if (rad)
   {
    printf("  %d",rad->info);
    traversare_rsd(rad->stg);
    traversare_rsd(rad->drt);
   }
 }


void arbbin::rsd()
 {
  nod *p;
  p=rad;
  traversare_rsd(p);
 }



void arbbin::traversare_sdr(nod *rad)
 {
  if (rad)
   {
    traversare_sdr(rad->stg);
    traversare_sdr(rad->drt);
    printf("  %d",rad->info);
   }
 }



void arbbin::sdr()
 {
  nod *p;
  p=rad;
  traversare_sdr(p);
 }

 

void arbbin::print(nod *rad)
 {
  if (rad)
   {
    printf("%d",rad->info);
    if((rad->stg)||(rad->drt))
     {
      printf("(");
      print(rad->stg);
      printf(",");
      print(rad->drt);
      printf(")");
     }
   }
  else 
   printf("-");
 }



void arbbin::tiparire()
 {
  nod *p;
  p=rad;
  print(p);
 }


int arbbin::sumaFrunze(nod *rad)
 {
  if (rad)
   if(!rad->stg&&!rad->drt)
    return rad->info;
   else 
    return sumaFrunze(rad->stg)+sumaFrunze(rad->drt);
  else
   return 0;
 }

int arbbin::sFrunza()
 {
  nod *p;
  p=rad;
  return sumaFrunze(p);
 }


int arbbin::numara(nod *rad)
 {
  if (rad)
   return 1+numara(rad->stg)+numara(rad->drt);
  else return 0;
 }

int arbbin::numara_nod()
 {
  nod *p;
  int nr;
  p=rad;
  nr=numara(p);
  return nr;
 }





void arbbin::stergere_nod(nod *&rad,int inf)
 {
  nod *aux;
  if (!rad) printf("\nNodul nu exista in arbore!");
  else 
   if (inf<rad->info) stergere_nod(rad->stg,inf);
   else 
    if (inf>rad->info) stergere_nod(rad->drt,inf);
     else
      {
       aux=rad;
       if (!aux->stg)
        {
         rad=aux->stg;
         delete(aux);
        }
       else
        if(!aux->drt)
         {
          rad=aux->drt;
          delete(aux);
         }
        else
         rad->info=stergere(rad->stg);

     }
 }


int arbbin::stergere(nod *&p)
 {
  if (p->stg)
   return stergere(p->stg);
  else
   {
    nod *q=p;
    int inf=q->info;
    p=p->stg;
    delete(q);
    return inf;
   }
 }



void arbbin::operator -(int inform)
 {
  nod *nou;
  nou=rad;
  stergere_nod(nou,inform);
 }



nod *arbbin::operator [] (int inf)
 {
  nod *aux;
  aux=rad;
  while(aux&&aux->info!=inf)
   { 
    if (inf<aux->info) 
     aux=aux->stg;
    else
     if (inf>aux->info) 
      aux=aux->drt;
   };
  if (aux&&aux->info==inf)
   cout<<"\nNodul cautat exista in arbore!";
  else
      cout<<"\nNodul cautat nu exista in arbore!";
  return aux;
 }


void arbbin::salvare()
 { 
  static int i,j;
  nod *aux;
  if (rad)
   {
       m[i][j]=rad->info;
    if (rad->stg)
     {
      i=2*i;
      j++;
      aux=rad;
                  rad=rad->stg;
      salvare();
                        rad=aux;
      j--;
      i=i/2;
     }
    if (rad->drt)
     {
      i=2*i+1;
      j++;
      aux=rad;
      rad=rad->drt;
      salvare();
      rad=aux;
      j--;
      i=(i-1)/2;
     }
   }
 }


arbbin &arbbin::operator <(FILE *f)  
 {
  int i,j;
  for(i=0;i<100;i++)
   for(j=0;j<100;j++)
    m[i][j]=0;
  salvare();
  for(i=0;i<100;i++)
   for(j=0;j<100;j++)
    if (m[j][i]!=0)
      fwrite(&m[j][i],sizeof(int),1,f);
  cout<<"\nArborele a fost salvat in fisier!";
  fclose(f);
  return *this;
 }



arbbin &arbbin::operator >(FILE *fis) 
{                                   
  int inf;
  nod *nou,*st,*dr;
  while (!feof(fis))
   {
    fread(&inf,sizeof(inf),1,fis);
    nou=new nod;
                nou->stg=st;
    nou->drt=dr;
    nou->info=inf;
    cout<<inf<<endl;
   }
  cout<<"\nArborele din fisier a fost reconstruit!";
  fclose(fis);
  return *this;         
 }



void arbbin::inserare_cuv(nod *&rad,char *cuvant)
 {
  if (!rad)
   {
    nod *nou=new nod;
    nou->stg=nou->drt=NULL;
    strcpy(nou->cuv,cuvant);
    rad=nou;
   }
  else 
   if (strcmp(cuvant,rad->cuv)<0) inserare_cuv(rad->stg,cuvant);
    else 
     if (strcmp(cuvant,rad->cuv)>0) inserare_cuv(rad->drt,cuvant);   
 }


void arbbin::insert(char *c)
 {
  nod *p;
  p=rad;
  inserare_cuv(p,c);
 }
 
istream &operator>>(istream &intrare ,arbbin &arb )
 { 
  char  text[400],*sep=" ,.?!:;'";
  char *ptext=text;
  fflush(stdin);
  printf("\nTextul dat este(se citeste pana la ^Z):");
  while (!feof(stdin))
   {
    gets(text);
    ptext=strtok(text,sep);
    while(ptext)
     {
      arb.insert(ptext);
      ptext=strtok(NULL,sep);
     }
   };
  return intrare;
 }


ostream &operator <<(ostream &iesire,arbbin &arb)
 {
  nod *p;
  arb.print(p);
  return iesire;
 }


void meniu()
 {

  cout<<"\n     1.ADAUGARE NOD DE LA TASTATURA";
        cout<<"\n     2.STERGERE NOD DE LA TASTATURA";
  cout<<"\n     3.SALVARE ARBORE IN FISIER";
  cout<<"\n     4.RESTAURARE ARBORE DIN FISIER";
  cout<<"\n     5.CAUTARE NOD IN ARBORE";
  cout<<"\n     6.TRAVERSARI ARBORE-SRD(),RSD(),SDR()";
  cout<<"\n     7.SUMA INFORMATIILOR DIN FRUNZE ";
  cout<<"\n     8.NUMARUL NODURILOR DIN ARBORE";
  cout<<"\n     9.AFISARE ARBORE";
  cout<<"\n     n.NUMAR CUVINTE DISTINCTE DINTR-UN TEXT DAT";  cout<<"\n     0.TERMINARE";
 }





void main()
{
 arbbin arb;
 int informatie;
 char optiune;
 arb+10;arb+7;arb+15;arb+9;arb+3;arb+8;arb+25;  
 meniu();
 optiune='1';
 while (optiune!='0')
  {
      cout<<"\nOptiunea dorita este:";
   cin>>optiune;
   if(((optiune>='0')&&(optiune<='9'))||(optiune=='n'))
   {
   switch(optiune)
    {
     case '1':
      char op;
       cout<<"\nNoul nod de introdus:";
       cin>>informatie;
       arb+informatie;
       cout<<"\nMai doriti adaugarea unui alt nod(d/n)?";
       cin>>op;
       while (op=='d')
        {
         cout<<"\nNoul nod de introdus:";
         cin>>informatie;
            arb+informatie;
         cout<<"\nSe va mai adauga alt nod(d/n)?";
         cin>>op;
        }
       getch();
       meniu();
      break;
     case '2':
      cout<<"\nNodul care se va sterge:";
      cin>>informatie;
      arb-informatie;
      getch();
      meniu();
      break;
     case '3':
      char nume[20];
      cout<<"\nNumele fisierului in care se va salva:";
      cin>>nume;;
      FILE *f;
      f=fopen(nume,"w+b");
      arb<f;  
      getch();
      meniu();
      break;
     case '4':
      char name[20];
      cout<<"\nNumele fisierului din care se va reconstrui arborele:";
      cin>>name;
      FILE *fi;
      if ((fi=fopen(name,"r+b"))!=NULL)
       arb>fi;
      else cout<<"\nFisierul nu exista!";
      getch();
      meniu();
      break;
     case '5':
      int elem;
      cout<<"\nNodul care va fi cautat:";
      cin>>elem;
      arb[elem];
      getch();
      meniu();
      break;
     case '6':
      printf("\nArborele traversat in SRD:");
      arb.srd();
      printf("\nArborele traversat in RSD:");
      arb.rsd();
      printf("\nArborele traversat in SDR:");
      arb.sdr();
      getch();
      meniu();
      break;
     case '7':
      printf("\nSuma informatiilor din frunze este:%d",arb.sFrunza());
      getch();
      meniu();
      break;
     case '8':
      cout<<"\nNumarul de noduri din arbore este:"<<arb.numara_nod();
      getch();
      meniu();
      break;
     case '9':
      printf("\nArborele tiparit in RSD:");
      arb.tiparire();
      getch();
      meniu();
      break;
     case 'n':
      { 
       int nr_anterioare=arb.numara_nod();
       cin>>arb;
       cout<<"\n\nNumarul de cuvinte distincte din text este:"<<arb.numara_nod()-nr_anterioare;
       cout<<"\nSe va reveni la arborele initial.";
      };
      getch();
      meniu();
      arb.~arbbin();
      arb+10;arb+7;arb+15;arb+9;arb+3;arb+8;arb+25;  
      break;
     case '0':
      break;
    }
   }
   else cout<<"\nOptiunea nu exista in meniu!";
  }
}

Comentarii

Postări populare de pe acest blog

program principal cpp

#include "clasa.h" #include <stdio.h> #include <conio.h> #include <stdlib.h> #include <string.h> #define DELAY 9000000 void delay() { for(long i=0;i<DELAY;i++); } //constructor cu initializare de la tastatura BigInt::BigInt() {char x; signed char t[400]; int i; printf("\nNumarul cu semn "); do s=getche(); while((s!='+')&&(s!='-')); n=0; do {x=getche(); t[n]=x-'0'; n++; } while((x>='0')&&(x<='9')); n--; for(i=0;i<n;i++) nr[i]=t[n-i-1]; } //constructor cu initializare prin parametri BigInt::BigInt(char semn,signed char numar[],int dim) {int i; s=semn; n=dim; for(i=0;i<n;i++) nr[i]=numar[n-i-1]; } //transform un int negativ in pozitiv int BigInt::Pozitiv(int x) {int a,vb; a=0; vb=0; while(vb==0) if((x+a)==0) vb=1; else a=a+1; x=a; return x; } //constructor dintr-un nr int obisnuit BigInt::BigInt(int x) {int i; if(x>=0) s='+'…

NUMERE PRIME ALGORITM C++

// NUMERE PRIME ALGORITM C++//  reediting from scratch //on this page is just the study for a next algoritm for generating the parime nr series like Fibonnaci or ....if possibile

74111121313417374414124343447 if u know the red part you can generate the orange part
1 0 1 111112222 1 1 23

o aplicatie php localitati romania

//APLICATIA SE REFERA LA BAZA DE DATE SIRUTA

//dragtable.js


/* dragtable v1.0 June 26, 2008 Dan Vanderkam, http://danvk.org/dragtable/ http://code.google.com/p/dragtable/ \Bsortabledraggable\B Instructions: - Download this file - Add <script src="dragtable.js"></script> to your HTML. - Add class="draggable" to any table you might like to reorder. - Drag the headers around to reorder them. This is code was based on: - Stuart Langridge's SortTable (kryogenix.org/code/browser/sorttable) - Mike Hall's draggable class (http://www.brainjar.com/dhtml/drag/) - A discussion of permuting table columns on comp.lang.javascript Licensed under the MIT license. */ // Here's the notice from Mike Hall's draggable script: //***************************************************************************** // Do not remove this notice. // // Copyright 2001 by Mike Hall. // See http…