#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <math.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;     
}

int derinlik(dugum *kok){
    if(kok==NULL)
                 return 0;
 if(kok->sol==NULL&& kok->sag ==NULL)
                     return 1;
 else if (kok->sol==NULL)
                     return derinlik(kok->sag)+1;
 else if (kok->sag==NULL)
                     return derinlik(kok->sol)+1;
 else if(derinlik(kok->sag) > derinlik (kok->sol))
                       return 1  + derinlik(kok->sag);
 else
                       return 1 + derinlik(kok->sol);   
    
}

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

      }
      
}

void bastir2(dugum * kok[],int boyut){
     int nullsayisi=0;
          for(int i = 0;i<boyut;i++){
           for(int j = 0;j< 2*derinlik(kok[i])+1;j++)
             printf(" ");
           if(kok[i]==NULL){
                            nullsayisi++;
                            printf(" ");
           }
           else{
                printf("%d",kok[i]->deger);
           }
          }
          if(nullsayisi==boyut)
                               return;
          printf("\n");
          dugum ** kok2 = (dugum **) malloc(sizeof(dugum) * boyut*2);
          for(int i = 0;i<boyut;i++){
                  if(kok[i]==NULL){
                       kok2[2*i]=NULL;
                       kok2[2*i+1]=NULL;            
                  }
                  
                  kok2[2*i]=kok[i]->sol;
                  kok2[2*i+1]=kok[i]->sag;       
          }
          bastir2(kok2,boyut*2);
}     
   
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 *deneme = NULL;
    deneme = ekle(deneme, 10);
    deneme = ekle(deneme, 5);
    deneme = ekle(deneme, 15);
    deneme = ekle(deneme, 7);
    deneme = ekle(deneme, 3);
    deneme = ekle(deneme, 17);
    deneme = ekle(deneme, 13);
    deneme = ekle(deneme, 1);
    deneme = ekle(deneme, 14);
    deneme = ekle(deneme, 6);
    deneme = ekle(deneme, 8);
    deneme = ekle(deneme, 11);
    deneme = ekle(deneme, 14);
    deneme = ekle(deneme, 16);
    deneme = ekle(deneme, 25);
     dugum *kokdizi[1];
 kokdizi[0]=deneme;
    bastir2(kokdizi ,1);
    getch();
 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);

 //bastir2(kokdizi,1);
  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,1);
 printf("\n ---------------- \n");
 getch();
 dugum * kok3 = topla(kok,kok2);
 bastir (kok3,1);
 printf("kok 1 : %d , kok 2 : %d , kok 3 : %d ",derinlik(kok),derinlik(kok2),derinlik(kok3));
 getch();
 return 0;   
}

