poj 2418 Hardwood Species
2011-11-05 20:32
225 查看
居然在POJ发现了老师布置的作业,一道二叉查找树,1A!
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct BST{
struct BST *l;
struct BST *r;
char name[50];
int count;
};
int total;
void insert(BST *root,char a[])
{
BST *rr;
if(strcmp(root->name,a)==0)
{
root->count++;
total++;
return;
}
else if(strcmp(root->name,a)>0)
{
if(root->l)
insert(root->l,a);
else
{
rr=(BST*)malloc(sizeof(BST));
rr->l=rr->r=NULL;
strcpy(rr->name,a);
rr->count=1;
root->l=rr;
total++;
}
}
else
{
if(root->r)
insert(root->r,a);
else
{
rr=(BST*)malloc(sizeof(BST));
rr->l=rr->r=NULL;
strcpy(rr->name,a);
rr->count=1;
root->r=rr;
total++;
}
}
}
void InOrder(BST *root)
{
if(root!=NULL)
{
InOrder(root->l);
printf("%s %.4f\n",root->name,double(root->count/(total+0.0))*100);
InOrder(root->r);
}
}
int main()
{
char s[50];
BST *root;
total=0;
gets(s);
root=(BST*)malloc(sizeof(BST));
root->l=root->r=NULL;
strcpy(root->name,s);
root->count=1;
total++;
while(gets(s)&&s[0]!='\0')
insert(root,s);
InOrder(root);
return 0;
}
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct BST{
struct BST *l;
struct BST *r;
char name[50];
int count;
};
int total;
void insert(BST *root,char a[])
{
BST *rr;
if(strcmp(root->name,a)==0)
{
root->count++;
total++;
return;
}
else if(strcmp(root->name,a)>0)
{
if(root->l)
insert(root->l,a);
else
{
rr=(BST*)malloc(sizeof(BST));
rr->l=rr->r=NULL;
strcpy(rr->name,a);
rr->count=1;
root->l=rr;
total++;
}
}
else
{
if(root->r)
insert(root->r,a);
else
{
rr=(BST*)malloc(sizeof(BST));
rr->l=rr->r=NULL;
strcpy(rr->name,a);
rr->count=1;
root->r=rr;
total++;
}
}
}
void InOrder(BST *root)
{
if(root!=NULL)
{
InOrder(root->l);
printf("%s %.4f\n",root->name,double(root->count/(total+0.0))*100);
InOrder(root->r);
}
}
int main()
{
char s[50];
BST *root;
total=0;
gets(s);
root=(BST*)malloc(sizeof(BST));
root->l=root->r=NULL;
strcpy(root->name,s);
root->count=1;
total++;
while(gets(s)&&s[0]!='\0')
insert(root,s);
InOrder(root);
return 0;
}
相关文章推荐
- POJ2418 Hardwood Species
- poj - 2418 Hardwood Species
- POJ 2418 Hardwood Species (trie树\map)
- poj2418Hardwood Species
- POJ 2418 Hardwood Species(STL在map应用)
- poj 2418 Hardwood Species
- [ACM] POJ 2418 Hardwood Species (Trie树或map)
- poj 2418 Hardwood Species(二叉排序树||map迭代器)
- POJ 2418 Hardwood Species【字典树 / map】
- POJ 2418 Hardwood Species (二叉搜索树/map)
- POJ2418——Hardwood Species
- poj_2418 Hardwood Species (字典序Trie树)
- POJ 2418 Hardwood Species
- Hardwood Species 字典树 poj 2418
- POJ 2418 Hardwood Species (map练习)
- [字典树] poj 2418 Hardwood Species
- poj 2418 Hardwood Species
- POJ 2418 Hardwood Species【字典树 / map】
- POJ 2418 Hardwood Species (字典树)
- POJ2418----Hardwood Species