很基本的排序二叉树
/* author:jxy lang:C/C++ university:China,Xidian University **If you need to reprint,please indicate the source** */ #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <queue> #define INF 1E9 using namespace std; int all; struct node { bool used; char name[35]; int num,l,r; }; node tree[10005]; char s[35]; int cnt; void increase(int now) { node &nn=tree[now]; if(nn.used==0) { strcpy(nn.name,s); nn.used=1; nn.num++; return; } int c=strcmp(s,nn.name); if(c<0) { if(nn.l==0)nn.l=++cnt; increase(nn.l); } else if(c>0) { if(nn.r==0)nn.r=++cnt; increase(nn.r); } else nn.num++; } void output(int now) { node &nn=tree[now]; if(nn.used==0)return; output(nn.l); printf("%s %.4f\n",nn.name,(double)nn.num/all*100.0); output(nn.r); return; } int main() { memset(tree,0,sizeof(tree)); all=0;cnt=1; while(gets(s)) { increase(1); all++; } output(1); }