哪里有问题啊,谁帮看看,已解决!
#include<stdio.h>#include<string.h>
#include<malloc.h>
#define LEN sizeof(bitree)
#define MAXSIZE 256
typedef struct node
{
char ch;
struct node *left,*right;
}*bitree;
//建树
bitree createbitree()
{
bitree t;
char c;
c=getchar();
if(c=='#') t=NULL;
else
{
t=(bitree)malloc(LEN);
if(t)
{
t->ch=c;
t->left=createbitree(t->left);
t->right=createbitree(t->right);
}
}
return t;
}
//递归先序遍历
void preordertravse(bitree t)
{
if(t!=NULL)
{
putchar(t->ch);
preordertravse(t->left);
preordertravse(t->right);
}
}
//非递归先序遍历
void preorder_n(bitree t)
{
bitree stack[MAXSIZE],p;
int top=0;
stack[top++]=t;
while(top>0)
{
p=stack[--top];
if(p!=NULL)
{
putchar(p->ch);
if(p->right) stack[top++]=p->right;
if(p->left) stack[top++]=p->left;
}
}
}
//递归中序遍历
void midordertravse(bitree t)
{
if(t!=NULL)
{
preordertravse(t->left);
putchar(t->ch);
preordertravse(t->right);
}
}
//递归后序遍历
void postordertravse(bitree t)
{
if(t!=NULL)
{
postordertravse(t->left);
postordertravse(t->right);
putchar(t->ch);
}
}
//深度
int deepth(bitree t)
{
int ld,rd;
if(t==NULL) return 0;
else
{
ld=deepth(t->left);
rd=deepth(t->right);
if(ld>rd) return ld+1;
else return rd+1;
}
}
//主函数****
int main()
{
bitree tree_start;
printf("建树: ");
tree_start=createbitree();
printf("\n递归先序遍历 : ");
preordertravse(tree_start);
printf("\n\n非递归先序遍历:");
preorder_n(tree_start);
printf("\n\n递归中序遍历 : ");
midordertravse(tree_start);
printf("\n\n递归后序遍历 : ");
postordertravse(tree_start);
printf("\n\n树的深度为:%d\n\n",deepth(tree_start));
return 0;
}
[ 本帖最后由 xdh0817 于 2012-2-11 21:41 编辑 ]