当前位置:网站首页>6-3 二叉树的非递归遍历
6-3 二叉树的非递归遍历
2022-06-22 20:24:00 【白—】
6-3 二叉树的非递归遍历
本题要求用非递归的方法实现对给定二叉树的 3 种遍历。
函数接口定义:
void InorderTraversal( BinTree BT );
void PreorderTraversal( BinTree BT );
void PostorderTraversal( BinTree BT );
其中BinTree结构定义如下:
typedef struct TNode *Position;
typedef Position BinTree;
struct TNode{
ElementType Data;
BinTree Left;
BinTree Right;
int flag;
};
要求 3 个函数分别按照访问顺序打印出结点的内容,格式为一个空格跟着一个字符。
此外,裁判程序中给出了堆栈的全套操作,可以直接调用。
裁判测试程序样例:
#include <stdio.h>
#include <stdlib.h>
typedef enum { false, true } bool;
typedef char ElementType;
typedef struct TNode *Position;
typedef Position BinTree;
struct TNode{
ElementType Data;
BinTree Left;
BinTree Right;
int flag;
};
/------堆栈的定义-------/
typedef Position SElementType;
typedef struct SNode *PtrToSNode;
struct SNode {
SElementType Data;
PtrToSNode Next;
};
typedef PtrToSNode Stack;
/* 裁判实现,细节不表 /
Stack CreateStack();
bool IsEmpty( Stack S );
bool Push( Stack S, SElementType X );
SElementType Pop( Stack S ); / 删除并仅返回S的栈顶元素 /
SElementType Peek( Stack S );/ 仅返回S的栈顶元素 /
/----堆栈的定义结束-----*/
BinTree CreateBinTree(); /* 裁判实现,细节不表 */
void InorderTraversal( BinTree BT );
void PreorderTraversal( BinTree BT );
void PostorderTraversal( BinTree BT );
int main()
{
BinTree BT = CreateBinTree();
printf(“Inorder:”); InorderTraversal(BT); printf(“\n”);
printf(“Preorder:”); PreorderTraversal(BT); printf(“\n”);
printf(“Postorder:”); PostorderTraversal(BT); printf(“\n”);
return 0;
}
/* 你的代码将被嵌在这里 */
输入样例:
如图
输出样例:
Inorder: D B E F A G H C I
Preorder: A B D F E C G H I
Postorder: D E F B H G I C A
代码:
void InorderTraversal( BinTree BT ){
if(BT==NULL)
return ;
InorderTraversal(BT->Left);
printf(" %c",BT->Data);
InorderTraversal(BT->Right);
}
void PreorderTraversal( BinTree BT ){
if(BT==NULL)
return ;
printf(" %c",BT->Data);
PreorderTraversal(BT->Left);
PreorderTraversal(BT->Right);
}
void PostorderTraversal( BinTree BT ){
if(BT==NULL)
return ;
PostorderTraversal(BT->Left);
PostorderTraversal(BT->Right);
printf(" %c",BT->Data);
}
202206201635一
边栏推荐
- The machine that lies in the 52nd monthly race of Niuke (the complexity of interval assignment operation from O (n^2) to o (n))
- 解决phpstudy中mysql无法启动,与本地安装的mysql冲突
- 74- how to remedy the loss of Oracle to MySQL for this kind of SQL optimization?
- IDC releases China Data Governance Report Yixin Huachen No. 1
- 杰理之列免晶振一拖八烧录升级【篇】
- Fegin的解析
- 300. 最长递增子序列 ●●
- PlainSelect. getGroupBy()Lnet/sf/jsqlparser/statement/select/GroupByElement;
- Quick sort template & considerations
- Summary of differences between localstorage, sessionstorage and cookies
猜你喜欢

杰理之MUSIC 模式获取播放文件的目录【篇】

RealNetworks vs. 微软:早期流媒体行业之争
![List of outstanding talents: no crystal vibration, one drag, eight burn and upgrade [chapter]](/img/6c/333bc95fe390234d3d06043e4bded1.png)
List of outstanding talents: no crystal vibration, one drag, eight burn and upgrade [chapter]
![[redis] cluster and common errors](/img/a5/94906b62b1ec0d549f9b72ff3db7f2.png)
[redis] cluster and common errors

ByteDance proposes a lightweight and efficient new network mocovit, which has better performance than GhostNet and mobilenetv3 in CV tasks such as classification and detection
![[records of different objects required by QIPA]](/img/f7/c0f0f56e4f1bf4f1a0a61552afcd2b.png)
[records of different objects required by QIPA]

LeetCode 每日一题——513. 找树左下角的值

杰理之动态切换 EQ 文件【篇】

为了不曾忘却的纪念:孙剑专题

引入稀疏激活机制!Uni-Perceiver-MoE显著提升通才模型的性能
随机推荐
第029讲:文件:一个任务 | 课后测试题及答案
大势智慧创建倾斜模型和切割单体化
300. 最长递增子序列 ●●
引入稀疏激活机制!Uni-Perceiver-MoE显著提升通才模型的性能
(DUC/DDC)数字上混频/正交下混频原理及matlab仿真
72 results and development suggestions of the latest on-site production system optimization
Leetcode daily question - 513 Find the value in the lower left corner of the tree
The problem that Jericho can't play the prompt tone in the music mode using the interrupt mode [chapter]
2022 a special equipment related management (elevator) examination questions and simulation examination
Jerry's problem of opening the near end of four channel call [chapter]
第031讲:永久存储:腌制一缸美味的泡菜 | 课后测试题及答案
微软 Edge 浏览器将支持网络测速,内置计算器和单位转换工具
万字长文 | 使用 RBAC 限制对 Kubernetes 资源的访问
《跟唐老师学习云网络》 - OpenStack网络实现
[records of different objects required by QIPA]
Lesson 031: permanent storage: pickle a jar of delicious pickles | after class test questions and answers
Redis core technology and practice: learning summary directory
The machine that lies in the 52nd monthly race of Niuke (the complexity of interval assignment operation from O (n^2) to o (n))
Redis核心技术与实战:学习总结目录
杰理之列免晶振一拖八烧录升级【篇】