POJ 2418 Hardwood Species 二叉排序树 (二)

2014-11-24 02:46:59 · 作者: · 浏览: 5
;
return 0;
}

#include
#include
#include
#define MAXN 40

typedef struct TNode{
char name[MAXN];
struct TNode *l, *r;
int cnt;
}Node;
int sum = 0;

Node* newnd(char *ch)
{
Node *u = (Node*) malloc(sizeof(Node));
if (u != NULL)
{
strcpy(u->name, ch);
u->r = u->l = NULL;
u->cnt = 1;
sum++;
}
return u;
}

Node* addnd(Node *nd, char *ch)
{
if (nd == NULL)
nd = newnd(ch);
else
{
int cmp = strcmp(nd->name, ch);
if (cmp == 0)
{
nd->cnt++;
sum++;
}
else if (cmp > 0)
nd->r = addnd(nd->r, ch);
else
nd->l = addnd(nd->l, ch);
}
return nd;
}

void Inorder(Node* root)
{
if (root != NULL)
{
Inorder(root->r);
printf("%s %.4f\n", root->name, 100*(double)root->cnt/(double)sum);
Inorder(root->l);
}
}

int main()
{
char ch[MAXN];
Node* root = NULL;
while (gets(ch) != NULL)
root = addnd(root, ch);
Inorder(root);
return 0;
}