思路就是先将每个单词存进二叉树中,没出现一次,修改该单词所在结点的cnt++;

最后通过递归中序遍历输出结果。

思路很清晰,主要注意一下指针的使用,想一想为什么要这么用?

简单的解释就是,insert函数修改的是指针的属性而不是指针指向的目标地址内容的属性。

代码:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;
int sum=;
//结构体里面似乎不能用string
// 指针的使用
struct node{
int cnt;
char word[];
node*l;
node*r;
}; void insertBST(node**root,char* word){
if(*root==NULL){
node*p=(node*)malloc(sizeof(node));
p->l=NULL;p->r=NULL;
p->cnt=;
strcpy(p->word,word);
*root=p;
}
else{
//if(word==(*root)->word){
if(strcmp(word,(*root)->word)==){
((*root)->cnt)++;
}
else if(strcmp(word,(*root)->word)<){
insertBST(&((*root)->l),word);
}
else{
insertBST(&((*root)->r),word);
}
}
} void midsearch(node*root){
if(root!=NULL){
midsearch(root->l);
printf("%s %.4lf\n",root->word,((double)root->cnt/(double)sum)*);
midsearch(root->r);
}
} int main(void){
node* root;
char w[];
while(gets(w)!=NULL){
insertBST(&root,w);//指针的指针,所以取的是root的地址而不是root
sum++;
}
midsearch(root);
return ;
}

POJ-2418 Hardwood Species(二叉搜索树)的更多相关文章

  1. 二叉搜索树 POJ 2418 Hardwood Species

    题目传送门 题意:输入一大堆字符串,问字典序输出每个字符串占的百分比 分析:二叉搜索树插入,然后中序遍历就是字典序,这里root 被new出来后要指向NULL,RE好几次.这题暴力sort也是可以过的 ...

  2. [字典树] poj 2418 Hardwood Species

    题目链接: id=2418">http://poj.org/problem?id=2418 Hardwood Species Time Limit: 10000MS   Memory ...

  3. POJ 1577 Falling Leaves 二叉搜索树

    HDU 3791 Falling Leaves 二叉搜索树  Figure 1Figure 1 shows a graphical representation of a binary tree of ...

  4. POJ 2418 Hardwood Species

                                                     Hardwood Species Time Limit: 10000MS   Memory Limit ...

  5. POJ 2418 Hardwood Species(STL在map应用)

    职务地址:id=2418">POJ 2418 通过这个题查了大量资料..知道了非常多曾经不知道的东西. . .. 在代码中凝视说明吧. 代码例如以下: #include <ios ...

  6. [ACM] POJ 2418 Hardwood Species (Trie树或map)

    Hardwood Species Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 17986   Accepted: 713 ...

  7. poj 2418 Hardwood Species (map)

    题目:http://poj.org/problem?id=2418 在poj 上交题总是有各种错误,再次感叹各个编译器. c++ AC代码,G++为超时,上代码: #include<cstdio ...

  8. Poj 2255 Tree Recovery(二叉搜索树)

    题目链接:http://poj.org/problem?id=2255 思路分析:根据先序遍历(如DBACEGF)可以找出根结点(D),其后为左右子树:根据中序遍历(如ABCDEFG),已知根结点(D ...

  9. POJ - 2418 Hardwood Species(map,trie,BST)

    1.输入若干行树名,输入结束后,按字典序输出树名及其所占百分比. 2.多种方法:map,trie,BST 3. map: #include<iostream> #include<st ...

  10. POJ 2418 Hardwood Species( AVL-Tree )

    #include <stdio.h> #include <stdlib.h> #include <math.h> #include <string.h> ...

随机推荐

  1. maven引用本地jar包

    教程:http://www.cnblogs.com/dcba1112/archive/2011/05/01/2033805.html 安装 到下载maven: http://maven.apache. ...

  2. Incompatible integer to pointer conversion assigning to 'NSInteger *' (aka 'int *') from 'NSInteger' (aka 'int')

    遇到这样的问题: integer to pointer conversion assigning to 'NSInteger *' (aka 'int *') from 'NSInteger' (ak ...

  3. 使用 postMessage + iframe 实现跨域通信

    一.postMessage window.postMessage() 方法可以安全地实现跨源通信.通常,对于两个不同页面的脚本,只有当执行它们的页面位于具有相同的协议(通常为https),端口号(44 ...

  4. Solr 整合

    1. Solr 与 Lucene 的区别 Lucene是一个开发源代码的全文检索引擎工具包,它不是一个完整的全文检索引擎,Lucene 提供了完整的查询引擎 和索引引擎,目的是为软件开发人员提供一个简 ...

  5. Xshell 连接虚拟机特别慢 解决方案

    由于各种原因,xshell连接虚拟机的rhel或者CentOS都几乎是龟速...... 今天专门查了一下解决方案: 原来是ssh的服务端在连接时会自动检测dns环境是否一致导致的,修改为不检测即可,操 ...

  6. 二叉树的先序、中序、后序和中序遍历——Java实现

    package treeTraverse; /** * 定义一个树的结点类 */ public class Node { private int data; private Node leftChil ...

  7. 不再依赖A*,利用C++编写全新寻路算法

    一,说在前面的话 大概在半年前,看见一到信息竞赛题:在任意方格阵中设置障碍物,确定起始点后,求这两点之间路径.当时觉得蛮有意思的,但是没有时间去做,今天花了两个小时来实现它.据说有一个更高级的寻路算法 ...

  8. Nginx+tomcat配置负载均衡集群

    操作系统版本:Centos 6.4 Nginx版本:nginx-1.3.15.tar.gz wget http://nginx.org/download/nginx-1.5.9.tar.gz JDK版 ...

  9. 存储库之——MongoDB

    阅读目录 一 简介 二 MongoDB基础知识 三 安装 四 基本数据类型 五 CRUD操作 六 可视化工具 七 pymongo 一 简介 MongoDB是一款强大.灵活.且易于扩展的通用型数据库1. ...

  10. day6-面向对象

    Python 面向对象 Python从设计之初就已经是一门面向对象的语言,正因为如此,在Python中创建一个类和对象是很容易的.本章节我们将详细介绍Python的面向对象编程. 如果你以前没有接触过 ...