题目:一棵完全二叉树以顺序方式存储,设计一个递归算法,对该完全二叉树进行中序遍历。
test.h
#include#include #include #define MAXSIZE 100typedef struct FullBiTree{ char node[MAXSIZE]; int n;}FullBiTree;FullBiTree *Init_Tree(){ FullBiTree *T; T=(FullBiTree *)malloc(sizeof(FullBiTree)); if(T!=NULL) { T->n=0; } return T;}void Set_Tree(FullBiTree *T,char *ch,int i){ char c; int j; if(T->n>=MAXSIZE-1) { return; } for(j =0;j n++; T->node[T->n] = c; } return;}void seqInOrder(FullBiTree *T,int i){ if(i==0) //递归调用的结束条件 return; else { if(2*i<=T->n) seqInOrder(T,2*i);//中序遍历 i 的左子树 else seqInOrder(T,0); printf("%2c",T->node[i]);//输出根结点 if(2*i+1<=T->n) seqInOrder(T,2*i+1);//中序遍历 i 的右子树 else seqInOrder(T,0); }}
test.c
#include"test.h"int main(){ int num; char *p; FullBiTree *T; T = Init_Tree(); p = (char *)malloc(sizeof(char)); printf("请输入每个结点:\n"); gets(p); num = strlen(p); printf("结点:%d\n",num); Set_Tree(T,p,num); printf("中序遍历:\n"); seqInOrder(T,1); printf("\n"); return 0;}