📄 inorder.cpp
字号:
#include<malloc.h>
#include <stdio.h>
#define m0 100 //定义字符串结构中的最大长度
#define m2 20
#define null 0
#define Len sizeof (BinNode)
enum tag{zero,one};
typedef struct BinNode // 结点结构
{
char data;
struct BinNode *lch,*rch;
enum tag ltag,rtag;
}BinNode,*Bintree;
struct chtp{ int len; char ch[m0]; }; //定义字符串结构
struct BinNode *bt; //定义2叉树的根节点指针
struct BinNode *stack[m2]; //定义堆栈,中序遍历二叉树的非递归算法要用
struct chtp s;
int top=0,ii=0; //两个全局变量
void crt_pre(Bintree *t)
{
char c;
c=s.ch[ii];
ii=ii+1;
if (c=='.') *t=null;
else
{
*t=(BinNode *)malloc(Len);
(*t)->data=c;
crt_pre(&(*t)->lch);
crt_pre(&(*t)->rch);
};
}
void inorder1( BinNode *t) /*中序遍历二叉树的非递归算法*/
{
BinNode *p;
top=0;stack[top]=t; //将根节点压入栈
while (top!=-1)
{
for(;stack[top]!=null;)
{
top=top+1;
stack[top]=stack[top-1]->lch; //依次将当前根节点的左孩子压入栈,直到最左孩子的左指针(为空)
};
top=top-1; //准备从栈中弹出当前根节点的最左孩子
if(top!=-1)
{
p=stack[top];
printf("%c\t",(*p).data);
stack[top]=(*p).rch;
};
}
printf("\n");
}
void main()
{
// char /*c[]={'a','b','d','.','.','.','c','e','.','.','f','.','.','!'};*/
char c[]={'a','b','c','.','.','d','e','.','g','.','.','f','.','.','.','!'};
int i=0;
for(i=0,s.len=0;c[i]!='!';i++,s.len++) s.ch[i]=c[i];
crt_pre(&bt);
inorder1(bt);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -