470,849 Members | 1,358 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,849 developers. It's quick & easy.

About the binary tree

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!

May 22 '06 #1
3 1706
In article <11**********************@38g2000cwa.googlegroups. com>,
Yuri CHUANG <yu*******@126.com> wrote:
Here is my code, int main(void)
{
BiTNode *p;
Here you create a variable with name p that is local to main(). p=NULL;
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(....) }
--
"No one has the right to destroy another person's belief by
demanding empirical evidence." -- Ann Landers
May 22 '06 #2
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.

May 23 '06 #3
Yuri CHUANG wrote:

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/>
May 23 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Will Oram | last post: by
11 posts views Thread by jova | last post: by
4 posts views Thread by Tarique Jawed | last post: by
4 posts views Thread by Today's Mulan | last post: by
90 posts views Thread by John Salerno | last post: by
8 posts views Thread by n.noumia | last post: by
11 posts views Thread by Defected | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.