Treceți la conținutul principal

arbore binar cpp

#include<iomanip.h>
#include<malloc.h>
#include<iostream.h>
#include<process.h>
#define tip_element int
class nod
{
 friend class arbin;
 tip_element info;
 nod *ss,*sd;

public:
 nod (tip_element k, nod *s=NULL,nod *d=NULL):info(k),ss(s),sd(d){}
 /*friend ostream &operator<<(ostream &os,nod *n)
 {
  os<<nod->info;
  return os;
 }*/
};

class  arbin
{
private:
 nod *rad;
 int nrn;
 nod *ins(nod*,tip_element);
 void sterge_arb(nod*);
 void preord(nod*);
 void inord(nod *);
 void *copiere(nod *);
 void sterge_nod(nod *&,tip_element);
 tip_element sterg(nod *&);
 bool egal(nod *,nod*);
 int inalt (nod *);
 bool cauta(nod *, tip_element);
 bool frunza(nod *r) {return r->ss=NULL && r->sd==NULL;}
 friend int max(int a,int b){ return a>b?a:b;}
public:
 arbin():rad(NULL),nrn(0){}
 void afis_RSD(){preord(rad);}
 void afis_SRD(){inord(rad);}
 int count(){ return nrn;}
 bool este_vid(){return rad=NULL;}
 int inaltimea(){return inalt(rad);}
 arbin & operator<<(tip_element k) { rad=ins(rad,k);return *this;}
     arbin &operator= (arbin &a);
     arbin &operator -= (tip_element k)  { sterge_nod (rad,k);return *this;}
  bool operator == (arbin &a) {return egal(rad,a.rad);}
  bool operator == (tip_element k) { return cauta(rad,k);}
    friend ostream & operator <<(ostream & os,arbin &a)
 {
  a.afis_RSD();return os;
 }
 ~arbin() {sterge_arb(rad);}
};
  
int arbin::inalt(nod *r)
{
 if(r) return 1 + max(inalt(r->ss),inalt(r->sd));
 else return 0;
}

void arbin::preord(nod *r)
{
 if(r)
 {
  cout<<r;
  if(!frunza(r))
  {
   cout<<"(";
   preord(r->ss);
   cout<<",";
   preord(r->sd);
   cout <<")";
  }
 }
 else cout<<".";
}


void arbin::inord(nod *r)
{
 if(r)
 {
  if(frunza(r))
  { cout<<"(" ;inord(r->ss);cout<<")";}
      cout<<r;
   if(!frunza(r)){cout<<"(";inord(r->sd);cout<<")";}
 }
else cout<<"."; 
}

nod *arbin::ins(nod *r,tip_element k)
{
 if(r)
 {
  if(k==r->info);
  else if(k>r->info) r->sd=ins(r->sd,k);
   else r->ss=ins(r->ss,k);
   return r;
 }
 else return nrn++,new nod(k);
}
arbin & arbin::operator =(arbin &srs)
{
 if(rad) sterge_arb(rad);
 rad=NULL;
 nrn=srs.nrn;
 //rad=copiere(srs.rad);
 return *this;
}

void arbin::sterge_arb(nod *r)
{
 if(r)
 {
  sterge_arb(r->ss);
  sterge_arb(r->sd);
  delete r;
 }
}


nod *arbin::copiere(nod *r)
{

if(r) return new nod((r->info),copiere(r->ss),copiere(r->sd));
 else return NULL;
}

bool arbin::egal(nod *r1,nod *r2)
{
 if(!r1) return r2=NULL;
    else if(!r2) return false;
    else return r1->info=r2.info && egal(r1->ss,r2->ss) && egal(r1->sd,r2->sd);
}

bool arbin::cauta(nod *r,tip_element k)
{
 if(!r) return false;
 else if(k==r->info) return true;
  else if(k>r->info) return cauta(r->sd,k);
  else return cauta(r->ss,k);
}

//** stergerea unui nod dintr-un arbore

void arbin::sterge_nod(nod *&r,tip_element k)
{
  nod *aux;
if(!r) cout<<"\n Nodul inexistent!!"<<endl; else 
if(k=r->info)
 {
   aux =r;
   if(!aux->sd){ r=aux->ss;delete aux;}
   else if(!aux->ss) {r=aux->sd;delete aux;}
   else
     r->info=sterg(r->sd);
 }
else if(k>r->info) sterge_nod(r->sd,k);
  else sterge_nod(r->ss,k);
}

void main()
{
 arbin a,b;

 a<<56<<12<<34<<90<<67<<80<<110;
 a.afis_RSD();
 cout<<"ARborele are" <<a.count()<<"noduri!"<<endl;
 cout<<a<<endl;
 b=a;b.afis_SRD();
 //testare daca doi arbori sunt identici
 if(a==b) cout<<"\nArbbori identici!"<<endl;
 else cout<<"\nArbori diferiti!!"<<endl;
 //determinare ianltimii unuio arbore
 cout<<"\nInaltimea arborelui este de"<<a.inaltimea()<<"niveluri";
}
























  
























Comentarii

Postări populare de pe acest blog

WINDOWS 10 COMPUTER FREEZING PROBLEM SOLVED

good news : a BIOS UPDATE can resolve the problem but just for a Windows 7 on 64 bits o.s. and the system is not stable all the time. even after  bios update the system can freeze.
new info : u can try to low the screen brightness and see if this error appear so often after 
news: last info !!! maybe a virus. scann our system now with an antivirus i generate this error using other device ( a tablet pc) connected in the same network and the laptop i have this problem just freeze  http://thehackernews.com/2013/10/backdoor-found-in-chinese-tenda.html

news : if u use a tenda router this make couse all this problems



what i discover so far :
1.the electric company have many failure and affect the main ISP router/switch for building  also the router/switch installed by the ISP may be affected by overheating and will crash after a long utilisation on heat conditions 2.the router/switch of ISP affect any router of the user between this router and pc/laptop of client 3.the router and any other device of t…

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='+'…

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…