huffman树实现的压缩算法,java
1.树的构建
- package huffman;
- public abstract class BinaryTreeBasis {
- protected TreeNode root;
- public BinaryTreeBasis() {
- root = null;
- } // end default constructor
- public BinaryTreeBasis(Object rootItem) {
- root = new TreeNode(rootItem, null, null);
- } // end constructor
- public boolean isEmpty() {
- // Returns true if the tree is empty, else returns false.
- return root == null;
- } // end isEmpty
- public void makeEmpty() {
- // Removes all nodes from the tree.
- root = null;
- } // end makeEmpty
- public Object getRootItem() throws TreeException {
- // Returns the item in the trees root.
- if (root == null) {
- throw new TreeException("TreeException: Empty tree");
- }
- else {
- return root.getItem();
- } // end if
- } // end getRootItem
- public TreeNode getRoot() throws TreeException {
- // additional method to return the root as TreeNode.
- return root;
- } // end getRoot
- } // end BinaryTreeBasis
- package huffman;
- public class BinaryTree extends BinaryTreeBasis {
- public BinaryTree() {
- } // end default constructor
- public BinaryTree(Object rootItem) {
- super(rootItem);
- } // end constructor
- public BinaryTree(Object rootItem,
- BinaryTree leftTree,
- BinaryTree rightTree) {
- root = new TreeNode(rootItem, null, null);
- attachLeftSubtree(leftTree);
- attachRightSubtree(rightTree);
- } // end constructor
- public void setRootItem(Object newItem) {
- if (root != null) {
- root.setItem(newItem);
- }
- else {
- root = new TreeNode(newItem, null, null);
- } // end if
- } // end setRootItem
- public void attachLeft(Object newItem) {
- if (!isEmpty() && root.getLeft() == null) {
- // assertion: nonempty tree; no left child
- root.setLeft(new TreeNode(newItem, null, null));
- } // end if
- } // end attachLeft
- public void attachRight(Object newItem) {
- if (!isEmpty() && root.getRight() == null) {
- // assertion: nonempty tree; no right child
- root.setRight(new TreeNode(newItem, null, null));
- } // end if
- } // end attachRight
- public void attachLeftSubtree(BinaryTree leftTree)
- throws TreeException {
- if (isEmpty()) {
- throw new TreeException("TreeException: Empty tree");
- }
- else if (root.getLeft() != null) {
- // a left subtree already exists; it should have been
- // deleted first
- throw new TreeException("TreeException: " +
- "Cannot overwrite left subtree");
- }
- else {
- // assertion: nonempty tree; no left child
- root.setLeft(leftTree.root);
- // don't want to leave multiple entry points into
- // our tree
- leftTree.makeEmpty();
- } // end if
- } // end attachLeftSubtree
- public void attachRightSubtree(BinaryTree rightTree)
- throws TreeException {
- if (isEmpty()) {
- throw new TreeException("TreeException: Empty tree");
- }
- else if (root.getRight() != null) {
- // a right subtree already exists; it should have been
- // deleted first
- throw new TreeException("TreeException: " +
- "Cannot overwrite right subtree");
- }
- else {
- // assertion: nonempty tree; no right child
- root.setRight(rightTree.root);
- // don't want to leave multiple entry points into
- // our tree
- rightTree.makeEmpty();
- } // end if
- } // end attachRightSubtree
- protected BinaryTree(TreeNode rootNode) {
- root = rootNode;
- } // end protected constructor
- public BinaryTree detachLeftSubtree()
- throws TreeException {
- if (isEmpty()) {
- throw new TreeException("TreeException: Empty tree");
- }
- else {
- // create a new binary tree that has root's left
- // node as its root
- BinaryTree leftTree;
- leftTree = new BinaryTree(root.getLeft());
- root.setLeft(null);
- return leftTree;
- } // end if
- } // end detachLeftSubtree
- public BinaryTree detachRightSubtree()
- throws TreeException {
- if (isEmpty()) {
- throw new TreeException("TreeException: Empty tree");
- }
- else {
- BinaryTree rightTree;
- rightTree = new BinaryTree(root.getRight());
- root.setRight(null);
- return rightTree;
- } // end if
- } // end detachRightSubtree
- } // end BinaryTree
- package huffman;
- public class TreeNode {
- private Object item;
- private TreeNode leftChild;
- private TreeNode rightChild;
- public TreeNode(Object newItem) {
- // Initializes tree node with item and no children.
- item = newItem;
- leftChild = null;
- rightChild = null;
- } // end constructor
- public TreeNode(Object newItem,
- TreeNode left, TreeNode right) {
- // Initializes tree node with item and
- // the left and right children references.
- item = newItem;
- leftChild = left;
- rightChild = right;
- } // end constructor
- public Object getItem() {
- // Returns the item field.
- return item;
- } // end getItem
- public void setItem(Object newItem) {
- // Sets the item field to the new value newItem.
- item = newItem;
- } // end setItem
- public TreeNode getLeft() {
- // Returns the reference to the left child.
- return leftChild;
- } // end getLeft
- public void setLeft(TreeNode left) {
- // Sets the left child reference to left.
- leftChild = left;
- } // end setLeft
- public TreeNode getRight() {
- // Returns the reference to the right child.
- return rightChild;
- } // end getRight
- public void setRight(TreeNode right) {
- // Sets the right child reference to right.
- rightChild = right;
- } // end setRight
- public String toString() {
- return "" + item;
- }
- public boolean isLeaf() {
- return ((getLeft() == null) && (getRight() == null));
- }
- } // end TreeNode
2.逐比特读写的方法
- package huffman;
- import java.io.*;
- /**
- * This class is designed to read in bits from a file that is stored in the same format
- * as specified by the BitWriter
- * It is based on the iterator model, with similar names. It has different return types though.
- * @author Shane Paul
- * 7/10/2003
- * @
- */
- public class BitReader {
- //The inputStream
- FileInputStream input;
- //Stores the next 3 bytes in the file, to check for EOF and deal with the last byte being potentially half full
- int current, plus1, plus2;
- //The number of bits that have been read so far in the current byte
- int count;
- //Have we reached the end of the file? If so, count must be ==0 also before we are finally done
- boolean finished;
- /**
- * Construct a new BitReader from a file.
- */
- public BitReader(String file) throws Exception{
- this(new File(file));
- }
- public BitReader(File file) throws Exception{
- file.createNewFile();
- if(!file.canRead())
- throw new IOException("Oh My!! This file cannot be read from.....whatever shall I do? \nfile: "+file);
- input = new FileInputStream(file);
- //Read in the first two bytes. This assumes that the list contains at least one bit and therefore two bytes...
- plus1 = input.read();
- plus2 = input.read();
- }
- /**
- * Returns the next available bit in the file
- * @return the next boolean in the file
- */
- public boolean next()throws Exception{
- if(!hasNext())
- throw new NoBitsLeftToReturn();
- //Get the next bit
- if(count==0){ //We have no more bits in this particular byte
- //Get the next byte from the file
- current=plus1;
- plus1=plus2;
- plus2 = input.read(); //-1 is returned if we reach the EOF
- count=8;
- if(plus2<0){
- finished = true;
- count = 8-plus1; //only need to read the leftover bits
- }
- }
- count--;
- //get the leftmost bit and shift to right most
- int bit = current&0x80;
- bit = bit>>7;
- //shift current to left for one bit
- current = current<<1;
- if(bit==0)
- return false;
- return true;
- }
- /**
- * returns whether or not there are any bits left to read.
- */
- public boolean hasNext(){
- return !(finished && count==0);
- }
- /**
- * Thiis an informative exception that is thrown when you forget to check hasNext()
- * @author Shane Paul
- * 7/10/2003
- * @
- */
- public class NoBitsLeftToReturn extends Exception{}
- /**
- * Testing
- * @param args
- */
- public static void main(String[] args) throws Exception{
- BitReader br = new BitReader("writetest.txt");
- while(br.hasNext()){
- if(br.next())
- System.out.println("1");
- else
- System.out.println("0");
- }
- }
- }
- package huffman;
- import java.io.*;
- /**
- * A simple class that allows a user to write a stream of bits to a file.
- * You should make sure you .flush() the BitWriter when you have finished writing.
- * Otherwise there will be bits and pieces left in the writer. :)
- * Once you HAVE flushed this writer, you can no longer write bits to it.
- * This is because it would make the algorithm a little more difficult and I am lazy. (it is
- * also not needed)
- *
- * For those that are interested:
- * The bots are stored in sections of bytes. The last byte in the file stores
- * the number of bits in the last byte that were filled in to make up a whole byte.
- *
- *
- * @author Shane Paul
- * 7/10/2003
- * @
- */
- public class BitWriter {
- //For writing to a file
- FileOutputStream output;
- //Stores 8 bits, before writing them to the FileWriter
- int buffer;
- //Count the number of bits
- int count;
- //Whether or not this Writer has been closed yet
- boolean closed = false;
- /**
- * Constuct a BitWriter using a String filename. The file will be overwritten.
- * @param filename The name of the file.
- * @exception IOException is thrown if the file does not exist or is not writable
- */
- public BitWriter(String filename) throws IOException{
- this(new File(filename)); //uses "this" to call the file constructor
- }
- /**
- * Construct a BitWriter using a File.
- * @param file The name of the file.
- * @exception IOException is thrown if the file does not exist or is not writable
- */
- public BitWriter(File file) throws IOException{
- //Test to see file is actually a file and can have data written to it
- file.createNewFile();
- if(!file.canWrite())
- throw new IOException("Oh My!! This file cannot be written to.....whatever shall I do? \nfile: "+file);
- output = new FileOutputStream(file);
- }
- /**
- * Writes a single bit to a file. The bit is defined by the boolean it receives
- * @param bit TRUE = 1, FALSE = 0
- * @exception IOException Throws an IOException if there were any problems writing to this file
- */
- public void writeBit(boolean bit)throws Exception{
- //Add in the specified bit by using the other method
- if(bit)
- writeBit(1);
- else
- writeBit(0);
- }
- /**
- * Flushes the buffer and closes this writer for good. The writer will accept no further bits after this method
- * has been called. You cannot "flush" this buffer because you cannot write single bits to the file.
- * Once you have "flushed" this buffer, it is then closed for good. (see class descrip. for why)
- */
- public void close()throws Exception{
- if(closed)
- throw new BitWriterClosedAlreadyException();
- output.flush();
- if(count!=0){
- int leftOverBits = 8-count;
- buffer = buffer<<leftOverBits;
- output.write(buffer);
- output.write(leftOverBits);
- }
- else
- output.write(0); //no extra bits
- output.close();
- closed = true;
- }
- /**
- * Another method that writes a single bit to a file.
- * This time it takes in an integer (or you could just pass a byte and it will be upcast)
- * only values 0 and 1 are acceptable.
- * <b>You need not do anything special with these exceptions, they are simply to inform you of the type
- * of error you have so you can debuig easier.</b>
- * If you try passing it another value, it will throw an exception. (to help debugging)
- * @param bit
- * @exception IOException Throws an IOException if there were any problems writing to this file
- * @exception InvalidBitException An exception I made up to indicate that you are not using this method correctly..
- */
- public void writeBit(int bit)throws Exception{
- //can't write to a closed bitwriter
- if(closed)
- throw new BitWriterClosedAlreadyException();
- if(bit <0 || bit>1)
- throw new InvalidBitException();
- count++;
- buffer=(buffer<<1);
- //Add in the specified bit
- if(bit==1)
- buffer|=1;
- //empty the buffer and reset the count
- if(count==8){
- output.write(buffer);
- count = 0;buffer = 0;
- }
- }
- /**
- * This is a simple Exception class that is thrown when you attempt to incorrectly call the writeBit method.
- * You do not need to do anything with this exception, they are just to make debugging easier
- * @author Shane Paul
- * 7/10/2003
- * @
- */
- public class InvalidBitException extends Exception{
- }
- /**
- * This is an exception to inform you that you are trying to write to a bitwriter that has already been closed.
- * @author Shane Paul
- * 7/10/2003
- * @
- */
- public class BitWriterClosedAlreadyException extends Exception{
- }
- /**
- * Testing...and an example of how to use this class.
- * @param args
- */
- public static void main(String[] args) throws Exception{
- BitWriter bw = new BitWriter("writetest.txt");
- for(int j=0;j<12;j++){
- int num=(int)(Math.random()*30);
- if (num % 2 == 0) {
- System.out.println("0");
- bw.writeBit(0);
- }
- else {
- System.out.println("1");
- bw.writeBit(1);
- }
- }
- bw.close();
- }
- }
3.CharFreq,字母以及出现频率的数据结构
- package huffman;
- // This class represents an object which stores a single character and an integer frequency
- public class CharFreq implements Comparable {
- private char c;
- private int freq;
- public CharFreq(char c, int freq) {
- this.c = c;
- this.freq = freq;
- }
- public char getChar() {
- return c;
- }
- public int getFreq() {
- return freq;
- }
- public int compareTo(Object o) {
- return freq - ((CharFreq)o).freq;
- }
- public String toString() {
- return "(" + c + ":" + freq + ")";
- }
- }
4.TreeException
- package huffman;
- public class TreeException extends RuntimeException {
- public TreeException(String s) {
- super(s);
- } // end constructor
- } // end TreeException
5.压缩与解压缩的测试代码
- package huffman;
- import java.io.*;
- import java.lang.reflect.Array;
- import java.util.*;
- public class TextZip {
- private static final String ID = "201692388";
- /**
- * This method generates the huffman tree for the text: "abracadabra!"
- *
- * @return the root of the huffman tree
- */
- public static TreeNode abracadbraTree() {
- TreeNode n0 = new TreeNode(new CharFreq('!', 1));
- TreeNode n1 = new TreeNode(new CharFreq('c', 1));
- TreeNode n2 = new TreeNode(new CharFreq('\u0000', 2), n0, n1);
- TreeNode n3 = new TreeNode(new CharFreq('r', 2));
- TreeNode n4 = new TreeNode(new CharFreq('\u0000', 4), n3, n2);
- TreeNode n5 = new TreeNode(new CharFreq('d', 1));
- TreeNode n6 = new TreeNode(new CharFreq('b', 2));
- TreeNode n7 = new TreeNode(new CharFreq('\u0000', 3), n5, n6);
- TreeNode n8 = new TreeNode(new CharFreq('\u0000', 7), n7, n4);
- TreeNode n9 = new TreeNode(new CharFreq('a', 5));
- TreeNode n10 = new TreeNode(new CharFreq('\u0000', 12), n9, n8);
- return n10;
- }
- /**
- * This method decompresses a huffman compressed text file. The compressed
- * file must be read one bit at a time using the supplied BitReader, and
- * then by traversing the supplied huffman tree, each sequence of compressed
- * bits should be converted to their corresponding characters. The
- * decompressed characters should be written to the FileWriter
- *
- * @param br the BitReader which reads one bit at a time from the
- * compressed file
- * huffman the huffman tree that was used for compression, and
- * hence should be used for decompression
- * fw a FileWriter for storing the decompressed text file
- */
- public static void decompress(BitReader br, TreeNode huffman, FileWriter fw) throws Exception {
- // IMPLEMENT THIS METHOD
- TreeNode scaner = huffman;
- while(br.hasNext()){
- for(;!scaner.isLeaf() && br.hasNext();){
- if(br.next())
- scaner = scaner.getRight();
- else
- scaner = scaner.getLeft();
- }
- CharFreq item = (CharFreq)(scaner.getItem());
- fw.write(item.getChar());
- scaner = huffman;
- }
- }
- /**
- * This method traverses the supplied huffman tree and prints out the
- * codes associated with each character
- *
- * @param t the root of the huffman tree to be traversed
- * code a String used to build the code for each character as
- * the tree is traversed recursively
- */
- static Map<Character,String> table = new Hashtable<>();
- public static void traverse(TreeNode t, String code) {
- // IMPLEMENT THIS METHOD
- if(!t.isLeaf()){
- traverse(t.getLeft(), code + '0');
- traverse(t.getRight(),code + '1');
- }
- else {
- CharFreq item = (CharFreq) (t.getItem());
- table.put(item.getChar(), code);
- System.out.println(item.getChar() +": "+ code);
- }
- }
- // public static Map<Character, String> traverse(TreeNode t) {
- //
- // Map<Character,String> table = new Hashtable<>();
- //
- // // IMPLEMENT THIS METHOD
- // if(!t.isLeaf()){
- // traverse(t.getLeft());
- // traverse(t.getRight());
- // }
- // else {
- // CharFreq item = (CharFreq) (t.getItem());
- // System.out.println(item.getChar()+": " + "code");
- // }
- // }
- /**
- * This method removes the TreeNode, from an ArrayList of TreeNodes, which
- * contains the smallest item. The items stored in each TreeNode must
- * implement the Comparable interface.
- * The ArrayList must contain at least one element.
- *
- * @param a an ArrayList containing TreeNode objects
- *
- * @return the TreeNode in the ArrayList which contains the smallest item.
- * This TreeNode is removed from the ArrayList.
- */
- public static TreeNode removeMin(ArrayList a) {
- int minIndex = 0;
- for (int i = 0; i < a.size(); i++) {
- TreeNode ti = (TreeNode)(a.get(i));
- TreeNode tmin = (TreeNode)(a.get(minIndex));
- if (((Comparable)(ti.getItem())).compareTo(tmin.getItem()) < 0)
- minIndex = i;
- }
- TreeNode n = (TreeNode)a.remove(minIndex);
- return n;
- }
- /**
- * This method counts the frequencies of each character in the supplied
- * FileReader, and produces an output text file which lists (on each line)
- * each character followed by the frequency count of that character. This
- * method also returns an ArrayList which contains TreeNodes. The item stored
- * in each TreeNode in the returned ArrayList is a CharFreq object, which
- * stores a character and its corresponding frequency
- *
- * @param fr the FileReader for which the character frequencies are being
- * counted
- * pw the PrintWriter which is used to produce the output text file
- * listing the character frequencies
- *
- * @return the ArrayList containing TreeNodes. The item stored in each
- * TreeNode is a CharFreq object.
- */
- public static ArrayList countFrequencies(FileReader fr, PrintWriter pw) throws Exception {
- // IMPLEMENT THIS METHOD
- int c;
- ArrayList<TreeNode> list = new ArrayList<>();
- while ((c = fr.read()) != -1) {
- if(list.isEmpty()) {
- list.add(new TreeNode(new CharFreq((char) c, 1)));
- continue;
- }
- int i;
- for( i=0; i<list.size(); i++){
- TreeNode temp = list.get(i);
- CharFreq item = (CharFreq) (temp.getItem());
- if((char)c == item.getChar())
- list.set(i, new TreeNode(new CharFreq((char)c, item.getFreq() + 1)));
- }
- if(i >=list.size())
- list.add(new TreeNode(new CharFreq((char)c, 1)));
- }
- for(int i = 0; i < list.size(); i++){
- CharFreq temp = (CharFreq)(list.get(i).getItem());
- pw.print(temp);
- }
- return list;
- }
- /**
- * This method builds a huffman tree from the supplied ArrayList of TreeNodes.
- * Initially, the items in each TreeNode in the ArrayList store a CharFreq object.
- * As the tree is built, the smallest two items in the ArrayList are removed,
- * merged to form a tree with a CharFreq object storing the sum of the frequencies
- * as the root, and the two original CharFreq objects as the children. The right
- * child must be the second of the two elements removed from the ArrayList (where
- * the ArrayList is scanned from left to right when the minimum element is found).
- * When the ArrayList contains just one element, this will be the root of the
- * completed huffman tree.
- *
- * @param trees the ArrayList containing the TreeNodes used in the algorithm
- * for generating the huffman tree
- *
- * @return the TreeNode referring to the root of the completed huffman tree
- */
- public static TreeNode buildTree(ArrayList trees) throws IOException {
- // IMPLEMENT THIS METHOD
- while(trees.size() > 1){
- TreeNode rleft = removeMin(trees);
- TreeNode rright = removeMin(trees);
- CharFreq item = new CharFreq('\u0000',((CharFreq)rleft.getItem()).getFreq() +
- ((CharFreq)rright.getItem()).getFreq() );
- TreeNode root = new TreeNode( item, rleft, rright);
- trees.add( root );
- }
- return (TreeNode)trees.get(0);
- }
- /**
- * This method compresses a text file using huffman encoding. Initially, the
- * supplied huffman tree is traversed to generate a lookup table of codes for
- * each character. The text file is then read one character at a time, and
- * each character is encoded by using the lookup table. The encoded bits for
- * each character are written one at a time to the specified BitWriter.
- *
- * @param fr the FileReader which contains the text file to be encoded
- * huffman the huffman tree that was used for compression, and
- * hence should be used for decompression
- * bw the BitWriter used to write the compressed bits to file
- */
- public static void compress(FileReader fr, TreeNode huffman, BitWriter bw) throws Exception {
- // IMPLEMENT THIS METHOD
- traverse(huffman,"");
- int c;
- while((c = fr.read()) != -1){
- char c1 = (char) c;
- String code = table.get(c);
- char[] arrayCodes = code.toCharArray();
- for(int i = 0; i < arrayCodes.length; i++){
- if(arrayCodes[i] == '0')
- bw.writeBit(false);
- if(arrayCodes[i] == '1')
- bw.writeBit(true);
- }
- }
- }
- /**
- * This method reads a frequency file (such as those generated by the
- * countFrequencies() method) and initialises an ArrayList of TreeNodes
- * where the item of each TreeNode is a CharFreq object storing a character
- * from the frequency file and its corresponding frequency. This method provides
- * the same functionality as the countFrequencies() method, but takes in a
- * frequency file as parameter rather than a text file.
- *
- * @param inputFreqFile the frequency file which stores characters and their
- * frequency (one character per line)
- *
- * @return the ArrayList containing TreeNodes. The item stored in each
- * TreeNode is a CharFreq object.
- */
- public static ArrayList readFrequencies(String inputFreqFile) throws Exception {
- // IMPLEMENT THIS METHOD
- FileInputStream fis = new FileInputStream(inputFreqFile);
- InputStreamReader isr = new InputStreamReader(fis,"utf-8");
- BufferedReader br = new BufferedReader(isr);
- ArrayList<TreeNode> list = new ArrayList<>();
- String s;
- while((s = br.readLine()) != null){
- String[] ss = s.split(" ");
- TreeNode t = new TreeNode(new CharFreq(ss[0].charAt(0), Integer.parseInt(ss[1])));
- list.add(t);
- }
- return list;
- }
- /* This TextZip application should support the following command line flags:
- QUESTION 2 PART 1
- =================
- -a : this uses a default prefix code tree and its compressed
- file, "a.txz", and decompresses the file, storing the output
- in the text file, "a.txt". It should also print out the size
- of the compressed file (in bytes), the size of the decompressed
- file (in bytes) and the compression ratio
- QUESTION 2 PART 2
- =================
- -f : given a text file (args[1]) and the name of an output frequency file
- (args[2]) this should count the character frequencies in the text file
- and store these in the frequency file (with one character and its
- frequency per line). It should then build the huffman tree based on
- the character frequencies, and then print out the prefix code for each
- character
- QUESTION 2 PART 3
- =================
- -c : given a text file (args[1]) and the name of an output frequency file
- (args[2]) and the name of the output compressed file (args[3]), this
- should compress file
- QUESTION 2 PART 4
- =================
- -d : given a compressed file (args[1]) and its corresponding frequency file
- (args[2]) and the name of the output decompressed text file (args[3]),
- this should decompress the file
- */
- public static void main(String[] args) throws Exception {
- if (args[0].equals("-a")) {
- BitReader br = new BitReader("a.txz");
- FileWriter fw = new FileWriter("a.txt");
- // Get the default prefix code tree
- TreeNode tn = abracadbraTree();
- // Decompress the default file "a.txz"
- decompress(br, tn, fw);
- // Close the ouput file
- fw.close();
- // Output the compression ratio
- // Write your own implementation here.
- }
- else if (args[0].equals("-f")) {
- FileReader fr = new FileReader(args[1]);
- PrintWriter pw = new PrintWriter(new FileWriter(args[2]));
- // Calculate the frequencies
- ArrayList trees = countFrequencies(fr, pw);
- // Close the files
- fr.close();
- pw.close();
- // Build the huffman tree
- TreeNode n = buildTree(trees);
- // Display the codes
- traverse(n, "");
- }
- else if (args[0].equals("-c")) {
- FileReader fr = new FileReader(args[1]);
- PrintWriter pw = new PrintWriter(new FileWriter(args[2]));
- ArrayList trees = countFrequencies(fr, pw);
- fr.close();
- pw.close();
- TreeNode n = buildTree(trees);
- // IMPLEMENT NEXT
- // Finish the compress function here
- // then output the compression ratio
- // Write your own implementation here.
- }
- else if (args[0].equals("-d")) {
- ArrayList a = readFrequencies(args[2]);
- TreeNode tn = buildTree(a);
- BitReader br = new BitReader(args[1]);
- FileWriter fw = new FileWriter(args[3]);
- decompress(br, tn, fw);
- fw.close();
- // Output the compression ratio
- // Write your own implementation here.
- }
- }
- }
huffman树实现的压缩算法,java的更多相关文章
- Java蓝桥杯练习题——Huffman树
Huffman树在编码中有着广泛的应用.在这里,我们只关心Huffman树的构造过程. 给出一列数{pi}={p0, p1, -, pn-1},用这列数构造Huffman树的过程如下: 找到{pi}中 ...
- [数据结构与算法]哈夫曼(Huffman)树与哈夫曼编码
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- Huffman树与编码的简单实现
好久没写代码了,这个是一个朋友问的要C实现,由于不会C,就用JAVA写了个简单的.注释掉的代码属性按照原来朋友发的题里带的参数,发现没什么用就给注释掉了. package other; import ...
- 数据结构(二十七)Huffman树和Huffman编码
Huffman树是一种在编码技术方面得到广泛应用的二叉树,它也是一种最优二叉树. 一.霍夫曼树的基本概念 1.结点的路径和结点的路径长度:结点间的路径是指从一个结点到另一个结点所经历的结点和分支序列. ...
- Huffman树与Huffman编码
1.Huffman树 今天复习Huffman树.依稀记得自己被Huffman树虐的经历.还记得是7月份,我刚开始看数据结构与算法,根本看不懂Huffman树的操作.后来我终于悟出了Huffman树是怎 ...
- Huffman树、霍夫曼编码
Huffman树指的是带权路径长度WPL最小的二叉树 WPL=路径*权值 Huffman常用于压缩编码,正常传输ABCDEF这些字母需要3位二进制树来描述,但由于一篇文章中ABCDEF这些字母出现的概 ...
- 构造数列Huffman树总耗费_蓝桥杯
快排! /** 问题描述 Huffman树在编码中有着广泛的应用.在这里,我们只关心Huffman树的构造过程. 给出一列数{pi}={p0, p1, …, pn-1},用这列数构造Huffman树的 ...
- NOI 2015 荷马史诗【BZOJ 4198】k叉Huffman树
抱歉因为NOIP集训,好长时间没再写题解了. NOI 2015也就只有这道题一看就能懂了-- 4198: [Noi2015]荷马史诗 Time Limit: 10 Sec Memory Limit: ...
- 【数据结构】Huffman树
参照书上写的Huffman树的代码 结构用的是线性存储的结构 不是二叉链表 里面要用到查找最小和第二小 理论上锦标赛法比较好 但是实现好麻烦啊 考虑到数据量不是很大 就直接用比较笨的先找最小 去掉最小 ...
随机推荐
- Spring Boot与Docker部署
开启Docker远程访问 首先需要开启docker远程访问功能,以便可以进行远程操作. CentOS 6 修改/etc/default/docker文件,重启后生效(service docker re ...
- 关于d3.js 将一个element 拖拽到另一个element的响应事件
rt 正在做机柜可视化, 一个需求是能拖拽左侧列表的设备名称, 拖到右侧42U机柜中,并将设备图片放置在对应机柜位置上. 开始的时候一切都很顺利,点击左侧设备名称,添加一个g容器,将设备名称作为tex ...
- 解决Oracle数据库空间不足问题
//查询表空间的大小以及文件路径地址select tablespace_name, file_id, file_name,round(bytes/(1024*1024),0) total_space ...
- SQLServer “无法对数据库'XXX' 执行删除,因为它正用于复制”的解决方法
“无法对数据库'XXX'执行删除,因为它正用于复制” 解决办法: 执行 sp_removedbreplication 'XXX' 这个语句的解释是:从数据库中删除所有复制对象,但不更新分发服务器上 ...
- SpringMVC的执行流程
解析SpringMVC执行流程 SpringMVC的执行图大概如下所示 1.首先它是由客户端发起请求,到DispatcherServlet,被拦截后到HandlerMapping这里充当一个路由器,通 ...
- 13. Roman to Integer (JAVA)
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...
- PCIE4.0 简单介绍
关于PCI-E的标准,可以从2003年说起,2003年推出了PCI-E 1.0标准,在三年之后就推出了PCI-E 2.0,而在4年后的2010年就推出了PCI-E 3.0,但是在2010年之后的6年里 ...
- OO_多线程电梯_单元总结
概述: 面向对象的第二单元是多线程电梯.第一次实现一部傻瓜电梯,每次只送一个人:第二次实现一部可稍带电梯:第三次实现三部可稍带电梯. 一.设计策略 1.第5.6次作业设计思路 第5.6次作业的架构相似 ...
- UML图之时序图
时序图(Sequence Diagram)是显示对象之间交互的图,这些对象是按时间顺序排列的.顺序图中显示的是参与交互的对象及其对象之间消息交互的顺序.时序图中包括的建模元素主要有:角色(Actor) ...
- IDEA开发环境中快捷键与系统 QQ等冲突的解决办法
1.快捷键冲突1:IDEA中,Ctrl+Alt+向左/右/箭头快捷键 (回到光标的前一个位置,回到光标的后一个位置).该快捷键与系统中旋转屏幕的快捷键冲突了,需要解决.为了保留IDEA的中快捷键,我就 ...