#include <bits/stdc++.h>
using namespace std;
typedef struct BinaryTree
{
char data[23];
int cnt; /// 记录此时该类树的个数
struct BinaryTree *Left;
struct BinaryTree *Right;
}Node;
int n;
void BiTreeCreat(Node *&root, char st[]); /// 创建二叉树
void InTraversal(Node *root); /// 中序遍
int main()
{
int t;
Node *root = NULL;
cin >> t;
getchar(); /// 用gets()得到字符串 需吃一个回车
n = t; /// 记录总数
while(t--)
{
char st[23];
gets(st);
int len = strlen(st);
for(int i = 0; i < len; i++)
{
if(st[i] >='A' && st[i] <= 'Z')
{
st[i] = st[i] + 32; /// 将大写字符转化为小写字符
}
}
BiTreeCreat(root, st);
}
InTraversal(root);
return 0;
}
void BiTreeCreat(Node *&root, char st[]) /// 创建二叉树
{
if(!root) /// 如果此时二叉树为空
{
root = new Node;
root->cnt = 1;
strcpy(root->data, st);
root->Left = NULL;
root->Right = NULL;
}
else
{
int t = strcmp(root->data, st); /// 按字典序建树
if(t > 0)
{
BiTreeCreat(root->Left, st);
}
else if(t < 0)
{
BiTreeCreat(root->Right, st);
}
else
{
root->cnt++; /// 如果此时节点数据域等于字符串 累计++;
}
}
}
void InTraversal(Node *root)
{
if(root)
{
InTraversal(root->Left);
printf("%s %.2f%%\n",root->data, 100.0*root->cnt/n);
InTraversal(root->Right);
}
}
树的种类统计
最新推荐文章于 2018-12-18 21:11:00 发布