# SDUT-3375 数据结构实验之查找三：树的种类统计

xiaoxiao2021-02-28  11

### 数据结构实验之查找三：树的种类统计

Time Limit: 400MS  Memory Limit: 65536KB Submit  Statistic  Discuss

#### Example Input

2 This is an Appletree this is an appletree

#### Example Output

this is an appletree 100.00%

#### Author

xam  可以用二叉排序树做，结构体加入一个变量num记录某条语句的出现次数， 将输入的字符串统一转为小写，最后中序遍历二叉树输出字符串 #include <bits/stdc++.h> using namespace std; string a,b; int n; typedef struct node { string data; node*left,*right; int num; } tree; tree*creat(tree*root,string x) { if(root==NULL) { root=new tree; root->data=x; root->left=root->right=NULL; root->num=1; } else { if(root->data==x) root->num++; if(x<root->data) root->left=creat(root->left,x); if(x>root->data) root->right=creat(root->right,x); } return root; } void mid(tree*root) { if(root) { mid(root->left); cout<<root->data<<' ';printf("%.2f%%\n",root->num*1.0*100/n); ///cout<<root->data<<' '<<setiosflags(ios::fixed)<<setprecision(2)<<(1.0*100*root->num)/n<<'%'<<endl; mid(root->right); } } int main() { tree*root=NULL; scanf("%d",&n); getchar(); for(int i=0; i<n; i++) { getline(cin,a); for(int i=0;a[i]!='\0';i++) { if(a[i]>='A'&&a[i]<='Z') a[i]+=32; } root=creat(root,a); } mid(root); return 0; }