当前位置:网站首页>二叉搜索树解决硬木问题
二叉搜索树解决硬木问题
2022-08-04 20:38:00 【chengqiuming】
一 原问题链接
Hardwood Species - POJ 2418 - Virtual Judgehttps://vjudge.net/problem/POJ-2418
二 输入和输出
1 输入
输入包括每棵树的物种清单,每行一棵树。物种名称不超过 30 个字符,不超过 10 000 种,不超过 1 000 000 棵树。
2 输出
按字母顺序输出植物种群中代表的每个物种的名称,然后是占所有种群的百分比,保留小数点后 4 位。
三 输入和输出样例
1 输入样例
Red Alder
Ash
Aspen
Basswood
Ash
Beech
Yellow Birch
Ash
Cherry
Cottonwood
Ash
Cypress
Red Elm
Gum
Hackberry
White Oak
Hickory
Pecan
Hard Maple
White Oak
Soft Maple
Red Oak
Red Oak
White Oak
Poplan
Sassafras
Sycamore
Black Walnut
Willow
2 输出样例
Ash 13.7931
Aspen 3.4483
Basswood 3.4483
Beech 3.4483
Black Walnut 3.4483
Cherry 3.4483
Cottonwood 3.4483
Cypress 3.4483
Gum 3.4483
Hackberry 3.4483
Hard Maple 3.4483
Hickory 3.4483
Pecan 3.4483
Poplan 3.4483
Red Alder 3.4483
Red Elm 3.4483
Red Oak 6.8966
Sassafras 3.4483
Soft Maple 3.4483
Sycamore 3.4483
White Oak 10.3448
Willow 3.4483
Yellow Birch 3.4483
四 算法设计
使用二叉搜索树,先将每个单词都存入二叉树中,每出现一次,就修改该单词所在节点 cnt,让它加1,最后通过中序遍历输出结果。
五 代码
package poj2418;
import java.util.Scanner;
public class Poj2418 {
static int sum = 0;
static node rt = new node();
static String w;
// 中序遍历
static void midprint(node root) {//中序遍历
if (root != null) {
midprint(root.l);
System.out.print(root.word);
System.out.printf(" %.4f\n", ((double) root.cnt / (double) sum) * 100);
midprint(root.r);
}
}
// 二叉排序树的插入
static public node insert(node root, String s) {
if (root == null) {
node p = new node(s, 1);
p.l = null;
p.r = null;
root = p;
return root;
}
// 一个临时节点指向根节点,用于返回值
node tmp = root;
node pre = root;
while (root != null) {
// 保存父节点
pre = root;
if (s.compareTo(root.word) > 0) {
root = root.r;
} else if ((s.compareTo(root.word) < 0)) {
root = root.l;
} else {
root.cnt++;
return tmp;
}
}
// 通过父节点添加
if (s.compareTo(pre.word) > 0) {
pre.r = new node(s, 1);
} else {
pre.l = new node(s, 1);
}
return tmp;
}
public static void main(String[] args) {
rt = null; // 一定要初始化
Scanner scanner = new Scanner(System.in);
while (true) {
w = scanner.nextLine();
if (w.equals("##")) {
break;
}
rt = insert(rt, w);
sum++;
}
midprint(rt);
}
}
class node {
String word;
node l;
node r;
int cnt;
public node() {
}
public node(String word, int cnt) {
this.word = word;
this.cnt = cnt;
}
}六 测试
1 输入

2 输出

边栏推荐
- EasyUi常用代码
- idea源码无法下载
- MySQL stored procedure introduction, creation, case, delete, view "recommended collection"
- 刷题-洛谷-P1179 数字统计
- How to carry out AI business diagnosis and quickly identify growth points for cost reduction and efficiency improvement?
- Oreo domain name authorization verification system v1.0.6 public open source version website source code
- Win10 uwp use ScaleTransform magnify an element
- If it is test axi dma catch a few words here
- 项目难管理?先学会用好甘特图(内附操作方法及实用模板)
- 从卖产品到卖“链路”:20条策略 解读直播带货迭代玩法
猜你喜欢
随机推荐
Uniapp微信雪糕刺客单页小程序源码
微信小程序云开发 | 赠、删、改城市名称信息的应用实现
数据安全解决方案的发展
Zero-knowledge proof - zkSNARK proof system
Oreo domain name authorization verification system v1.0.6 public open source version website source code
用 Excel 爬取网络数据的四个小案例
Differences in the working mechanism between SAP E-commerce Cloud Accelerator and Spartacus UI
【一起学Rust | 进阶篇 | Service Manager库】Rust专用跨平台服务管理库
【数据挖掘】搜狐公司数据挖掘工程师笔试题
如何找到某个 ABAP structure 某字段的源头来自哪个数据库表
【SQL】触发器同步表数据
MySQL field type
ts集成和使用
WIN10系统如何开启终端
idea源码无法下载
[Academic related] Tsinghua professor persuaded to quit his Ph.D.:I have seen too many doctoral students have mental breakdowns, mental imbalances, physical collapses, and nothing!...
Using Baidu EasyDL to realize forest fire early warning and identification
泰山OFFICE技术讲座:底纹、高亮、边框的关系
动态数组底层是如何实现的
Red5搭建直播平台







![[TypeScript] In-depth study of TypeScript enumeration](/img/27/4836e59528bb5a51ffc1cf9961c6b6.png)

