数据结构实验之查找三:树的种类统计---(查找树做法与map做法)

随着卫星成像技术的应用,自然资源研究机构可以识别每一个棵树的种类。请编写程序帮助研究人员统计每种树的数量,计算每种树占总数的百分比。
Input

输入一组测试数据。数据的第1行给出一个正整数N (n <= 100000),N表示树的数量;随后N行,每行给出卫星观测到的一棵树的种类名称,树的名称是一个不超过20个字符的字符串,字符串由英文字母和空格组成,不区分大小写。
Output

按字典序输出各种树的种类名称和它占的百分比,中间以空格间隔,小数点后保留两位小数。
Example Input

2
This is an Appletree
this is an appletree
Example Output

this is an appletree 100.00%


看到这个题目的第一个想法就是利用map数组一一映射,由于测试不准谁用stl函数,先采用正常做法---加权二叉查找树的做法:

原理:由于按照字典序输出二叉树,因此这是一个基于字符串建立的树。

插入规则为,如果所插入的节点的字符串,在字符串比较函数strcmp()中与根值root->name比较,较小的放到根的左孩子上,较大的放在右孩子上,,如果与根值相同,则根值的权值++;

代码如下:

#include<algorithm>
#include<iostream>
#include<map>
#include<cstdio>
#include<cstring>
using namespace std;
struct node
{
    char name[25]; //mingzi
    int num;//quanzhi
    struct node *lchild, *rchild;
};
struct node *creat(struct node *root,char s[])
{
    if(!root)
    {
        root = new node;//不存在根节点,注意需要申请根节点,否则会CE
        strcpy(root->name,s);
        root->num=1;
        root->lchild =root->rchild =NULL;
    }
    else
    {
        if(strcmp(root->name,s)==0)
        {
            root->num++;
        }
        else  if(strcmp(root->name,s)>0)
        {
            root->lchild = creat(root->lchild, s);
        }
        else if(strcmp(root->name,s)<0)
        {
            root->rchild = creat(root->rchild, s);
        }
    }
    return root;
};
void  midprint(struct node *root, int n) //输出方式会 左孩子-根-右孩子 即中序遍历
{
    if(root){
    midprint(root->lchild, n);
    printf("%s %.2lf%%\n",root->name, 100*(double)root->num/n);
    midprint(root->rchild, n);
    }
}
int main()
{
    char s[25];
    int n;
   cin>>n;

        getchar();
        struct node *root;
        root = NULL;
        for(int j=0; j<n; j++)
        {
            gets(s);
            int m=strlen(s);
            //getsmall(s);
            for(int i=0; i<m; i++)
            {
                if(s[i]>='A' && s[i]<='Z')
                    s[i]=s[i]-'A'+'a';
            }
           root = creat(root,s);
        }
        //lchild-root---rchild
        midprint(root, n);

}
二:

利用 map函数映射 map<int  string>mp 实现 字符串与数字的一一对应,实现 mp[1] = str1  mp[2] =str2…… 如果来一个新的串strn  则将 mp[1]-mp[k](一共k个)与之一一匹配,如果已包含这个串,则桶排计数函数num[x]++(when -> map[x]==strn),  否则使得mp[k+1] =strn

这样完成后,利用冒泡排序对mp[i]数组进行一次从小到大的排序(注意num数组也要跟着变化)

然后输出即可,代码如下

#include<iostream>
#include<cstring>
#include<cstdio>
#include <map>
#include<algorithm>
using namespace std;
map<int,string>mp;
int a[100100];
char s[100];
void become(int n)
{
    for(int i=0;i<n;i++)
    {
        if(s[i]>='A' && s[i]<='Z')
        {
            s[i]=s[i]-'A'+'a';
        }
    }
}
int main ()
{
    int n;
    scanf("%d",&n);
    getchar();
    memset(a,0,sizeof(a));
    int px=0;
    for(int i=0;i<n;i++)
    {
        gets(s);
        int m=strlen((s));
        become(m);
        int flag=0;
        for(int i=0;i<px;i++)
        {
            if(mp[i]==s)
            {
                flag=1;
                a[i]++;
                break;
            }
        }
        if(!flag)
        {
            mp[px]=s;
            a[px]++;
            px++;
        }
    }
    string t;
    int k;
    for(int i=0; i<px-1;i++)
    {
        for(int j=i+1;j<px;j++)
        {
            if(mp[j]<mp[i])
            {
                t=mp[i];
                mp[i]=mp[j];
                mp[j]=t;
                k=a[i];
                a[i]=a[j];
                a[j]=k;
            }
        }
    }
    for(int i=0;i<px;i++)
    {
    cout<<mp[i]<<" ";
    a[i]*=100;
    printf("%.2lf%%\n",(double)a[i]/n);
    }
}



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在C++中,std::map 是使用红黑(RedBlack Tree)作为底层数据结构实现的。红黑是一种自平衡二叉查找,它具有以下特点: 1. 每个节点要么红色,要么是黑色。 2. 根节点是黑色的。 3. 所有叶子节点(NIL节点)都是黑色的。 4. 如果一个节点是红色的,则它的两个子节点都是黑色的。 5. 对于每个节点,从该节点到其后代叶子节点的简单路径上,均包含相同数目的黑色节点。 这些特性保证了红黑的平衡性,使得在最坏情况下,插入、删除和查找操作的时间复杂度都是O(log n)。 std::map 是C++标准库中提供的关联容器之一,它提供了一种键值对的存储方式,并根据键的顺序自动进行排序。通过使用红黑作为底层数据结构,std::map 可以在插入、删除和查找操作上具有较好的性能表现。 以下是一个使用 std::map 的简单示例: ```cpp #include <iostream> #include <map> int main() { std::map<int, std::string> myMap; // 插入键值对 myMap.insert(std::make_pair(1, "Apple")); myMap.insert(std::make_pair(2, "Banana")); myMap.insert(std::make_pair(3, "Orange")); // 遍历输出 for (const auto& pair : myMap) { std::cout << pair.first << ": " << pair.second << std::endl; } // 查找键对应的值 auto it = myMap.find(2); if (it != myMap.end()) { std::cout << "Value of key 2: " << it->second << std::endl; } return 0; } ``` 上述示例中,我们使用 std::map 存储了一些水果的名称,并按照键的顺序输出。通过调用 find() 函数,我们可以查找指定键对应的值。 希望以上回答能够解决你的问题!如有更多疑问,请继续提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值