Section 3.4 American Heritage

/*
ID: niepeng1
PROG: heritage
LANG: C++
*/
#include <iostream>
#include <string.h>
using namespace std;
#define Max1 30
int n;
FILE *in,*out;
struct Node{
 Node(){
  right=NULL;
  left=NULL;
 }
 char c;
 Node *right;
 Node *left;
};
int Seek(char ch[],char t)
{
 int i;
 for( i=0; i<n; i++)
 {
  if( ch[i] == t)
   return i;
 }
 return -1;
}
void Post(Node *p)
{
 if( !p)
  return;
 Post(p->left);
 Post(p->right);
 fprintf(out,"%c",p->c);

 
}
void convert(Node *p,char Pre[],char In[],int lenPre)
{
 char *Pre1,*In1,*Pre2,*In2;
 int len1,len2;
 int i;
 i = Seek(In,Pre[0]);
 
 p->c=Pre[0];
 Node *q=new Node;
 Node *q1=new Node;
 Pre1=Pre+1;
 Pre2=Pre+1+i;
 In1=In;
 len1=i;
 In2=In+i+1;
 len2=lenPre-i-1;
 if( i > 0)
 {
  p->left=q;
  convert(q,Pre1,In1,len1);
 }
 if( i < lenPre -1)
 {
  p->right=q1;
  convert(q1,Pre2,In2,len2);
 }
}
int main()
{

 char Pre[Max1],In[Max1];
 
 in = fopen ("heritage.in", "r");
 out = fopen ("heritage.out", "w");
 fscanf(in,"%s %s",In,Pre);
 n=strlen(Pre);
 Node *p;
 p=new Node;
 convert(p,Pre,In,n);

 
 
 Post(p);
 fprintf(out,"/n");
 fclose(in);fclose(out);
 return 1;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值