473,385 Members | 1,341 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 1851
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Will Oram | last post by:
Hi, My assignment is to create a non-binary tree of arbitrary form, and then print out the data in an orderly fashion. The handout contains a tree to be inputted: 2 / | \ 3 7 5 / \ |
11
by: jova | last post by:
Is there a difference between a Binary Tree and a Binary Search Tree? If so can someone please explain. Thank You.
4
by: Tarique Jawed | last post by:
Alright I needed some help regarding a removal of a binary search tree. Yes its for a class, and yes I have tried working on it on my own, so no patronizing please. I have most of the code working,...
4
by: Ken | last post by:
I have a binary tree in VB NET and insertions seem to be slow. The program receives data from one source and inserts it into the tree. The program receives data from another source and...
4
by: Today's Mulan | last post by:
I guess this is new, at least to people who don't know about this, like me, please be prepared to give me a satifying answer, so please tell me how the compiler "knows" variable "i" as in a...
27
by: Chad | last post by:
The problem is: Write a recursive version of the function reverse(s), which reverses the string s in place. In "The C Answer Book", Second Edition, near the bottom of page 95, the authors say...
90
by: John Salerno | last post by:
I'm a little confused. Why doesn't s evaluate to True in the first part, but it does in the second? Is the first statement something different? False print 'hi' hi Thanks.
1
by: hn.ft.pris | last post by:
I have the following code: Tree.h defines a simple binary search tree node structure ########## FILE Tree.h ################ #ifndef TREE_H #define TREE_H //using namespace std; template...
8
by: n.noumia | last post by:
- with 10 nodes, give one example of an unbalanced binary tree and one example of a balanced binary tree - what is the advantage of having a balanced binary tree over an unbalanced tree? - number...
11
by: Defected | last post by:
Hi, How i can create a Binary Search Tree with a class ? thanks
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.