您的位置:首页 > 其它

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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: