poj2418 Hardwood Species 排序二叉树

很基本的排序二叉树

 

/*
author:jxy
lang:C/C++
university:China,Xidian University
**If you need to reprint,please indicate the source**
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
#define INF 1E9
using namespace std;
int all;
struct node
{
    bool used;
    char name[35];
    int num,l,r;
};
node tree[10005];
char s[35];
int cnt;
void increase(int now)
{
    node &nn=tree[now];
    if(nn.used==0)
    {
        strcpy(nn.name,s);
        nn.used=1;
        nn.num++;
        return;
    }
    int c=strcmp(s,nn.name);
    if(c<0)
    {
        if(nn.l==0)nn.l=++cnt;
        increase(nn.l);
    }
    else if(c>0)
    {
        if(nn.r==0)nn.r=++cnt;
        increase(nn.r);
    }
    else nn.num++;
}
void output(int now)
{
    node &nn=tree[now];
    if(nn.used==0)return;
    output(nn.l);
    printf("%s %.4f\n",nn.name,(double)nn.num/all*100.0);
    output(nn.r);
    return;
}
int main()
{
    memset(tree,0,sizeof(tree));
    all=0;cnt=1;
    while(gets(s))
    {
        increase(1);
        all++;
    }
    output(1);
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值