Java(11)

article/2025/8/23 23:28:54

学习来源:日撸 Java 三百行(21-30天,树与二叉树)

第 28 天: Huffman 编码 (节点定义与文件读取)

输入:输入表示文本文件的字符串paraFilename
输出:构造对象tempHuffman并输出文本文件的内容inputText
优化目标:无

ps:每个节点的内容包括:字符 (仅对叶节点有效)、权重 (表示该字符的个数)、指向子节点和父节点的引用。

package my_java;import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.stream.Collectors;/*** @Description: Huffman tree* @author: Xin-Yu Li* @date: 2022年4月19日*/
public class Huffman {class HuffmanNode {char character;int weight;HuffmanNode leftChild;HuffmanNode rightChild;HuffmanNode parent;public HuffmanNode(char paraCharacter, int paraWeight, HuffmanNode paraLeftChild,HuffmanNode paraRightChild, HuffmanNode paraParent) {character = paraCharacter;weight = paraWeight;leftChild = paraLeftChild;rightChild = paraRightChild;parent = paraParent;}// Of HuffmanNodepublic String toString() {String resultString = "(" + character + ", " + weight + ")";return resultString;}// Of toString}// Of class HuffmanNodepublic static final int NUM_CHARS = 256;String inputText;int alphabetLength;char[] alphabet;int[] charCounts;int[] charMapping;String[] huffmanCodes;HuffmanNode[] nodes;/*** @Description: The first constructor.* @param paraFilename The text filename.*/public Huffman(String paraFilename) {charMapping = new int[NUM_CHARS];readText(paraFilename);}// Of the first constructor/*** @Description: Read text.* @param paraFilename The text filename.*/public void readText(String paraFilename) {try {inputText = Files.newBufferedReader(Paths.get(paraFilename), StandardCharsets.UTF_8).lines().collect(Collectors.joining("\n"));} catch (Exception ee) {System.out.println(ee);System.exit(0);} // Of trySystem.out.println("The text is:\r\n" + inputText);}// Of readTextpublic static void main(String args[]) {Huffman tempHuffman = new Huffman("C:/Users/94296/Desktop/huffman.txt");}// Of main}// Of class Huffman

运行截图:
在这里插入图片描述

第 29 天: Huffman 编码 (建树)

建树就是一个自底向上,贪心选择的过程。确定子节点、父节点的代码是核心。

输入:输入文本文件的内容inputText来构建哈夫曼树
输出:输出inputText中出现过的字符、字符的权重、建树选结点的过程以及根节点。
优化目标:无

	/*** @Description: Construct the alphabet.*/public void constructAlphabet() {Arrays.fill(charMapping, -1);//这种初始化工作非常重要int[] tempCharCounts = new int[NUM_CHARS];int tempCharIndex;char tempChar;for (int i = 0; i < inputText.length(); i++) {tempChar = inputText.charAt(i);tempCharIndex = (int) tempChar;//将 char强制转换为 int, 即其在 ASCII字符集中的位置System.out.print("" + tempCharIndex + " ");tempCharCounts[tempCharIndex]++;} // Of for ialphabetLength = 0;for (int i = 0; i < 255; i++) {if (tempCharCounts[i] > 0) {alphabetLength++;} // Of if} // Of for i//Compress to the alphabetalphabet = new char[alphabetLength];charCounts = new int[2 * alphabetLength - 1];int tempCounter = 0;for (int i = 0; i < NUM_CHARS; i++) {if (tempCharCounts[i] > 0) {alphabet[tempCounter] = (char) i;//tempCounter位置存储的字符charCounts[tempCounter] = tempCharCounts[i];//该字符的个数charMapping[i] = tempCounter;//i表示在ASCII字符集中的位置tempCounter++;} // Of if} // Of for iSystem.out.println("The alphabet is: " + Arrays.toString(alphabet));System.out.println("Their counts are: " + Arrays.toString(charCounts));System.out.println("The char mappings are: " + Arrays.toString(charMapping));}// Of constructAlphabet/** * @Description: Construct the tree.*/public void constructTree() {nodes = new HuffmanNode[alphabetLength * 2 - 1];boolean[] tempProcessed = new boolean[alphabetLength * 2 - 1];//建立叶子结点for (int i = 0; i < alphabetLength; i++) {nodes[i] = new HuffmanNode(alphabet[i], charCounts[i], null, null, null);} // Of for iint tempLeft, tempRight, tempMinimal;for (int i = alphabetLength; i < 2 * alphabetLength - 1; i++) {//选择权值最小的作为左孩子tempLeft = -1;tempMinimal = Integer.MAX_VALUE;for (int j = 0; j < i; j++) {if (tempProcessed[j]) {continue;} // Of ifif (tempMinimal > charCounts[j]) {tempMinimal = charCounts[j];tempLeft = j;} // Of if} // Of for jtempProcessed[tempLeft] = true;//选择权值第二小的作为右孩子tempRight = -1;tempMinimal = Integer.MAX_VALUE;for (int j = 0; j < i; j++) {if (tempProcessed[j]) {continue;} // Of ifif (tempMinimal > charCounts[j]) {tempMinimal = charCounts[j];tempRight = j;} // Of if} // Of for jtempProcessed[tempRight] = true;System.out.println("Selecting " + tempLeft + " and " + tempRight);//建立链接两个孩子结点的新结点charCounts[i] = charCounts[tempLeft] + charCounts[tempRight];nodes[i] = new HuffmanNode('*', charCounts[i], nodes[tempLeft], nodes[tempRight], null);//孩子结点指向父节点nodes[tempLeft].parent = nodes[i];nodes[tempRight].parent = nodes[i];System.out.println("The children of " + i + " are " + tempLeft + " and " + tempRight);} // Of for i}// Of constructTree/*** @Description: Get the root of the binary tree.* @return The root.*/public HuffmanNode getRoot() {return nodes[nodes.length - 1];}// Of getRootpublic static void main(String args[]) {Huffman tempHuffman = new Huffman("C:/Users/94296/Desktop/huffman.txt");tempHuffman.constructAlphabet();tempHuffman.constructTree();HuffmanNode tempRoot = tempHuffman.getRoot();System.out.println("The root is: " + tempRoot);}// Of main

运行截图:
在这里插入图片描述

第 30 天: Huffman 编码 (编码与解码)

编码是从叶节点到根节点,解码就是反过来。

输入:输入文本文件的内容inputText来构建哈夫曼树以及输入待编码字符串“hello”
输出:输出哈夫曼树的先序遍历序列、每个字符的编码以及待编码字符串的编码和解码过程
优化目标:无

	/*** @Description: Pre-order visit.* @param paraNode The Root*/public void preOrderVisit(HuffmanNode paraNode) {System.out.print("(" + paraNode.character + ", " + paraNode.weight + ") ");if (paraNode.leftChild != null) {preOrderVisit(paraNode.leftChild);} // Of ifif (paraNode.rightChild != null) {preOrderVisit(paraNode.rightChild);} // Of if}// Of preOrderVisit/*** @Description: Generate codes for each character in the alphabet.* @param none*/public void generateCodes() {huffmanCodes = new String[alphabetLength];HuffmanNode tempNode;for (int i = 0; i < alphabetLength; i++) {tempNode = nodes[i];String tempCharCode = "";while (tempNode.parent != null) {if (tempNode == tempNode.parent.leftChild) {tempCharCode = "0" + tempCharCode;} else {tempCharCode = "1" + tempCharCode;} // Of iftempNode = tempNode.parent;} // Of whilehuffmanCodes[i] = tempCharCode;System.out.println("The code of " + alphabet[i] + " is " + tempCharCode);} // Of for i}// Of generateCodes/*** @Description: Encode the given string.* @param paraString The given string.*/public String coding(String paraString) {String resultCodeString = "";int tempIndex;for (int i = 0; i < paraString.length(); i++) {tempIndex = charMapping[(int) paraString.charAt(i)];resultCodeString += huffmanCodes[tempIndex];} // Of for ireturn resultCodeString;}// Of coding/*** @Description: Decode the given string.* @param paraString The given string.*/public String decoding(String paraString) {String resultCodeString = "";HuffmanNode tempNode = getRoot();for (int i = 0; i < paraString.length(); i++) {if (paraString.charAt(i) == '0') {tempNode = tempNode.leftChild;System.out.println(tempNode);} else {tempNode = tempNode.rightChild;System.out.println(tempNode);} // Of ifif (tempNode.leftChild == null) {System.out.println("Decode one:" + tempNode);resultCodeString += tempNode.character;tempNode = getRoot();} // Of if} // Of for ireturn resultCodeString;}// Of decodingpublic static void main(String args[]) {Huffman tempHuffman = new Huffman("C:/Users/94296/Desktop/huffman.txt");tempHuffman.constructAlphabet();tempHuffman.constructTree();HuffmanNode tempRoot = tempHuffman.getRoot();System.out.println("The root is: " + tempRoot);tempHuffman.preOrderVisit(tempHuffman.getRoot());tempHuffman.generateCodes();//前序遍历代码的作用仅仅是调拭String tempCoded = tempHuffman.coding("abcdb");System.out.println("Coded: " + tempCoded);String tempDecoded = tempHuffman.decoding(tempCoded);System.out.println("Decoded: " + tempDecoded);}// Of main

运行截图:
在这里插入图片描述


http://chatgpt.dhexx.cn/article/AqakjSHq.shtml

相关文章

Java-1110

https://github.com/Lannister-never-pay/JavaWebLearning/tree/main/java1108 因为懒&#xff0c;还是用的1108的module JSP 指令 作用&#xff1a;用于配置JSP页面&#xff0c;导入资源文件 格式&#xff1a;<% 指令名称 属性名1属性值1 属性名2属性值2 %> 多个键值…

Java——详解Integer128陷阱

今天我们来一起探讨一下Java的128陷阱 首先我们通过代码对128陷阱进行一个认知 public static void main(String[] args){Integer a 127 ;Integer b 127 ;Integer c 128 ;Integer d 128 ;Integer e 1000 ;Integer f 1000 ;int a1 127;int b1 127;int c1 128;int d1 …

Java-11

学习来源&#xff1a;日撸 Java 三百行&#xff08;31-40天&#xff0c;图&#xff09;_闵帆的博客-CSDN博客 36 邻接表 36.1 相当于图的压缩存储. 每一行数据用一个单链表存储。 36.2 重写了广度优先遍历. 可以发现, 使用队列的机制不变. 仅仅是把其中的 for 循环换成了 wh…

JAVA101-135

JAVA101-150 字符串StringBuilder链式编程简化代码对应的关系可以使用查表法&#xff0c;通过数组的对应的下表来改变成相应的值 修改字符串字符串变整数重点&#xff1a;字符串变为数组 ArrayList集合的基本使用集合一开始的长度为0&#xff0c;如果用循环&#xff0c;进不去 …

Java-1214

Spring5总体学习内容 Spring基本概念IOC容器AopJdbcTemplate事务管理Spring5新特性 框架概述 Spring是轻量级的开源的JavaEE框架Spring可以解决企业应用开发的复杂性Spring有两个核心部分&#xff1a;IOC、Aop IOC&#xff1a;控制反转&#xff0c;把创建对象的过程交给Spri…

下载Google Play外国区APP技巧

安卓用户若遇到喜欢的APP是外国区的&#xff0c;只要翻墙就能下载。比起果粉还要注册&#xff0c;是简便很多。但有没有更简单的办法&#xff1f;这个必须有&#xff01;笔者前几天在网上闲逛时&#xff0c;就发现了一个给力的网站。让你不用翻墙&#xff0c;只需3个步骤&#…

Google Play国内应用市场发布版本步骤指导

应用发布步骤指导 前言Google Play华为小米Vivooppo 博客创建时间&#xff1a;2022.08.19 博客更新时间&#xff1a;2022.08.22 以Android studio build7.0.0&#xff0c;SDKVersion 31来分析讲解。如图文和网上其他资料不一致&#xff0c;可能是别的资料版本较低而已。 前言 …

Google Play App Signing的问题以及解决方式

Google Play App Signing是Google Play 的应用签名&#xff0c;在Google Play上创建项目的时候如果勾选了它&#xff0c;那么它就会生成一个签名文件&#xff0c;不管你上传到Google Play的apk是否用你的签名文件打包&#xff0c;最终都会被替换成Google Play App Signing里的签…

如何将Flutter开发的Android app 发布Google Play(谷歌应用商店)流程

将Flutter Android app 发布Google Play&#xff08;谷歌应用商店&#xff09;流程 一、首先就是要做到科学&#xff01; 二、打开google play官网&#xff0c;注册谷歌账号 三、打开谷歌开发者站点https://play.google.com/apps/publish/signup/创建你的App应用 四、创建完…

h5/uni-app打开手机app,没有则跳转到商店下载

需求&#xff1a;在做商品分享/直播分享时&#xff0c;app内分享出去的链接&#xff0c;能够在微信、手机浏览器打开。 遇到的问题&#xff1a; 1&#xff0c;Android&#xff0c;当手机没有下载app时&#xff0c;在浏览器打开&#xff0c;会下载app&#xff0c;但是手机下载了…

最新版Google Pay上传App指南

现在2022年&#xff0c;是时候来个最新版的操作指南 创建应用 使用谷歌市场开发者账号登录 开发者平台。成功登录后&#xff0c;单击 创建应用。 填写应用的 应用名称。选择应用的 默认语言。在应用或游戏处&#xff0c;选择 应用。根据个人情况选择免费或付费。 勾选 开发者…

网页下载Google Play 的App

网页下载Google Play 的App 文章目录[点击展开](?)[] 前言 当你想在google play上下载某个应用&#xff0c;而无奈手机的系统并没有安装google servicess&#xff0c;此刻是否有些捉急&#xff1f; 本文分享的是一个网站&#xff0c;它可以无需手机而直接通过网页下载Google P…

【Google Play】App Bundle 使用详解 ( 简介 | 应用内更新 | 即时更新 | 灵活更新 )

Google Play 上架完整流程 系列文章目录 【Google Play】创建 Google 开发者账号 ( 注册邮箱账号 | 创建开发者账号 ) 【Google Play】创建并设置应用 ( 访问权限 | 内容分级 | 受众群体 | 类别及联系方式 | 商品详情 ) 【Google Play】App Bundle 使用详解 ( 简介 | 应用内更…

Google Play上架App设置隐私政策声明问题

APP上架Google Play一定要设置隐私政策声明,否则是不给上架的 隐私政策解决方法,生成隐私内容&#xff1a; 点击网址进入 App Privacy Policy Generator 之后根据app的名称&#xff0c;类型&#xff0c;平台&#xff0c;选择对应的选项&#xff0c; 包含对应的第三方隐私服务…

WhatsApp的下载与更新

这两天登录手机&#xff08;安卓&#xff09;的WhatsApp&#xff0c;一直显示我的WhatsApp即将几天后更新&#xff0c;请及时更新到最新的版本&#xff0c;尝试了网上的多种方法&#xff0c;还是没有成功&#xff0c;当然不排除我笨的因素&#xff0c;后来我的小脑瓜子那么一转…

ubuntu安装google app engine环境

需要goog app engine的运行环境&#xff0c;结果翻找半天找不到怎么安装&#xff0c;做记录&#xff1a; 下载app engine &#xff0c; 地址如下&#xff1a; https://cloud.google.com/appengine/downloads?hlzh-TW 到这个网页&#xff0c;找不到下载地址&#xff0c;但却…

google play app下载方法测试

部分参考&#xff1a;http://www.zhihu.com/question/20232626 因为需求&#xff0c;需要从Google play上下载一个APP&#xff1a;Ticketmaster 寻找了一些方法&#xff1a; 基本要求&#xff1a;需要翻墙。 方法1&#xff1a;http://apk-dl.com/ &#xff08;不用翻墙&…

在电脑端下载google play上的app,将其下载成apk

想要下载googleplay上的app&#xff0c;但是没有直接的下载链接。这里推荐一个chrome浏览器上的插件&#xff1a;APK Downloader。 插件安装完成后&#xff0c;在google play搜索到需要下载的app后&#xff0c;将其网页URL复制到插件上生成下载链接即可。

直接下载Google Play上APP的安装包

1、先在GooglePlay上找到自己需要下载的Package Name或者软件的地址链接. 下图是APP提取网站的示意图&#xff1a; 2、打开Online APK Downloader(点击进入)&#xff0c;在输入框中粘贴刚才复制的Package Name或地址。点击“Generate DownIoad Link”。如果输入地址提示错误&a…