poj 2418 二叉搜索树

http://acm.pku.edu.cn/JudgeOnline/problem?id=2418

输入一系列的的单词,然后统计单词出现的频率wordcount / count * 100,输出要从小到大排列输出,结果保留小数点后4位。

package poj; import java.text.DecimalFormat; import java.util.Iterator; import java.util.Scanner; import java.util.Set; import java.util.Stack; import java.util.TreeMap; public class Main2418 { static DecimalFormat format = new DecimalFormat("0.0000"); public static void main(String[] args) { Scanner sc = new Scanner(System.in); /* ---------------用java API实现的---------------- */ // TreeMap<String, Integer> trees = new TreeMap<String, Integer>(); // int sum = 0; // while (sc.hasNextLine()) { // ++sum; // String str = sc.nextLine(); // if (trees.containsKey(str)) { // int tmp = trees.get(str); // trees.put(str, ++tmp); // } else { // trees.put(str, 1); // } // } // Set<String> keyset = trees.keySet(); // Iterator<String> iter = keyset.iterator(); // while (iter.hasNext()) { // String key = iter.next(); // System.out.println(key + " " // + format.format((float) trees.get(key) / sum * 100)); // // } /* ------------------专用程序分割线----------------------- */ searchTree = new TreeNode(); while (sc.hasNextLine()) { Main2418.sum++; String str = sc.nextLine(); Main2418.put(str, 1); } Main2418.inOrder(Main2418.searchTree); } protected static TreeNode searchTree = null; protected static int sum = 0; /** * 二叉搜索树添加节点 * * @param key * @param value */ public static void put(String key, Integer value) { if (searchTree.key == null) { searchTree.key = key; searchTree.value = value; } else { TreeNode leave = new TreeNode(); leave.key = key; leave.value = value; TreeNode pointer = searchTree; while (pointer != null) { if (key.compareTo(pointer.key) > 0) { if (pointer.right != null) { pointer = pointer.right; } else if (pointer.right == null) { pointer.right = leave; break; } } else if (key.compareTo(pointer.key) < 0) { if (pointer.left != null) { pointer = pointer.left; } else if (pointer.left == null) { pointer.left = leave; break; } } else { pointer.value++;// 这道题特有的 break; } } } } public static void inOrder(TreeNode tree) { /* -------------递归中序遍历(这道题会把runtime error)------------ */ // if (tree != null) { // inOrder(tree.left); // System.out.println(tree.key + " " // + format.format((float) tree.value / sum * 100)); // inOrder(tree.right); // // } /* ----------------专用程序分割线---------------- */ Stack<TreeNode> stack = new Stack<TreeNode>(); do { if (tree != null) { // System.out.println(tree.key); stack.push(tree); tree = tree.left; // System.out.println(stack.isEmpty()); } else { TreeNode tmp = stack.pop(); System.out.println(tmp.key + " " + format.format((float) tmp.value / sum * 100)); tree = tmp.right; /* -----------------为了防止到根节点时,stack为空而退出循环------------- */ if (tree != null) { stack.push(tree); tree = tree.left; } } } while (!stack.isEmpty()); } static class TreeNode { String key = null; Integer value = null; TreeNode left = null; TreeNode right = null; } }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值