C++ 实现霍夫曼树生成算法
#include
struct Node { int weight; Node* left; Node* right; Node(int w) : weight(w), left(nullptr), right(nullptr) {} };
struct cmp { bool operator () (Node* a, Node* b) { return a->weight > b->weight; } };
Node* buildHuffmanTree(vector
void printHuffmanCode(Node* root, string code) { if (!root) return; if (!root->left && !root->right) { cout << 'value: ' << root->weight << ', code: ' << code << endl; return; } if (root->left) printHuffmanCode(root->left, code + '0'); if (root->right) printHuffmanCode(root->right, code + '1'); }
int main() {
vector
原文地址: https://www.cveoy.top/t/topic/ndGa 著作权归作者所有。请勿转载和采集!