Click here to Skip to main content
16,013,548 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
I did a program in c to display a binary tree but it is not showing ouput,can u please help me out what is the problem with this program

What I have tried:

C
#include<stdlib.h>
#include<stdio.h>
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(info<root->info)
			root->left=r;
		else
			root->right=r;
		return r;

	}
	if(info<r->info)
		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;i<l;++i)>
		printf(" ");
	printf("%c \n",r->info);
	print_tree(r->left,l+1);


}
Posted
Updated 30-Jul-18 6:48am
v3

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[^]
 
Share this answer
 
You already posted this in the C/C++ forum. Please do not crosspost.
 
Share this answer
 
#include<stdio.h>
main()
{int a,b;
a='*';
printf("enter the number");
scanf("%d",b);
while(a=b,a<b,a++)
printf("%d",a);
}
 
Share this answer
 
Comments
jeron1 30-Jul-18 13:07pm    
Why post this here?
Patrice T 30-Jul-18 13:20pm    
Junk !

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900