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;
}

你可能感兴趣的:(poj)