creatree.c
来自「《数据结构》教材源程序,可以让你轻松的根据教材学习数据结构」· C语言 代码 · 共 41 行
C
41 行
#include <stdio.h>
#define m 3
typedef char datatype;
typedef struct node {
datatype data;
struct node *child[m];
} node,*tree;
void createtree(tree *p)
{/*按前序遍历顺序建立一棵3度树的递归算法*/
int i; char ch;
if ((ch=getchar())==' ') *p=NULL;
else
{
*p=(tree) malloc (sizeof(node));
(*p)->data=ch;
for (i=0;i<m;++i)
createtree(&(*p)->child[i]);
}
}
void postorder(tree p)
{/*树的后序遍历*/
int i;
if (p!=NULL)
{
for (i=0;i<m;++i)
postorder(p->child[i]);
printf("%c",p->data);
}
}
main()
{
tree t;
printf("please input the preorder sequence of the tree:\n");
createtree(&t);
printf("\nthe postorderis:");
postorder(t);
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?