思路是,是用队列构造FBI树,然后遍历
求解!
#include <stdio.h>
#include "stdlib.h"
#define Queue_MakeEmpty(arg) ((arg).size = 0, (arg).Rear = 0, (arg).Front = 1)
#define Queue_IsEmpty(arg) ((arg).size == 0)
#define Queue_Size(arg) ((arg).size)
#define Queue_EnQueue(arg, value) ((arg).size += 1, (arg).Arr[(arg).Rear = ((arg).Rear +1 >= 1024 ? 0 : (arg).Rear +1)] = value)
#define Queue_DeQueue(arg) ((arg).size -= 1, (arg).Front = ((arg).Front +1 >= 1024 ? 0 : (arg).Front +1), (arg).Arr[(arg).Front == 0 ? 1023 : (arg).Front -1])
typedef struct FBI_tag
{
char type ;
struct FBI_tag *Left ;
struct FBI_tag *Right;
}*FBI;
typedef struct Queue_tag
{
int size ;
int Rear ;
int Front;
FBI Arr[1024] ;
}Queue;
void OutPut(FBI T) ;
void OutPut(FBI T)
{
if(!T)return ;
if(T->Left == NULL)
{
printf("%c", T->type) ;
return ;
}
else
{
OutPut(T->Left) ;
OutPut(T->Right);
printf("%c", T->type) ;
}
}
int main(void)
{
int n, i ;
int num ;
FBI FBI_left = NULL ;
char FBI_left_type ;
Queue FBI_Tree ;
FBI left, right, tmp ;
Queue_MakeEmpty(FBI_Tree) ;
scanf("%d", &n) ;
fflush(stdin) ;
num = 1 ;
num <<= n ;
for(i = 0; i < num; i++)
{
FBI_left = (FBI)calloc(1, sizeof(struct FBI_tag)) ;
if(!FBI_left) exit(1) ;
scanf("%c", &FBI_left_type) ;
FBI_left->type = FBI_left_type == '0' ? 'B' : 'I' ;
Queue_EnQueue(FBI_Tree, FBI_left) ;
FBI_left = NULL ;
}
while(Queue_Size(FBI_Tree) != 1)
{
tmp = (FBI)calloc(1, sizeof(struct FBI_tag)) ;
if(!tmp)exit(1) ;
left = Queue_DeQueue(FBI_Tree) ;
right = Queue_DeQueue(FBI_Tree) ;
tmp->Left = left ;
tmp->Right = right ;
tmp->type = left->type == 'B' && right->type == 'B' ? 'B' :
(left->type == 'I' && right->type == 'I' ? 'I' : 'F') ;
Queue_EnQueue(FBI_Tree, tmp) ;
tmp = left = right = NULL ;
}
tmp = Queue_DeQueue(FBI_Tree) ;
OutPut(tmp) ;
fflush(stdin) ;
getchar() ;
return 0 ;
}