數據結構實驗之查找三:樹的種類統計

數據結構實驗之查找三:樹的種類統計

#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);
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章