树的后序遍历方式源码
来源:互联网 发布:windows longhorn开机 编辑:程序博客网 时间:2024/06/06 18:52
#include <stdio.h>
#include <stdlib.h>
typedef struct node{
char data;
struct node *llink;
struct node *rlink;
}NODE;
NODE*build()//
{
NODE *tree;
char c;
scanf("%c",&c);
if(c=='#') tree = NULL;
else
{
tree=(NODE*)malloc(sizeof(NODE));
tree->data=c;
tree->llink=build();
tree->rlink=build();
}
return tree;
}
void houxu(NODE*tree)
{
NODE*pt=tree;
int tt[100],top=-1;
NODE*s[100];
do
{
while(pt!=NULL)
{
s[++top]=pt;
tt[top]=0;
pt=pt->llink;
}
if(top>=0)
{
if(!tt[top])
{
pt=s[top];
tt[top]=1;
pt=pt->rlink;
}
else printf("%c",s[top--]->data);
}
}while((pt!=NULL)||(top!=-1));
}
int main()
{
printf("请输入树的结点:\n");
NODE*tree=(NODE*)malloc(sizeof(NODE));
tree=build();
printf("后序遍历的结果是:\n");
houxu(tree);
printf("\n");
return 0;
}
/*测试数据
abd##e##cf###
输出:debfca
*/
#include <stdlib.h>
typedef struct node{
char data;
struct node *llink;
struct node *rlink;
}NODE;
NODE*build()//
{
NODE *tree;
char c;
scanf("%c",&c);
if(c=='#') tree = NULL;
else
{
tree=(NODE*)malloc(sizeof(NODE));
tree->data=c;
tree->llink=build();
tree->rlink=build();
}
return tree;
}
void houxu(NODE*tree)
{
NODE*pt=tree;
int tt[100],top=-1;
NODE*s[100];
do
{
while(pt!=NULL)
{
s[++top]=pt;
tt[top]=0;
pt=pt->llink;
}
if(top>=0)
{
if(!tt[top])
{
pt=s[top];
tt[top]=1;
pt=pt->rlink;
}
else printf("%c",s[top--]->data);
}
}while((pt!=NULL)||(top!=-1));
}
int main()
{
printf("请输入树的结点:\n");
NODE*tree=(NODE*)malloc(sizeof(NODE));
tree=build();
printf("后序遍历的结果是:\n");
houxu(tree);
printf("\n");
return 0;
}
/*测试数据
abd##e##cf###
输出:debfca
*/
阅读全文
1 0
- 树的后序遍历方式源码
- 非递归的方式后序遍历二叉树
- 树遍历方式总结:层次遍历、先序遍历、中序遍历、后序遍历
- 二叉树的先序遍历、中序遍历以及后序遍历(递归以及非递归方式)
- 树的后序遍历
- 树的后序遍历
- 数据结构与算法分析之二叉树的三种遍历方式。--前序遍历,中序遍历和后序遍历
- 二叉树的四种遍历方式 (前序,中序,后序,层序遍历 python实现)
- 二叉树的遍历 前序 中序 后序 分别实现递归和非递归遍历方式
- 树的先序、中序和后序遍历方式
- 有关二叉树的三种遍历方式------先序,中序,后序
- 树的先序、中序和后序遍历方式
- 二叉树的非递归遍历方式(前序、中序和后序)
- 二叉树的三种遍历方式,前序、中序和后序。
- 树的先序、中序和后序遍历方式
- 看图说话之二叉树的前序,中序,后序,层次遍历方式
- 线索化中序、先序、后序遍历二叉树的方式(待补充完整)
- 剑指offer系列源码-二叉搜索树的后序遍历序列
- 启动minicom时出现错误"Device /dev/ttyS0 is locked minicom"的解决办法
- 前端学习(六)回调,异步,非阻塞,事件驱动等知识
- vuex学习之路之出现的错误一
- 深入理解JVM(六)——类文件结构——code
- [模板]二维凸包(纯代码)
- 树的后序遍历方式源码
- (9)Javasript入门二
- 123
- php常见面试题
- sublime个人配置
- 方法的重载
- Android InputMethodManager 导致的内存泄露及解决方案
- PHP与memcache数据交互
- 1 ubuntu server 14.04 LTS环境配置