二叉树------从文件中读取进行层次遍历(顺序表)

来源:互联网 发布:linux下部署jira 编辑:程序博客网 时间:2024/06/07 20:12
#include<stdio.h>#include <conio.h>#include<malloc.h>char Value[100];int count = 0;typedef struct _BiNode {struct _BiNode *L;char Data;struct _BiNode *R;}BiNode;typedef struct _Que{BiNode * Base;int Front;int Rear;int Len;}Que; void InitQue(Que *que,int count);void EnQue(Que *que,BiNode *Node);BiNode OutQue(Que *que);int QuEmpty(Que *que);void GetValue(FILE *fp);void InitBiTree(BiNode **Hold);void CreateBiTree(BiNode **Head);int main(){BiNode *  Head,MyNode;FILE *   fp_r;Que MyQu;InitQue(&MyQu,100);InitBiTree(&Head);fp_r = fopen("d:\\1.txt","r");GetValue(fp_r);fclose(fp_r);CreateBiTree(&Head);EnQue(&MyQu,Head);while(!QuEmpty(&MyQu)){MyNode = OutQue(&MyQu);printf("%c",MyNode.Data);if(MyNode.L != NULL)EnQue(&MyQu,MyNode.L);if(MyNode.R !=NULL)EnQue(&MyQu,MyNode.R);}getch();return 0;}void InitBiTree(BiNode **Hold){*Hold = NULL;}void InitQue(Que *que,int count){que->Base = (BiNode *)malloc(sizeof(BiNode)*count);que->Front=que->Rear =0;que->Len =0;}void GetValue(FILE *fp){char c;while(!feof(fp)){c =fgetc(fp);Value[count++]=c;}count = 0;}void CreateBiTree(BiNode **Hold){char c;BiNode *p;c = Value[count++];if(c ==' '){*Hold = NULL;return ;}p=(BiNode *)malloc(sizeof(BiNode));p->Data= c;*Hold = p;CreateBiTree(&(p->L));CreateBiTree(&(p->R));}void EnQue(Que *que,BiNode  *Node){que->Base[que->Rear].Data=Node->Data;que->Base[que->Rear].R=Node->R;que->Base[que->Rear].L=Node->L;que->Len++;que->Rear++;}BiNode OutQue(Que *que){BiNode n;n = que->Base[que->Front];que->Front++;que->Len--;return n;}int QuEmpty(Que *que){if(que->Len == 0)return 1;else return 0;}

0 0