poj 2418二插排序树

View Code
#pragma warning (disable : 4786)
#include<stdio.h>
#include<map>
#include<string>
#include<iostream>
using namespace std;
int main()
{
char str[50];
double num=0;
map<string,int> M;
while(gets(str)!=NULL)
{
M[str]++;
num++;
}
map<string,int>::iterator it;
for(it=M.begin();it!=M.end();it++)
printf("%s %.4lf\n",it->first.c_str(),it->second*1.0/num*100);
return 0;
}

 

二叉排序树

View Code
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct node{
char name[33];
int times;
struct node *left;
struct node *right;
}*tree;
int tot=0;
node *build(node *root,char s[])
{
if(root==NULL)
{
root=new node;
root->left=NULL;
root->right=NULL;
root->times=1;
strcpy(root->name,s);
return root;
}
else
{
int t=strcmp(root->name,s);
if(t==0)
root->times++;
else if(t>0)
root->left=build(root->left,s);
else
root->right=build(root->right,s);
return root;
}
}
void visit(node *root)
{
if(root!=NULL)
printf("%s %.4f\n",root->name,root->times*1.0/tot*100);
}
void midorder(node *root)
{
if(root!=NULL)
{
midorder(root->left);
visit(root);
midorder(root->right);
}
}
int main()
{
char s[33];
while(gets(s)!=NULL)
{
tot++;
tree=build(tree,s);
}
midorder(tree);
return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值