POJ 2418 Hardwood Species(二叉排序树)

Hardwood Species

Time Limit: 10000MS

 

Memory Limit: 65536K

Total Submissions: 12870

 

Accepted: 5261

Description

Hardwoods are the botanical group of trees that have broad leaves, produce a fruit or nut, and generally go dormant in the winter. 
America's temperate climates produce forests with hundreds of hardwood species -- trees that share certain biological characteristics. Although oak, maple and cherry all are types of hardwood trees, for example, they are different species. Together, all the hardwood species represent 40 percent of the trees in the United States. 

On the other hand, softwoods, or conifers, from the Latin word meaning "cone-bearing," have needles. Widely available US softwoods include cedar, fir, hemlock, pine, redwood, spruce and cypress. In a home, the softwoods are used primarily as structural lumber such as 2x4s and 2x6s, with some limited decorative applications. 

Using satellite imaging technology, the Department of Natural Resources has compiled an inventory of every tree standing on a particular day. You are to compute the total fraction of the tree population represented by each species.

Input

Input to your program consists of a list of the species of every tree observed by the satellite; one tree per line. No species name exceeds 30 characters. There are no more than 10,000 species and no more than 1,000,000 trees.

Output

Print the name of each species represented in the population, in alphabetical order, followed by the percentage of the population it represents, to 4 decimal places.

Sample Input

Red Alder
Ash
Aspen
Basswood
Ash
Beech
Yellow Birch
Ash
Cherry
Cottonwood
Ash
Cypress
Red Elm
Gum
Hackberry
White Oak
Hickory
Pecan
Hard Maple
White Oak
Soft Maple
Red Oak
Red Oak
White Oak
Poplan
Sassafras
Sycamore
Black Walnut
Willow

Sample Output

Ash 13.7931
Aspen 3.4483
Basswood 3.4483
Beech 3.4483
Black Walnut 3.4483
Cherry 3.4483
Cottonwood 3.4483
Cypress 3.4483
Gum 3.4483
Hackberry 3.4483
Hard Maple 3.4483
Hickory 3.4483
Pecan 3.4483
Poplan 3.4483
Red Alder 3.4483
Red Elm 3.4483
Red Oak 6.8966
Sassafras 3.4483
Soft Maple 3.4483
Sycamore 3.4483
White Oak 10.3448
Willow 3.4483
Yellow Birch 3.4483

Hint

This problem has huge input, use scanf instead of cin to avoid time limit exceeded.

Source

Waterloo Local 2002.01.26

 解题报告:这道题就是统计输入的树叶类型占全部的百分比,并按字典序的顺序输出结果,我们可以利用二叉排序树来做这道题,因为二叉排序树。 它或者是一棵空树;或者是具有下列性质的二叉树: (1)若左子树不空,则左子树上所有结点的值均小于它的根结点的值; (2)若右子树不空,则右子树上所有结点的值均大于它的根结点的值; (3)左、右子树也分别为二叉排序树。

代码如下:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;
const int MAX = 35;
char str[MAX];//输入的数据
int sum = 0;//记录输入的总个数
struct BiTree//二叉树的节点类型
{
    int num;//存储这种类型的树叶的个数
    char Species[MAX];//树叶的名字
    struct BiTree *lchild;//左孩子
    struct BiTree *rchild;//右孩子
};
struct BiTree *T;//二叉树的树根
struct BiTree *NewSet()//建立新的节点
{
    struct BiTree *p;
    p = (struct BiTree *)malloc(sizeof(struct BiTree));
    p->lchild = NULL;
    p->rchild = NULL;
    p->num = 1;
    return p;//返回类型
}
void Insert(char *str, struct BiTree *T)//节点的插入函数
{
    if (strcmp(str, T->Species) == 0)//若树叶的类型已经有了
    {
        T->num ++;//这种类型加一
        return;
    }
    else if (strcmp(str, T->Species) < 0)//若要插入的节点小于该节点就遍历它的左孩子
    {
        if (T->lchild == NULL)//若没有左孩子时
        {
            T->lchild = NewSet();//插入新的节点
            strcpy(T->lchild->Species, str);//给左孩子的树叶类型赋值
            return;
        }
        else
        {
            Insert(str, T->lchild);//遍历它的左孩子
        }

    
    
    }
    else//若要插入的节点大于该节点就遍历它的右孩子
    {
        if (T->rchild == NULL)//若没有右孩子时
        {
            T->rchild = NewSet();
            strcpy(T->rchild->Species, str);
            return;
        }
        else
        {
            Insert(str, T->rchild);
        }
    }

}
void PrintBiTree(struct BiTree *T)
{
    if (T->lchild == NULL && T->rchild == NULL)//对叶子节点的处理
    {
        printf("%s %.4f\n", T->Species, T->num * 100.00 / sum);
        return;
    }
    if (T->lchild)
    {
        PrintBiTree(T->lchild);
    }
    printf("%s %.4f\n", T->Species, T->num * 100.00 / sum);
    if (T->rchild)
    {
        PrintBiTree(T->rchild);
    }
}
int main()
{
    gets(str);
    T = NewSet();//开辟根节点
    strcpy(T->Species, str);
    sum = 1;
    while (gets(str) != NULL)
    {
        sum ++;
        Insert(str, T);
    }
    PrintBiTree(T);//输出函数
    return 0;
}
原文地址:https://www.cnblogs.com/lidaojian/p/2446335.html