二叉树的建立 及前中后序遍历

#include <stdio.h>
#include <malloc.h>
typedef struct bir{
	char data;
	struct bir *lchild,*rchild;
}birnode,*bitree;
int cry(bitree *t)//二叉树的建立 
{
	char ch;
	scanf("%c",&ch);
	if(ch=='#'){
		*t=NULL;
		return 0;
	}
	*t=(bitree )malloc(sizeof(birnode));
	(*t)->data=ch;
	cry(&((*t)->lchild));
	cry(&((*t)->rchild));
}
int print(birnode *t)//二叉树的前序遍历 
{
	if(t==NULL) return 0;
	printf("%c",t->data);
	
	print(t->lchild);
	print(t->rchild);
}
int print1(birnode *t)//二叉树的中序遍历
{
	if(t==NULL) return 0;
	
	print1(t->lchild);
	
	printf("%c",t->data);
	
	print1(t->rchild);
}
int print2(birnode *t)//二叉树的后序遍历
{
	if(t==NULL) return 0;
	print2(t->lchild);	
	print2(t->rchild);
	
	printf("%c",t->data);
}
int main()
{
	birnode *tree;
	cry(&tree);
	
	print(tree);
	print1(tree);
	print2(tree);
	
	
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_42868863/article/details/86559094