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

来源:互联网 发布:电磁炉 知乎 编辑:程序博客网 时间:2024/06/07 08:55
#include <iostream>#include<stdio.h>#include<stdlib.h>#include<algorithm>#include<cstring>#include<iomanip>typedef struct node{    char data[21];    int num;    node* lchild;    node* rchild;} Btree,*Qtree; int flag;     int m;using namespace std;char *little(char *s){    int i = 0;    while(s[i]!='\0')    {        if(s[i]>='A'&&s[i]<='Z')            s[i] = s[i]+32;        i++;    }    return s;}Qtree Create(Qtree root,char *p){    little(p);    if(root==NULL)    {        root = (Qtree)malloc(sizeof(Btree));        root->lchild = NULL;        root->rchild = NULL;        strcpy(root->data,p);        root->num = 1;    }    else    {        if(strcmp(p,root->data)<0)        {           root->lchild = Create(root->lchild,p);        }        else if(strcmp(p,root->data)>0)        {           root->rchild= Create(root->rchild,p);        }        else            root->num++;    }    return root;}void Midprint(Qtree root){    if(root)    {        Midprint(root->lchild);        cout<<root->data<<" "<<setiosflags(ios_base::fixed)<<setprecision(2)<<(root->num)*100.0/m<<"%"<<endl;        Midprint(root->rchild);    }}int main(){    char s[50];    Qtree root;    root = NULL;    int n;    cin>>n;    m = n;    getchar();    while(n--)    {        gets(s);        root = Create(root,s);    }    Midprint(root);}
原创粉丝点击