二叉树遍历代码
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
printf("请按先序次序输入二叉树各节点的值,以.表示空树,以#号结束:\n");
createBiTreeByPreOrder(T);
printf("先序遍历二叉树:\n");
preOrderPrint(T);
printf("\n");
printf("中序遍历二叉树:\n");
inOrderPrint(T);
void LayerTraversePrint(BiTree T){
SqQueue Q;
initQueue(Q);
if(T){
Q.data[Q.r] = T;//根节点入队
Q.r++;
}
while(Q.f != Q.r){
if(Q.data[Q.f]->lchild){
Q.data[Q.r] = Q.data[Q.f]->lchild;
printf("\n");
printf("后序遍历二叉树:\n");
postOrderPrint(T);
printf("\n");
printf("按层次遍历二叉树:\n");
LayerTraversePrint(T);
return 0;
}
printf("%c ", T->data);
}
}
typedef BiTNode* QElemType;
typedef struct{
QElemType data[20];
int f;
int r;
}SqQueue;
void initQueue(SqQueue &Q){
Q.f = Q.r = 0;
}
//层次遍历
char ch;
scanf("%c", &ch);
if(ch != '#'){
if(ch == '.'){
T = NULL;
}else{
T = (BiTNode *)malloc(sizeof(BiTNode));
T->data = ch;
createBiTreeByPreOrder(T->lchild);
if(T){
inOrderPrint(T->lchild);
printf("%c ", T->data);
inOrderPrint(T->rchild);
}
}
void postOrderPrint(BiTree T){
if(T){
postOrderPrint(T->lchild);
postOrderPrint(T->rchild);
createBiTreeByPreOrder(T->rchild);
}
}
}
void preOrderPrint(BiTree T){
if(T){
printf("%c ", T->data);
preOrderPrint(T->lchild);
preOrderPrint(T->rchild);
}
}
void inOrderPrint(BiTree T){
#include<stdio.h>
#include<stdlib.h>
typedef char TElemType;
typedef struct BiTNode{
TElemType data;
struct BiTNode *lchild, *rchild;
}BiTNode, பைடு நூலகம்BiTree;
void createBiTreeByPreOrder(BiTree &T){
Q.r++;
}
if(Q.data[Q.f]->rchild){
Q.data[Q.r] = Q.data[Q.f]->rchild;
Q.r++;
}
printf("%c ", Q.data[Q.f]->data);
Q.f++;//队头元素出队
}
printf("\n");
}
int main(){
BiTree T;
createBiTreeByPreOrder(T);
printf("先序遍历二叉树:\n");
preOrderPrint(T);
printf("\n");
printf("中序遍历二叉树:\n");
inOrderPrint(T);
void LayerTraversePrint(BiTree T){
SqQueue Q;
initQueue(Q);
if(T){
Q.data[Q.r] = T;//根节点入队
Q.r++;
}
while(Q.f != Q.r){
if(Q.data[Q.f]->lchild){
Q.data[Q.r] = Q.data[Q.f]->lchild;
printf("\n");
printf("后序遍历二叉树:\n");
postOrderPrint(T);
printf("\n");
printf("按层次遍历二叉树:\n");
LayerTraversePrint(T);
return 0;
}
printf("%c ", T->data);
}
}
typedef BiTNode* QElemType;
typedef struct{
QElemType data[20];
int f;
int r;
}SqQueue;
void initQueue(SqQueue &Q){
Q.f = Q.r = 0;
}
//层次遍历
char ch;
scanf("%c", &ch);
if(ch != '#'){
if(ch == '.'){
T = NULL;
}else{
T = (BiTNode *)malloc(sizeof(BiTNode));
T->data = ch;
createBiTreeByPreOrder(T->lchild);
if(T){
inOrderPrint(T->lchild);
printf("%c ", T->data);
inOrderPrint(T->rchild);
}
}
void postOrderPrint(BiTree T){
if(T){
postOrderPrint(T->lchild);
postOrderPrint(T->rchild);
createBiTreeByPreOrder(T->rchild);
}
}
}
void preOrderPrint(BiTree T){
if(T){
printf("%c ", T->data);
preOrderPrint(T->lchild);
preOrderPrint(T->rchild);
}
}
void inOrderPrint(BiTree T){
#include<stdio.h>
#include<stdlib.h>
typedef char TElemType;
typedef struct BiTNode{
TElemType data;
struct BiTNode *lchild, *rchild;
}BiTNode, பைடு நூலகம்BiTree;
void createBiTreeByPreOrder(BiTree &T){
Q.r++;
}
if(Q.data[Q.f]->rchild){
Q.data[Q.r] = Q.data[Q.f]->rchild;
Q.r++;
}
printf("%c ", Q.data[Q.f]->data);
Q.f++;//队头元素出队
}
printf("\n");
}
int main(){
BiTree T;