/* Function: getFrequencyTable * Usage: MyMap<ext_char, int> freq = getFrequencyTable(file); * -------------------------------------------------------- * This function will also set the frequency of the EOF * character to be 1, which ensures that any future encoding * tree built from these frequencies will have an encoding for * the EOF character. */ MyMap<ext_char, int> getFrequencyTable(ibstream& infileStream) { MyMap<ext_char, int> resultFrequenciesMap; ext_char nextChar; while ((nextChar = infileStream.get()) != EOF) { modifyMap(resultFrequenciesMap, nextChar); } resultFrequenciesMap.add(EOF, 1); return resultFrequenciesMap; }
/* Function: buildCypherTable * --------------------------- * Modifies param cypher table map * by entries [symbol][symbol Huffman code] * for this huffman tree. * Makes DFS from root to every symbol-leaf to * cocantenate currentCypher code value. * * * @param currentCypher symbol * @param node root of Huffman tree * @param cypherTable cypher table map to fill by entries */ void buildCodesTableForTree(string currentSymbolCode, Node* node, MyMap<ext_char, string>& charsCodesTable) { /* This node is fork end */ if ((node->leftChild == NULL) && (node->rightChild == NULL)) { charsCodesTable.add(node->symbol, currentSymbolCode); }else{ /* This node has childs */ if (node->leftChild != NULL) { string leftCypher = currentSymbolCode + "0"; buildCodesTableForTree(leftCypher, node->leftChild, charsCodesTable); } if (node->rightChild != NULL) { string rightCypher = currentSymbolCode + "1"; buildCodesTableForTree(rightCypher, node->rightChild, charsCodesTable); } } }