数据结构实验之查找三:树的种类统计
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using
namespace std;
struct tree {
int cou;
char a[20+5];
tree *l, *r;
tree()
{
l=r=NULL;
cou=0;
}
};
char s[20+5];
int n;
tree *insert(tree *root)
{
if(!root) {
root=new tree;
strcpy(root->a,s);
root->cou+
+;
}
else if(strcmp(s,root->a)<0)
root->l=insert(root->l);
else if
(strcmp(s,root->a)>0)
root->r=insert(root->r);
else root->cou++;
return root;
}
void on_order(tree *root) {
if(root) {
on_order(root->l);
printf("%s %.2lf%%\n", root->a, root->cou*100.0/n);
on_order(root->r);
}
}
int main() {
while(~scanf("%d", &n)) {
getchar();
tree
*root=NULL;
for(int j=0;j<n;j++) {
gets(s);
for(int
i=0;s[i]!='\0';i++)
if(s[i]>='A'&&s[i]<='Z')
s
[i]+=32;
root=insert(root);
}
on_order(root);
}
}