void merge(int* smallest, vector<HuffmanNode*>& set) { if(set.size() > 50) exit(1); HuffmanNode* hNode = createHuffmanNode( set[smallest[0]]->percentage + set[smallest[1]]->percentage, "***", set[smallest[0]], set[smallest[1]]); removeHuffmanNodes(set[smallest[0]], set[smallest[1]], set); set.push_back(hNode); }
int huffmanWeight(Tree** heap, int n) { heapify(heap, n); while(n > 1) { Tree* huff =createHuffmanNode(); huff->left = heapDelete(heap, n--); huff->right = heapDelete(heap, n--); huff->weight = huff->left->weight + huff->right->weight; heapAdd(heap, n++, huff); } return weighting(heap[0]); }
codeWords generateOptimalHuffmanCodeWordsWithWieghts(partitionedString pStrings, bool firstCode) { partitionedString pS = distinction(pStrings); int total = pStrings.size(); vector<HuffmanNode*> set; for(int i = 0; i < pS.size(); i++) { set.push_back(createHuffmanNode(pS[i]->frequency/(total*1.0), pS[i]->partitionStr, NULL, NULL)); } while(set.size() > 1) { merge(getTwoSmallestPercentageNodeIndexes(set), set); } return codeWordsFromTree(set[0], pS.size(), firstCode); }
int main(int argc, const char* argv[]) { huffmanTree* HuffmanTree = malloc(sizeof(huffmanTree)); HuffmanTree->root = NULL; HuffmanTree->numberOfNodes = 0; huffmanNode* HuffmanNode = createHuffmanNode(); HuffmanNode->frequency = 15; HuffmanNode->symbol = 'E'; huffmanInsert(HuffmanTree, HuffmanNode); huffmanNode* HuffmanNode1 = createHuffmanNode(); HuffmanNode1->frequency = 13; HuffmanNode1->symbol = ' '; huffmanInsert(HuffmanTree, HuffmanNode1); huffmanNode* HuffmanNode2 = createHuffmanNode(); HuffmanNode2->frequency = 12; HuffmanNode2->symbol = 'I'; huffmanInsert(HuffmanTree, HuffmanNode2); huffmanNode* HuffmanNode3 = createHuffmanNode(); HuffmanNode3->frequency = 10; HuffmanNode3->symbol = 'A'; huffmanInsert(HuffmanTree, HuffmanNode3); huffmanNode* HuffmanNode4 = createHuffmanNode(); HuffmanNode4->frequency = 4; HuffmanNode4->symbol = 'T'; huffmanInsert(HuffmanTree, HuffmanNode4); huffmanNode* HuffmanNode5 = createHuffmanNode(); HuffmanNode5->frequency = 3; HuffmanNode5->symbol = 'S'; huffmanInsert(HuffmanTree, HuffmanNode5); huffmanNode* HuffmanNode6 = createHuffmanNode(); HuffmanNode6->frequency = 1; HuffmanNode6->symbol = '\n'; huffmanInsert(HuffmanTree, HuffmanNode6); showEncode(HuffmanTree, 'A'); showEncode(HuffmanTree, ' '); showEncode(HuffmanTree, 'a'); showEncode(HuffmanTree, 'm'); char* testCode = "0111011011110111110"; char* output = analyzeCodeword(HuffmanTree, testCode); printf("\nThe output for %s is %s.\n", testCode, output); /* Other than the root node's left child which we get with a path of '0' all other * nodes added produce a '0' and a '1' kind of path in the tree. So the total * number of bits is twice the number of data nodes less one. */ float numberOfBits = (2*(HuffmanTree->numberOfNodes))-1; float numberOfSymbols = HuffmanTree->numberOfNodes; float avgBitsPerSymbol = (numberOfBits/numberOfSymbols); printf("\n\nThe average no. of bits per symbol is %f\n", avgBitsPerSymbol); showEncode(HuffmanTree, 'A'); showEncode(HuffmanTree, ' '); showEncode(HuffmanTree, 'a'); showEncode(HuffmanTree, 'm'); clearHuffmanTree(HuffmanTree); return 0; }
int main() { int n; scanf("%d", &n); Tree* charset[n]; for(int i = 0; i < n; i++) { charset[i] = (Tree*)malloc(sizeof(Tree)); char str[2]; scanf("%s", str); scanf("%d", &charset[i]->weight); charset[i]->key = str[0]; charset[i]->left = NULL; charset[i]->right = NULL; } Tree* tempset[n]; for(int i = 0; i < n; i++) { tempset[i] = (Tree*)malloc(sizeof(Tree)); copy(charset[i], tempset[i]); } int rw = huffmanWeight(tempset, n); for(int i = 0; i < n; i++) { free(tempset[i]); } int m; scanf("%d", &m); for(int i = 0; i < m; i++) { // for each submission Tree* huffman = createHuffmanNode(); int valid = 1; int j; for(j = 0; j < n && valid; j++) { // for each char char notcare[2]; scanf("%s", notcare); char code[8]; scanf("%s", code); Tree* temp = huffman; for(int k = 0; code[k] != '\0'; k++) { if(code[k] == '0') { if(!temp->left) { temp->left = createHuffmanNode(); } else { if(temp->left->key != '*') { valid = 0; //printf("node duplicate.\n"); break; } } temp = temp->left; } else { if(!temp->right) { temp->right = createHuffmanNode(); } else { if(temp->right->key != '*') { valid = 0; break; } } temp = temp->right; } } // end of k if(temp->left || temp->right) { valid = 0; j++; break; } copy(charset[j], temp); } // end of j if(j < n) { for(j; j < n; j++) { char notcare[2]; scanf("%s", notcare); char code[8]; scanf("%s", code); } } // read unused inputs if(valid) { // what's valid? All chars are Huffman's leaves. // traversal(huffman); int aw = weighting(huffman); if(aw == rw) { printf("Yes\n"); } else { printf("No\n"); } } else { printf("No\n"); } } // end of i return 0; }