8. 二叉树的中序、前序、后序的递归、非递归遍历算法,层次序的非递归遍历算法的实现,应包含建树的实现。

来源:百度知道 编辑:UC知道 时间:2024/06/16 00:26:25
二叉树的中序、前序、后序的递归、非递归遍历算法,层次序的非递归遍历算法的实现,应包含建树的实现。
要求:遍历的内容应是千姿百态的。

#include<stdlib.h>
#include<math.h>
#define NULL 0
#define maxsize 100
#define LEN sizeof(struct student)
typedef struct student
{struct student *lchild,*rchild;
char data;
}*STU;
STU stree_creat(char *s,int k)
{STU p;
if(s[k]=='\0'||k>maxsize)
return NULL;
else
{p=(STU)malloc(LEN);
p->data=s[k];
p->lchild=stree_creat(s,2*k+1);
p->rchild=stree_creat(s,2*k+2);
return p;
}
}
void print(STU p)
{ STU h[maxsize]={NULL};
int top=0,base=0;
h[top]=p;
printf("The chengci order:\n\n");
while(h[base]!=NULL)
{
printf("[%c]",h[base]->data);
h[++top]=h[base]->lchild;
h[++top]=h[base]->rchild;
base++;
}
printf("\nSuccess......\