package hopehuffman; import hopehuffman.Tree.Node; import java.io.FileInputStream; import java.io.IOException; public class HopeHuffMan { public static void main(String[] args) throws IOException { FileInputStream input; input = new FileInputStream( "f:/222.txt" ); String text=changeinputtostring(input); int[] counts = getCharacterFrequency(text); // Count frequency System.out.print("\n"+"ASCII Code"+ " Character"+ " Frequency"+ " Code\n"); Tree tree = getHuffmanTree(counts); // Create a Huffman tree String[] codes = getCode(tree.root); // Get codes for (int i = 0; i < codes.length; i++) if (counts[i] != 0) // (char)i is not in text if counts[i] is 0 System.out.print("\n"+i+" "+ (char)i+" " + counts[i]+" " +codes[i]); } //------------------------------------------------------------------------ private static String changeinputtostring(FileInputStream input1) throws IOException{ int ch; StringBuilder strContent = new StringBuilder(""); while((ch = input1.read()) != -1) strContent.append((char) ch); input1.close(); String text = strContent.toString(); return text; } /** Get the frequency of the characters */ private static int[] getCharacterFrequency(String text) { int[] counts = new int[256]; // 256 ASCII characters for (int i = 0; i < text.length(); i++) counts[(int)text.charAt(i)]++; // Count the character in text return counts; } //------------------------------------------------------------------------ /** Get a Huffman tree from the codes */ private static Tree getHuffmanTree(int[] counts) { // Create a heap to hold trees Heap<Tree> heap = new Heap<Tree>(); // Defined in Listing 24.10 for (int i = 0; i < counts.length; i++) { if (counts[i] > 0) heap.add(new Tree(counts[i], (char)i)); // A leaf node tree } while (heap.getSize() > 1) { Tree t1 = heap.remove(); // Remove the smallest weight tree Tree t2 = heap.remove(); // Remove the next smallest weight heap.add(new Tree(t1, t2)); // Combine two trees } return heap.remove(); // The final tree } //------------------------------------------------------------------------ /* Recursively get codes to the leaf node */ private static String[] getCode(Node root) { if (root == null) return null; String[] codes = new String[2 * 128]; assignCode(root, codes); return codes; } //------------------------------------------------------------------------ /* Recursively get codes to the leaf node */ private static void assignCode(Node root, String[] codes) { if (root.left != null) { root.left.code = root.code + "0"; assignCode(root.left, codes); root.right.code = root.code + "1"; assignCode(root.right, codes); } else { codes[(int)root.element] = root.code; } } //------------------------------------------------------------------------ } and the tree class <pre lang="java"> package hopehuffman; public class Tree implements Comparable<Tree> { Node root; // The root of the tree /** Create a tree with two subtrees */ public Tree(Tree t1, Tree t2) { root = new Node(); root.left = t1.root; root.right = t2.root; root.weight = t1.root.weight + t2.root.weight; } /** Create a tree containing a leaf node */ public Tree(int weight, char element) { root = new Node(weight, element); } /** Compare trees based on their weights */ @Override public int compareTo(Tree o) { if (root.weight < o.root.weight) // Purposely reverse the order return 1; else if (root.weight == o.root.weight) return 0; else return -1; } public class Node { char element; // Stores the character for a leaf node int weight; // weight of the subtree rooted at this node Node left; // Reference to the left subtree Node right; // Reference to the right subtree String code = ""; // The code of this node from the root /** Create an empty node */ public Node() { } /** Create a node with the specified weight and character */ public Node(int weight, char element) { this.weight = weight; this.element = element; } } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)