public final category Huffman non-public Huffman() ; personal static category HuffmanNode } personal static category HuffManComparator implements Comparator<HuffmanNode> come node1.frequency - node2.frequency; } } /** * Compresses the string victimization huffman formula. * The huffman tree and also the huffman code area unit serialized to disk * * @param sentence The sentence to be serialized * @throws FileNotFoundException If file isn\'t found * @throws IOException If IO exception happens. */ public static void compress(String sentence) throws FileNotFoundException, IOException ought to atleast have one character.\"); } final Map<Character, Integer> charFreq = getCharFrequency(sentence); final HuffmanNode root = buildTree(charFreq); final Map<Character, String> charCode = generateCodes(charFreq.keySet(), root); final String encodedMessage = encodeMessage(charCode, sentence); serializeTree(root); serializeMessage(encodedMessage); } personal static Map<Character, Integer> getCharFrequency(String sentence) else } come map; } /** * Map<Character, Integer> map * Some implementation of that treeSet is passed as parameter. * @param map */ personal static HuffmanNode buildTree(Map<Character, Integer> map) whereas (nodeQueue.size() > 1) // take away it to forestall object leak. come nodeQueue.remove(); } personal static Queue<HuffmanNode> createNodeQueue(Map<Character, Integer> map) return pq; } personal static Map<Character, String> generateCodes(Set<Character> chars, HuffmanNode node) { final Map<Character, String> map = new HashMap<Character, String>(); doGenerateCode(node, map, \"\"); come map; } personal static void doGenerateCode(HuffmanNode node, Map<Character, String> map, String s) doGenerateCode(node.left, map, s + \'0\'); doGenerateCode(node.right, map, s + \'1\' ); } personal static String encodeMessage(Map<Character, String> charCode, String sentence) come stringBuilder.toString(); } personal static void serializeTree(HuffmanNode node) throws FileNotFoundException, IOException attempt (ObjectOutputStream oosTree = new ObjectOutputStream(new FileOutputStream(\"/Users/ap/Desktop/tree\"))) attempt (ObjectOutputStream oosChar = new ObjectOutputStream(new FileOutputStream(\"/Users/ap/Desktop/char\"))) cushioned to mark finish of bit set relevant for deserialization. oosTree.writeObject(bitSet); } } } personal static category IntObject /* * Algo: * 1. Access the node * 2. Register the worth in bit set. * * * here true and false dont correspond to left branch and right branch. * there, * - true means that \"a branch originates from leaf\" * - false mens \"a branch originates from non-left\". * * conjointly since branches originate from some node, the foundation node should be provided as supply * or start line of initial branches. * * Diagram and the way associate degree bit set would look as a result. * (source node) * / \\ * true true * / \\ * (leaf node) (leaf node) * | | * false false * | | * * thus currently a little set sounds like [fals.