#include <stdio.h>
#include <iostream>
using namespace std;
typedef char TElemType;
typedef void Status;
typedef int ElemType;
typedef struct BiTNode {
TElemType data;
BiTNode* lchild, * rchild;
}BiTNode, * BiTree;
void CreateBiTree(BiTree& T) {
char ch;
scanf("%c", &ch);
if (ch == '#') {
T = NULL;
}else {
T = new BiTNode;
T->data = ch;
CreateBiTree(T->lchild);
CreateBiTree(T->rchild);
}
}
//中序遍历二叉树
Status InOrderTraverse(BiTree T) {
// 二叉树非空
if (T){
InOrderTraverse(T->lchild);
cout << T->data;
InOrderTraverse(T->rchild);
}
}
//层序遍历二叉树
Status FloorPrint(BiTree T){
//创建pTreeNode指针类型的指针数组
BiTree temp[100];
int in = 0;
int out = 0;
// 先保存二叉树根节点
temp[in++] = T;
while (in > out){
if (temp[out]){
cout << temp[out]->data;
temp[in++] = temp[out]->lchild;
temp[in++] = temp[out]->rchild;
}
out++;
}
}
int main() {
BiTree T;
cout << "先序遍历输入(以##结束,需要两个#):";
CreateBiTree(T);
cout << "中序遍历输出:";
InOrderTraverse(T);
cout << endl;
cout << "层序遍历输出:";
FloorPrint(T);
}
标签:遍历,temp,int,创建,ch,二叉树,BiTree,out
From: https://www.cnblogs.com/mikizero/p/17876550.html