#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
struct node
{
char word[35];
int count;
struct node *left,*right;
node(char *n)
{
strcpy(word,n);
count=1;
left=NULL;
right=NULL;
}
}*root;
node *insert(node *p,char *word)
{
if(p==NULL)
p= new node(word);
else if(strcmp(word,p->word)<0)
{
p->left = insert(p->left,word);
}
else if(strcmp(word,p->word)==0)
{
p->count++;
}
else
{
p->right = insert(p->right,word);
}
return p;
}
int sum;
void dfs(node *p)
{
if(p==NULL)
return ;
dfs(p->left);
printf("%s %.4lf\n",p->word,1.0*(p->count)/sum*100);
dfs(p->right);
delete(p);
}
int main()
{
sum=0;
char str[35];
while(gets(str))
{
root = insert(root,str);
sum++;
}
dfs(root);
// for(;;);
return 0;
}
二叉排序树
最新推荐文章于 2024-12-08 08:30:00 发布