|
沙发
楼主 |
发表于 2018-9-7 16:47:18
|
只看该作者
- #include<iostream>
- using namespace std;
- string s1;
- int pi;
- struct tree
- {
- string s;
- char k;
- tree *left,*right;
- }*h;
- bool flag0,flag1;
- char j(string x)
- {
- flag0=0;flag1=0;
- for(int i=0;i<x.size();i++)
- {
- if(x[i]=='0')flag0=1;
- if(x[i]=='1') flag1=1;
- }
- if(flag1)
- {
- if(flag0)return 'F';
- else return 'I';
- }
- else return 'B';
- }
- void bt(tree *&q,string x)
- {
- if(x.size()<=1)return;
- string x1,x2;
- int n=x.size()/2;
- for(int i=0;i<=n-1;i++)
- {
- x1+=x[i];
- x2+=x[i+n];
- }
- tree *ls,*rs;
- ls=new tree;
- rs=new tree;
- ls->left=NULL;
- ls->right=NULL;
- rs->left=NULL;
- rs->right=NULL;
- q->left=ls;
- q->right=rs;
- ls->s=x1;
- rs->s=x2;
- ls->k=j(x1);
- rs->k=j(x2);
- bt(ls,x1);
- bt(rs,x2);
- }
- void hx(tree *x)
- {
- if(x!=NULL)
- {
- hx(x->left);
- hx(x->right);
- cout<<x->k;
- }
- }
- int main()
- {
- cin>>pi;
- cin>>s1;
- h=new tree;
- h->s=s1;
- h->k=j(s1);
- bt(h,s1);
- hx(h);
- return 0;
- }
复制代码 |
|