【7.4】
1 #include2 #include 3 #include 4 using namespace std; 5 #define MAXN 100 6 7 typedef struct node{ 8 char data; 9 node *lchild;10 node *rchild;11 } BTNode,*LinkBTNode;12 LinkBTNode BuildBTree(char a[],int d) //构造二叉树13 {14 if(a[d]==0)15 return NULL;16 LinkBTNode bt = (LinkBTNode)malloc(sizeof(BTNode));17 bt->data = a[d]; 18 bt->lchild = BuildBTree(a,d<<1);19 bt->rchild = BuildBTree(a,d<<1|1);20 return bt;21 }22 void DispBTNode(LinkBTNode b) //以括号表示法输出二叉树23 {24 if(b!=NULL){25 cout< data;26 if(b->lchild!=NULL||b->rchild!=NULL){27 cout<<"(";28 DispBTNode(b->lchild);29 if(b->rchild!=NULL)30 cout<<',';31 DispBTNode(b->rchild);32 cout<<')';33 }34 }35 }36 int main()37 {38 int i,n;39 while(cin>>n){40 if(n==0) break;41 getchar();42 char a[MAXN*3+1] = { 0};43 for(i=1;i<=n;i++) //依次输入完全二叉树的节点44 cin>>a[i];45 LinkBTNode bt = BuildBTree(a,1); //用二叉链创建二叉树46 DispBTNode(bt);47 cout<
Freecode :