实验7 二叉树
1、编写算法函数void preorder1(bintree t)实现二叉树t的非递归前序遍历。
#include "bintree.h"
char *a="ABC##D#E##F##";
void preorder1(bintree t)
{
seqstack s;
init(&s);
while ( !empty(&s) || t )
{
if (t)
{
printf("%c",t->data);
push(&s, t);
t=t->lchild;
}
else
{
t=pop(&s);
t=t->rchild;
}
}
}
int main()
{
bintree t;
t=creatbintree();
printf("二叉树的前序序列为:\n");
preorder1(t);
return 0;
}
2、编写算法函数void levelbintree(bintree t),实现二叉树的层次遍历。
#include "bintree.h"
char *a="ABC##D#E##F##";
void levelbintree(bintree t)
{
bintree queue[100];
int f,r;
f=0;r=1;
if (t)
{
queue[0]=t;
while (f<r)
{
t=queue[f++];
printf("%c",t-><