binary search tree

Hi everybody. I make logical mistake but I don't find mistake.

my algorithm;

#include<iostream>

using namespace std;

struct node{
struct node *left;
struct node *right;
int data;
};

void add(node *p,int sayi){

if(p==NULL){
p=new node();
p->data=sayi;
p->left=NULL;
p->right=NULL;
}
else{
if(p->data>sayi)
{
if(p->left==NULL)
p->data=sayi;
else
add(p->left,sayi);
}
else{
if(p->right==NULL)
p->data=sayi;
else
add(p->right,sayi);
}
}
cout<<"sayi ="<<p->data;

}

void postorder(node *p){
if(p!=NULL)
{

if(p->left!=NULL)
postorder(p->left);
if(p->right!=NULL)
postorder(p->right);
cout<< p->data<<endl;
}
else{
cout<<"hata"<<endl;

}
}
void main(){
struct node *p =NULL;
int sayi=0;

while(sayi!=-1){
cout<<"Bir sayi giriniz...";
cin>>sayi;
add(p,sayi);

postorder(p);
}

system("pause");
}

Thanks ;)
Last edited on
Topic archived. No new replies allowed.