Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <conio.h>
- #include <stdlib.h>
- typedef struct nod
- {
- int valoare;
- struct nod *st;
- struct nod *dr;
- }nod;
- nod *rad1=NULL;
- nod *rad2=NULL;
- nod* adaugare1(nod *t, int v)
- {
- if(t==NULL){
- t=(nod*)malloc(sizeof(nod));
- t->valoare=v;
- t->st=NULL;
- t->dr=NULL;
- }
- else
- if(t->valoare>v)
- t->st=adaugare1(t->st,v);
- else
- if(t->valoare<v)
- t->dr=adaugare1(t->dr,v);
- else
- printf("Elementul deja exista in arbore.\n");
- return t;
- }
- nod* adaugare2(nod *t, int v)
- {
- if(t==NULL){
- t=(nod*)malloc(sizeof(nod));
- t->valoare=v;
- t->st=NULL;
- t->dr=NULL;
- }
- else
- if(t->valoare>v)
- t->st=adaugare2(t->st,v);
- else
- if(t->valoare<v)
- t->dr=adaugare2(t->dr,v);
- else
- printf("Elementul deja exista in arbore.\n");
- return t;
- }
- void citire1()
- {
- FILE *f;
- int v;
- f=fopen("in.txt","rt");
- if(f==NULL)
- printf("fisierul nu poate fi deschis\n");
- else{
- while(!feof(f)){
- fscanf(f,"%d",&v);
- rad1=adaugare1(rad1,v);
- }
- fclose(f);
- }
- }
- void citire2()
- {
- FILE *f;
- int v;
- f=fopen("in2.txt","rt");
- if(f==NULL)
- printf("fisierul nu poate fi deschis\n");
- else{
- while(!feof(f)){
- fscanf(f,"%d",&v);
- rad2=adaugare2(rad2,v);
- }
- fclose(f);
- }
- }
- void afisare1(nod *t)
- {
- if(t != NULL){
- afisare1(t->st);
- printf(" %d", t->valoare);
- afisare1(t->dr);
- }
- }
- void afisare2(nod *t)
- {
- if(t != NULL){
- afisare2(t->st);
- printf(" %d", t->valoare);
- afisare2(t->dr);
- }
- }
- nod *elim1(nod *t, nod *p)
- {
- nod *q, *q1;
- q = t;
- if(q->dr != NULL)
- {
- q->dr = elim1(q->dr, p);
- }
- else
- {
- q1 = q;
- p->valoare = q->valoare;
- q = q->st;
- }
- return q;
- }
- nod *elim2(nod *t, nod *p)
- {
- nod *q, *q1;
- q = t;
- if(q->dr != NULL)
- {
- q->dr = elim2(q->dr, p);
- }
- else
- {
- q1 = q;
- p->valoare = q->valoare;
- q = q->st;
- }
- return q;
- }
- nod *sterge1(nod *t, int valoare)
- {
- nod *q, *q1;
- if(t == NULL)
- printf("Nodul nu exista!");
- else
- if(t->valoare > valoare)
- t->st = sterge1(t->st, valoare);
- else{
- if(t->valoare < valoare)
- t->dr = sterge1(t->dr, valoare);
- else{
- if(t->st == NULL)
- {
- q1 = t;
- q = t->dr;
- free(q1);
- return q;
- }
- else
- if(t->dr == NULL)
- {
- q1 = t;
- q = t->st;
- free(q1);
- return q;
- }
- else
- t->st = elim1(t->st, t);
- }
- }
- return t;
- }
- nod *sterge2(nod *t, int valoare)
- {
- nod *q, *q1;
- if(t == NULL)
- printf("Nodul nu exista!");
- else
- if(t->valoare > valoare)
- t->st = sterge2(t->st, valoare);
- else{
- if(t->valoare < valoare)
- t->dr = sterge2(t->dr, valoare);
- else{
- if(t->st == NULL)
- {
- q1 = t;
- q = t->dr;
- free(q1);
- return q;
- }
- else
- if(t->dr == NULL)
- {
- q1 = t;
- q = t->st;
- free(q1);
- return q;
- }
- else
- t->st = elim2(t->st, t);
- }
- }
- return t;
- }
- int main(){
- citire1();
- citire2();
- printf("Arborii completi sunt: \n");
- afisare1(rad1);
- printf("\n");
- afisare2(rad2);
- sterge2(rad2,5);
- sterge1(rad1,4);
- printf("\nArborii dupa eliminarea elementelor dorite sunt: \n");
- afisare1(rad1);
- printf("\n");
- afisare2(rad2);
- getch();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement