dreamscöpçe
08/01/2008, 16:45
merhaba arkadaşlar yazdığım programda derlediğimde herhangi bir hata yok fakat turbu c de adım adım çalıştırdığımda şunu gördüm: insert fonksiyonuna girdiğinde ilk eklenecek eleman için ilk if deyiminin çalışması doğru fakat daha sonraki eklemelerde onun çalışmaması gerekirken tekrar onu çalıştırıyo ve hiç bir zaman ikinci if deyimine geçmiyor burada 2. if deyimi dediğim else ten sonrası yani ağacın boştan farklı olma durumu.bu da rootptr hiç bir zaman nulldan farklı olmuyor anlamına geliyor. ve hatayı nasıl düzeltmem gerektiğini bulamadım. yardım ederseniz çok sevinirim:) başta yaptığım tanımlamaları da veriyorum..
typedef struct binarytree{int data;
struct binarytree *left;
struct binarytree *right;
}tree;
void insertnode(tree *rootptr,int value);
void inorder(tree *rootptr);
void main()
{
char cev;
tree *root;
root=NULL;
do
{ printf("tamsayi bir deger giriniz");
scanf("%d",&num);
insertnode(root,num);
printf("any data?? y for yes, n for no\n");
cev=getch();
}while(char(cev)!='n');
printf("\n\n the inorder traversal is:\n");
inorder(root);
}/*end main*/
void insertnode(tree *rootptr,int value)
{
/*if tree is empty*/
if(rootptr==NULL)
{
rootptr=(tree*)malloc(sizeof(tree));
/*if memory was allocated then assign data*/
if(rootptr!=NULL)
{
rootptr->data=value;
rootptr->left=NULL;
rootptr->right=NULL;
rootptr=(tree*)malloc(sizeof(tree));
}/*endif*/
else /*if rootptr==NULL*/
printf("%d not inserted.No memory available.\n",value);
return;
}/*endif*/
else
{
/*tree is not empty*/
/*data to insert is less than data in current node*/
if (value<rootptr->data)
{
insertnode(rootptr->left,value);
}/*endif*/
/*data to insert greater than data in current node*/
else if (value>rootptr->data)
{
insertnode(rootptr->right,value);
}/*end elseif*/
else
{
/*duplicate data value ignored*/
printf("dup");
}/*end else*/
}/*end else*/
}/*end function insertnode*/
void inorder(tree *rootptr)
{
/*if(!rootptr) return;*/
/*if tree is not empty then traverse*/
if(rootptr!=NULL)
{ inorder(rootptr->left);
printf("%3d",rootptr->data);
inorder(rootptr->right);
}/*endif*/
}/*end function inorder*/
typedef struct binarytree{int data;
struct binarytree *left;
struct binarytree *right;
}tree;
void insertnode(tree *rootptr,int value);
void inorder(tree *rootptr);
void main()
{
char cev;
tree *root;
root=NULL;
do
{ printf("tamsayi bir deger giriniz");
scanf("%d",&num);
insertnode(root,num);
printf("any data?? y for yes, n for no\n");
cev=getch();
}while(char(cev)!='n');
printf("\n\n the inorder traversal is:\n");
inorder(root);
}/*end main*/
void insertnode(tree *rootptr,int value)
{
/*if tree is empty*/
if(rootptr==NULL)
{
rootptr=(tree*)malloc(sizeof(tree));
/*if memory was allocated then assign data*/
if(rootptr!=NULL)
{
rootptr->data=value;
rootptr->left=NULL;
rootptr->right=NULL;
rootptr=(tree*)malloc(sizeof(tree));
}/*endif*/
else /*if rootptr==NULL*/
printf("%d not inserted.No memory available.\n",value);
return;
}/*endif*/
else
{
/*tree is not empty*/
/*data to insert is less than data in current node*/
if (value<rootptr->data)
{
insertnode(rootptr->left,value);
}/*endif*/
/*data to insert greater than data in current node*/
else if (value>rootptr->data)
{
insertnode(rootptr->right,value);
}/*end elseif*/
else
{
/*duplicate data value ignored*/
printf("dup");
}/*end else*/
}/*end else*/
}/*end function insertnode*/
void inorder(tree *rootptr)
{
/*if(!rootptr) return;*/
/*if tree is not empty then traverse*/
if(rootptr!=NULL)
{ inorder(rootptr->left);
printf("%3d",rootptr->data);
inorder(rootptr->right);
}/*endif*/
}/*end function inorder*/