正文

PKU2418 二叉搜索树2009-10-16 16:24:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/swell/49339.html

分享到:

http://acm.pku.edu.cn/JudgeOnline/problem?id=2418 二叉搜索树的典型题.... 很久没做ACM了,感觉有一个月那么多了吧. 现在来打这些也感觉生疏咯... BST用到这题目很合适,也有人说用qsort可以过,应该是题目时间太宽的缘故. 上代码...BST  #include <stdio.h> #include <string.h> #include <stdlib.h> typedef struct BSTNode { char name[100]; int times; struct BSTNode *lchild,*rchild; }BSTNode,*BSTree; int count; void BSTInsert(BSTree &t,char str[]) { BSTree p,f; p = t; while(p) { if(strcmp(str,p -> name) == 0) { p -> times++; return; } f = p; p = (strcmp(str,p -> name) < 0) ? p -> lchild : p -> rchild; } p = (BSTree) malloc (sizeof(BSTNode)); p -> lchild = p -> rchild = NULL; strcpy(p -> name,str); p -> times = 1; if(t == NULL) t = p; else { ( strcmp(p -> name,f -> name) < 0 ) ? (f -> lchild = p) : (f -> rchild = p); } } void InOrderTravel(BSTree &t) { if(t != NULL) { InOrderTravel(t -> lchild); printf("%s %.4f\n",t -> name,t -> times * 100.0 / count); InOrderTravel(t -> rchild); } } int main() { count = 0; BSTree t = NULL; char str[100]; while(gets(str) != NULL) { BSTInsert(t,str); count++; } InOrderTravel(t); return 0; }

阅读(1196) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册