C程序显示二叉树 [英] C program to display a binary tree

查看:150
本文介绍了C程序显示二叉树的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在c中做了一个显示二叉树的程序,但它没有显示输出,请你帮我解决这个程序有什么问题



我尝试了什么:



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:

#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);


}

推荐答案

您应该尽快学会使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。

调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ]
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[^]


您已在C / C ++论坛中发布此内容。请不要交叉。
You already posted this in the C/C++ forum. Please do not crosspost.


#include< stdio.h>

main()

{int a,b;

a ='*';

printf(输入数字);

scanf(%d,b);

while(a = b,a< b,a ++)

printf(%d,a);

}
#include<stdio.h>
main()
{int a,b;
a='*';
printf("enter the number");
scanf("%d",b);
while(a=b,a<b,a++)
printf("%d",a);
}


这篇关于C程序显示二叉树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆