About the binary tree

Y

Yuri CHUANG

Hi,
Here is my code,
#include<stdio.h>
#include<stdlib.h>


typedef struct BiTNode
{
char data;
struct BiTNode *lchild,*rchild;
}BiTNode;

void CreateBiTree(BiTNode*);
void PreOrder(BiTNode*);

int main(void)
{
BiTNode *p;
p=NULL;
CreateBiTree(p);
printf("%d\n",p);
PreOrder(p);


system("PAUSE");
return 0;
}

void CreateBiTree(BiTNode* t)
{
char ch;
ch=getche();
printf("\n");
if(ch==' ')t=NULL;
else
{
t=(BiTNode*)malloc(sizeof(BiTNode));
printf("%d\n",t);
if(t==NULL)exit(-1);
t->data=ch;
puts("left child");
CreateBiTree(t->lchild);
puts("right child");
CreateBiTree(t->rchild);
}
}

void PreOrder(BiTNode* t)
{
if(t!=NULL)
{
printf("%c ",t->data);
PreOrder(t->lchild);
PreOrder(t->rchild);
}
}

I create the tree recursively,but in main function,the pointer is
NULL.Could anyone help me to build a binary tree with recursion?Thank
you!
 
W

Walter Roberson

Yuri CHUANG said:
Here is my code,
int main(void)
{
BiTNode *p;

Here you create a variable with name p that is local to main().

Here you set that variable to an initial value.
CreateBiTree(p);

Here you pass the *value* of the variable to CreateBiTree()
printf("%d\n",p);

And here you expect the *value* of the variable to have changed.
PreOrder(p);
system("PAUSE");
return 0;
}
void CreateBiTree(BiTNode* t)
{
char ch;
ch=getche();

Here you call upon a windows-specific function getche() without
a particularily good reason to do so, and without having declared
getche() in any header.

You also assign the result into a char variable directly, before
testing it for EOF, even though EOF is not certain to be
representable as a char .
printf("\n");
if(ch==' ')t=NULL;
else
{
t=(BiTNode*)malloc(sizeof(BiTNode));

Here you assign a value to the variable t, which is a *copy* of
the parameter value that you passed in from the previous level.
In particular, when you called CreateBiTree() from main, a -copy-
of a NULL pointer was put into t, and you then overwrite that copy.
But overwriting the copy does not change the parameter value in
the calling routine, so the main()'s variable named p is not going
to be changed by what you do to t in CreateBiTree().

printf("%d\n",t);

Here you attempt to print out a pointer as if were an int. That will
work on *some* systems, but it will not work on most systems, as
pointers are often bigger than int. It is quite common, for example,
for a pointer to be the size of a long instead of an int. (You
should also consider the possibility that the pointer converted to
an int is going to turn out negative.)
I create the tree recursively,but in main function,the pointer is
NULL.Could anyone help me to build a binary tree with recursion?

Either return the new pointer values from the routines, or else
pass in a pointer *to* the pointer: e.g., CreateBiTree(&p)
and then void CreateBiTree(BiTNode** t) { *t = malloc(....) }
 
Y

Yuri CHUANG

You give me the correct answer,thank you very much!
But,as I've learned before,I pass the pointer not a variable as the
parameter to the CreateBiTree function,it should be changed when return
to the main.
 
C

CBFalconer

Yuri said:
You give me the correct answer,thank you very much!
But,as I've learned before,I pass the pointer not a variable as
the parameter to the CreateBiTree function,it should be changed
when return to the main.

This is incomprehensible. In general on usenet you should realize
that readers may very well not have convenient access to previous
articles in a thread. That means that your reply articles should
include adequate context, so that they stand by themselves. Google
is NOT usenet, it is only a very poor interface to the real usenet
system. To include proper context when using google, see my sig.
below. Please be sure to read the referenced URLs.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,067
Latest member
HunterTere

Latest Threads

Top