c prgram to display a binary tree but it is not showing any output, what is the error in this program can you help me
-
#include
#include
struct tree
{
char info;
struct tree *left;
struct tree *right;
};
struct tree *root;
struct tree *stree(struct tree *root,struct tree *r,char info);
void print_tree(struct tree *root,int l);
int main(void)
{
char s[80];
int l=3;
root=NULL;
do
{
printf("enter a letter:");
gets(s);
root=stree(root,root, *s);} while(\*s); print\_tree(root,0); return 0;
}
struct tree *stree(struct tree *root,struct tree *r,char info)
{
if(!r)
{
r=(struct tree *) malloc(sizeof(struct tree));
if(!r)
{
printf("out of memory \n");
exit(0);} r->left=NULL; r->right=NULL; r->info=info; if(!root) return r; if(infoinfo) root->left=r; else root->right=r; return r; } if(infoinfo) stree(r,r->left,info); else stree(r,r->right,info); return root;
}
void print_tree(struct tree *r,int l)
{
int i;
if(!r) return ;
print_tree(r->right,l+1);
for(i=0;iinfo);
print_tree(r->left,l+1);}
-
#include
#include
struct tree
{
char info;
struct tree *left;
struct tree *right;
};
struct tree *root;
struct tree *stree(struct tree *root,struct tree *r,char info);
void print_tree(struct tree *root,int l);
int main(void)
{
char s[80];
int l=3;
root=NULL;
do
{
printf("enter a letter:");
gets(s);
root=stree(root,root, *s);} while(\*s); print\_tree(root,0); return 0;
}
struct tree *stree(struct tree *root,struct tree *r,char info)
{
if(!r)
{
r=(struct tree *) malloc(sizeof(struct tree));
if(!r)
{
printf("out of memory \n");
exit(0);} r->left=NULL; r->right=NULL; r->info=info; if(!root) return r; if(infoinfo) root->left=r; else root->right=r; return r; } if(infoinfo) stree(r,r->left,info); else stree(r,r->right,info); return root;
}
void print_tree(struct tree *r,int l)
{
int i;
if(!r) return ;
print_tree(r->right,l+1);
for(i=0;iinfo);
print_tree(r->left,l+1);}
-
#include
#include
struct tree
{
char info;
struct tree *left;
struct tree *right;
};
struct tree *root;
struct tree *stree(struct tree *root,struct tree *r,char info);
void print_tree(struct tree *root,int l);
int main(void)
{
char s[80];
int l=3;
root=NULL;
do
{
printf("enter a letter:");
gets(s);
root=stree(root,root, *s);} while(\*s); print\_tree(root,0); return 0;
}
struct tree *stree(struct tree *root,struct tree *r,char info)
{
if(!r)
{
r=(struct tree *) malloc(sizeof(struct tree));
if(!r)
{
printf("out of memory \n");
exit(0);} r->left=NULL; r->right=NULL; r->info=info; if(!root) return r; if(infoinfo) root->left=r; else root->right=r; return r; } if(infoinfo) stree(r,r->left,info); else stree(r,r->right,info); return root;
}
void print_tree(struct tree *r,int l)
{
int i;
if(!r) return ;
print_tree(r->right,l+1);
for(i=0;iinfo);
print_tree(r->left,l+1);}
Ditto what Richard suggested. Printing aside, you need to first verify that the tree is being built correctly. The only way to do that is to single step through each line of code (the
stree()
function) using the debugger. Note the values ofinfo
,left
, andright
along the way. As you build the tree on paper, what you see in the debugger should match. Are you trying to print the tree contents pre-order, in-order, or post-order?"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
-
#include
#include
struct tree
{
char info;
struct tree *left;
struct tree *right;
};
struct tree *root;
struct tree *stree(struct tree *root,struct tree *r,char info);
void print_tree(struct tree *root,int l);
int main(void)
{
char s[80];
int l=3;
root=NULL;
do
{
printf("enter a letter:");
gets(s);
root=stree(root,root, *s);} while(\*s); print\_tree(root,0); return 0;
}
struct tree *stree(struct tree *root,struct tree *r,char info)
{
if(!r)
{
r=(struct tree *) malloc(sizeof(struct tree));
if(!r)
{
printf("out of memory \n");
exit(0);} r->left=NULL; r->right=NULL; r->info=info; if(!root) return r; if(infoinfo) root->left=r; else root->right=r; return r; } if(infoinfo) stree(r,r->left,info); else stree(r,r->right,info); return root;
}
void print_tree(struct tree *r,int l)
{
int i;
if(!r) return ;
print_tree(r->right,l+1);
for(i=0;iinfo);
print_tree(r->left,l+1);}
You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect. The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect. Debugger - Wikipedia, the free encyclopedia[^] Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Patrice “Everything should be made as simple as possible, but no simpler.” Albert Einstein