#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
typedef struct dugum{
        int deger;
        dugum *sol;
        dugum *sag;
};

dugum *  ekle (dugum * kok , int eklenen){
          if(kok == NULL){
                 kok = (dugum *) malloc(sizeof(dugum));
                 kok ->deger = eklenen;
                 kok->sol =NULL;
                 kok->sag =NULL;
                 return kok;
          }
          else if (eklenen < kok ->deger){
               kok->sol = ekle(kok->sol , eklenen);
          }
          else if (eklenen > kok->deger ) {
               kok->sag =  ekle(kok->sag , eklenen);
          }
          else{
               // aynı değer ikinci kere bulundu
               }
          return kok;     
}

void bastir(dugum *kok, int bosluk){
       for(int i = 0;i< bosluk ; i++)
               printf("\t");
      printf("%d",kok->deger);
    
      if(kok->sol!= NULL){
                    printf("\n+");
                    bastir(kok->sol, bosluk);
      }
      if(kok->sag!= NULL)
      {
                   
                    bastir(kok->sag,++bosluk);
                    printf("\n");
      }
}

dugum * topla(dugum *kok1 , dugum *kok2){
      if(kok1==NULL&&kok2==NULL){
               return NULL;                        
      }
      else if(kok1!=NULL && kok2!=NULL){
           dugum *yeni = (dugum * )malloc (sizeof(dugum));
           yeni ->deger = kok1->deger + kok2->deger;     
           yeni ->sol = topla(kok1->sol, kok2->sol);
           yeni ->sag = topla(kok1->sag , kok2->sag);
           return yeni;
      }
      else if(kok1!=NULL && kok2 == NULL){
         
           return kok1;     
      }
      else{
           return kok2;
      }
      
}
int main(){
 dugum *kok =NULL;
 kok = ekle(kok,3);
 kok = ekle(kok,7);
 kok = ekle(kok,5);
 kok = ekle(kok,6);
 kok = ekle(kok,11);
 kok = ekle(kok,17);
 kok = ekle(kok,12);
 bastir(kok,0);
  printf("\n ---------------- \n");
 dugum *kok2 = NULL;
 kok2 = ekle(kok2,12);
 kok2 = ekle(kok2,8);
 kok2 = ekle(kok2,6);
 kok2 = ekle(kok2,21);
 kok2 = ekle(kok2,9);
 kok2 = ekle(kok2,3);
 kok2 = ekle(kok2,1);
 bastir(kok2,0);
 printf("\n ---------------- \n");
 getch();
 dugum * kok3 = topla(kok,kok2);
 bastir (kok3,0);
 getch();
 return 0;   
}

