#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;


struct dugum{

int sayi;

dugum *sol;

dugum *sag;

};

struct agac{

dugum *kok;

int elemansayisi;

void agacolustur();

void ekle(dugum *);

void yazdir();

void preorder(dugum *);

void inorder(dugum *);

void postorder(dugum *);

};



void agac::preorder(dugum *p){

if(p){

cout << p->sayi << ”  “;

preorder(p->sol);

preorder(p->sag);

}

}



void agac::inorder(dugum *p){

if(p){

inorder(p->sol);

cout << p->sayi << ”  “;

inorder(p->sag);

}

}

void agac::postorder(dugum *p){

if(p){

postorder(p->sol);

postorder(p->sag);

cout << p->sayi << ”  “;

}

}

void agac::agacolustur(){

kok = NULL;

elemansayisi = 0;

cout << “Agac olusturuldu! ” <<endl;

}



void agac::ekle(dugum *eklenecek){

bool eklendi = false;

dugum *tara; dugum *yeni = new dugum;

tara = kok;

*yeni = *eklenecek; yeni->sol = NULL; yeni->sag = NULL;



if(kok == NULL){

kok = yeni;

cout << “Ilk eleman eklendi ! (” << kok->sayi << “)\n”<<endl;

elemansayisi++;

return;

}

while((tara != NULL) && (!eklendi))

{

if( yeni->sayi < tara->sayi)

{

if(tara->sol != NULL) { tara = tara->sol; }

else

{

cout << tara->sayi << ” dugumunun soluna ” << yeni->sayi << ” elemanini ekledim.\n” << endl;

tara->sol = yeni;

eklendi = true;

}

}

else if ( yeni->sayi > tara->sayi)

{

if(tara->sag != NULL) tara = tara->sag;

else

{

cout << tara->sayi << ” dugumunun sagina ” << yeni->sayi << ” elemanini ekledim.\n” << endl;

tara->sag = yeni;

eklendi = true;

}

}else { cout << “Aynı Değeri Girdiniz\n” << endl;  return;}

}

if(eklendi == true) { elemansayisi++; }

cout << “\nEleman sayisi : ” << elemansayisi  << endl  ;

cout << “PREORDER :\t\t”; preorder(kok);

cout << endl;

cout << “INORDER :\t\t”; inorder(kok);

cout << endl;

cout << “POSTORDER :\t\t”; postorder(kok);

cout << endl;

}



int main(){

typedef agac veriyapisi;

veriyapisi kayit;

dugum yenikayit;

kayit.agacolustur();



for(int i=1;i<10 ;i++){

cout << endl << “Eleman : “;

cin>>yenikayit.sayi;

kayit.ekle(&yenikayit);

}
system(“pause”);

return 0;

}

Facebook'dan yorumla