日撸代码300行:第28天(Huffman编码)

代码来自闵老师”日撸 Java 三百行(21-30天)“,链接:https://blog.csdn.net/minfanphd/article/details/116975721

package datastructure.tree;

import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Collector;
import java.util.stream.Collectors;

/**
 * Huffman tree, encoding, and decoding. For simplicity, only ASCII
 * characters are supported.
 * 
 * 
 * @author WX873
 *
 */

public class Huffman {
	
	
	/**
	 * ******************************************************
	 * An inner class of Huffman nodes.
	 * @author WX873
	 * ******************************************************
	 */
	class HuffmanNode{
		/**
		 * The char. Only valid for leaf nodes.
		 */
		char character;
		
		/**
		 * Weight. It can also be double.
		 */
		int weight;
		
		/**
		 * The left child.
		 */
		HuffmanNode leftChild;

		/**
		 * The right child.
		 */
		HuffmanNode rightChild;
		/**
		 * The parent. It helps constructing the huffman code of each character.
		 */
		HuffmanNode parent;
		
		/**
		 * ***************************************************
		 * The first constructor of HuffmanNode.
		 * @param paraCharacter
		 * @param paraWeight
		 * @param paraLeftChild
		 * @param paraRightChild
		 * @param paraParent
		 * ***************************************************
		 */
		public HuffmanNode(char paraCharacter, int paraWeight, HuffmanNode paraLeftChild, HuffmanNode paraRightChild, HuffmanNode paraParent) {
			// TODO Auto-generated constructor stub
			character = paraCharacter;
			weight = paraWeight;
			leftChild = paraLeftChild;
			rightChild = paraRightChild;
			parent = paraParent;
		}//The first constructor of the HuffmanNode.
		
		public String toString() {
			String resultString = "(" + character + ", " + weight + ")";
			return resultString;
		}//of toString
	}//of class HuffmanNode
	
	/**
	 * The number of characters. 256 for ASCII.
	 */
	public static final int NUM_CHARS = 256;
	
	/**
	 * The input index. It is stored in a string for simplicity.
	 */
	String inputText;
	
	/**
	 * The length of the alphabet, also the number of leaves.
	 */
	int alphabetLength;
	
	/**
	 * The alphabet
	 */
	char alphabet;
	
	/**
	 * The count of chars. The length is 2 * alphabetLength - 1 to 
	 * include non-leaf nodes.
	 */
	int[] charCounts;
	
	/**
	 * The mapping of chars to the indices in the alphabet.
	 */
	int[] charMapping;
	
	/**
	 * Codes for each char in the alphabet. It should have the same length as alphabet.
	 */
	String[] huffmanCode;
	
	/**
	 * All nodes. The last node is the root.
	 */
	HuffmanNode[] nodes;
	
	/**
	 * ******************************************************
	 * The first constructor.
	 * 
	 * @param paraFilename
	 * ******************************************************
	 */
	public Huffman(String paraFilename) {
		// TODO Auto-generated constructor stub
		charMapping = new int[NUM_CHARS];
		
		readText(paraFilename);
	}//of the first constructor
	
	/**
	 * 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) {
			// TODO: handle exception
			System.out.println(ee);
			System.exit(0);
		}//of try
		
		System.out.println("The text is:\r\n" + inputText);
	}//of readText
	
	/**
	 * ******************************************************
	 * The entrance of the program.
	 * 
	 * @param args Not used now
	 * ******************************************************
	 */
	public static void main(String args[]) {
		Huffman tempHuffman = new Huffman("E:\\Datasets\\UCIdatasets\\temp\\HuffmanTest.txt");
	}//of main

}//of Huffman
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值