• 二叉搜索树解决硬木问题


    一 原问题链接

    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,最后通过中序遍历输出结果。

    五 代码

    1. package poj2418;
    2. import java.util.Scanner;
    3. public class Poj2418 {
    4. static int sum = 0;
    5. static node rt = new node();
    6. static String w;
    7. // 中序遍历
    8. static void midprint(node root) {//中序遍历
    9. if (root != null) {
    10. midprint(root.l);
    11. System.out.print(root.word);
    12. System.out.printf(" %.4f\n", ((double) root.cnt / (double) sum) * 100);
    13. midprint(root.r);
    14. }
    15. }
    16. // 二叉排序树的插入
    17. static public node insert(node root, String s) {
    18. if (root == null) {
    19. node p = new node(s, 1);
    20. p.l = null;
    21. p.r = null;
    22. root = p;
    23. return root;
    24. }
    25. // 一个临时节点指向根节点,用于返回值
    26. node tmp = root;
    27. node pre = root;
    28. while (root != null) {
    29. // 保存父节点
    30. pre = root;
    31. if (s.compareTo(root.word) > 0) {
    32. root = root.r;
    33. } else if ((s.compareTo(root.word) < 0)) {
    34. root = root.l;
    35. } else {
    36. root.cnt++;
    37. return tmp;
    38. }
    39. }
    40. // 通过父节点添加
    41. if (s.compareTo(pre.word) > 0) {
    42. pre.r = new node(s, 1);
    43. } else {
    44. pre.l = new node(s, 1);
    45. }
    46. return tmp;
    47. }
    48. public static void main(String[] args) {
    49. rt = null; // 一定要初始化
    50. Scanner scanner = new Scanner(System.in);
    51. while (true) {
    52. w = scanner.nextLine();
    53. if (w.equals("##")) {
    54. break;
    55. }
    56. rt = insert(rt, w);
    57. sum++;
    58. }
    59. midprint(rt);
    60. }
    61. }
    62. class node {
    63. String word;
    64. node l;
    65. node r;
    66. int cnt;
    67. public node() {
    68. }
    69. public node(String word, int cnt) {
    70. this.word = word;
    71. this.cnt = cnt;
    72. }
    73. }

    六 测试

    1 输入

    2 输出

  • 相关阅读:
    LeetCode——比较字符串最小字母出现频次
    设计模式代码
    SQLDEV平台教学 - 权限配置
    CSP模拟58联测20 注视一切的终结
    大数据技术在金融行业反洗钱业务的应用分析
    把zoom视频会议web客户端嵌入企业平台
    第八章:关系数据库设计
    视野修炼-技术周刊第57期
    什么是IO
    人工智能:群智能算法的一般框架、特点和不足
  • 原文地址:https://blog.csdn.net/chengqiuming/article/details/126165680