MakeTree

 

 

 

#include <stdio.h>
#include <string>
char  s1[27], s2[27];
char  result[27];
int ind1 = 0, ind2 = 0, n = 0;
int len = 0;

int root;

typedef struct _node {
 int  lnode;
 int  rnode;
} node_t;

node_t node[27];

int map[2][27];
int ti = 0;
inline void maketree(int root, int n1, int n2, bool blflag = true)
{
 int ind = ind1, nn, root1;
    if(n1 > n2 || ind1 >= len) return;
 while(1) {
  nn = (int)&s2[map[0][(int)&s1[ind1] - (int)&s1[0]]] - (int)&s2[0];
  if(nn >= n1 && nn <= n2) break;
  ind1++;
  if(ind1 >= len) return;
 }
 if(blflag) {
  root1 = node[root].lnode = s2[nn] - 'A';
 }
 else       {
  root1 = node[root].rnode = s2[nn] - 'A';
 }
   
 maketree(root1, n1, nn - 1, true);
 maketree(root1, nn + 1, n2, false);
}

inline void traversetree(int root)
{
 if(node[root].lnode != -1) traversetree(node[root].lnode);
 if(node[root].rnode != -1) traversetree(node[root].rnode);
 result[ti++] = root + 'A';
}

int main()
{
 while(scanf("%s%s", &s1, &s2) != EOF)
 {
  ind1 = 0; ti = 0;
  len = strlen(s1);
  for(int i = 0; i < 27; i++) {node[i].lnode = -1; node[i].rnode = -1;}
  for(int i = 0; i < len; i++) {
   for(int j = 0; j < len; j++) {
    if(s1[i] == s2[j]) {map[0][i] = j; map[1][j] = i; break;}
   }
  }
        root = n = s1[ind1] - 'A';
  ind2 = map[0][(int)&s1[ind1] - (int)&s1[0]];
  ind1++;
  maketree(root, 0, ind2 - 1, true);
  maketree(root, ind2 + 1, len, false);
  traversetree(root);
  result[ti] = 0;
  printf("%s\n", result);
 }

 return 0;
}

 

#include <bits/stdc++.h> using namespace std; #define MAXSIZE 1001 int tot; struct Node{ string data; int lchild; int rchild; int fchild; }node[MAXSIZE]; int stack_num[MAXSIZE];//下标数组 int stack_op[MAXSIZE];//运算符数组 int top_num; int top_op; int op_rank[255]; int makenode(string x){ tot++; node[tot].data=x; node[tot].lchild=0; node[tot].rchild=0; node[tot].fchild=0; return tot; }//叶子节点的构造 int maketree(int a,int b,char c){ tot++; node[tot].data=c; node[tot].lchild=a; node[tot].rchild=b; node[tot].fchild=0; node[a].fchild=tot; node[b].fchild=tot; return tot; }//叶子结点构造树 void print_tree(int x){ if(x==0) return; print_tree(node[x].lchild); print_tree(node[x].rchild); cout<<node[x].data<<' '; }//后序遍历 void push_num(int num){ top_num++; stack_num[top_num]=num; } int pop_num(){ return stack_num[top_num--]; } void push_op(char c){ top_op++; stack_op[top_op]=c; } char pop_op(){ return stack_op[top_op--]; } void solve(){ char c=pop_op(); while(c!='('){ int b=pop_num(); int a=pop_num(); push_num(maketree(a,b,c)); c=pop_op(); } } void judge_op(char c){ if(op_rank[c]>op_rank[stack_op[top_op]]||top_op==0){ push_op(c); return; } char x=pop_op(); int b=pop_num(); int a=pop_num(); push_num(maketree(a,b,x)); judge_op(c); } void clearstack(){ while(top_op!=0){ char c=pop_op(); int b=pop_num(); int a=pop_num(); push_num(maketree(a,b,c)); } } int main(){ op_rank['(']=1; op_rank['+']=2; op_rank['-']=2; op_rank['*']=3; op_rank['/']=3; op_rank[')']=4; string s; getline(cin,s); s=s+'@'; tot=0; string s_num; for(int i=0;i<s.size();i++){ switch(s[i]){ case '@': if(s_num!="") push_num(makenode(s_num)); clearstack(); break; case'(': push_op('('); solve(); break; case '+': case '-': case '*': case '/': if(s_num!="") push_num(makenode(s_num)); s_num=""; judge_op(s[i]); break; default: s_num+=s[i]; break; } } print_tree(pop_num()); return 0; }修改此代码使能输出结果
05-24
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值