Huffman, Huffman coding

// constantly updated

Huffman coding @:
    @ Huffman coding for data compression, the use of unequal length encoded according to other alternate coding weights
struct HNODE {
    unsigned int weight;
    unsigned int parent;
    unsigned int LChild;
    unsigned int RChild;
};
struct HCode {
    char Data;
    char code [100];
};
class Huffman {
Private:
    HNODE * The HTree;
    HCode * HCodeTable;
public:
    void CreateHTree (int A [], int n-);
    void CreateCodeTable (char B [] , n-int);
    void the Encode (S char *, char * D);
    void the Decode (S char *, char * D);
    ~ Huffman ();
};
void Huffman :: CreateHTree (A int [], int n-) {
    The HTree HNODE new new = [n-2 * -. 1];
    for (int i = 0; i < n; i++) {
        HTree[i].weight = a[i];
        HTree[i].LChild = -1;
        HTree[i].RChild = -1;
        HTree[i].parent = -1;
    }
    int x, y;
    for (int i = n; i < 2 * n - 1; i++) {
        SelectMin(x, y, 0, 1);
        HTree[x].parent = HTree[y].parent = i;
        HTree[i].weight = HTree[x].weight + HTree[y].weight;
        HTree[i].LChild = x;
        HTree[i].RChild = y;
        HTree[i].parent = -1;
    }
}
void Huffman::CreateCodeTable(char b[], int n) {
    HCodeTable = new HCode[n];
    for (int i = 0; i < n; i++)
    {
        HCodeTable[i].data = b[i];
        int child = i;
        int parent = HTree[i].parent;
        int k = 0;
        while (parent != -1) {
            if (child == HTree[parent].LChild) {
                HCodeTable[i].code[k] = '0';
            }
            else {
                HCodeTable[i].code[k] = '1';
            }
            k++;
            child = parent;
            parent = HTree[child].parent;
        }
        HCodeTable[i].code[k] = '\0';
        Reverse(HCodeTable[i].code);
    }
}
void Huffman::Decode(char* s, char* d) {
    while (*s != '\0') {
        int parent = 2 * n - 1 - 1;
        while (HTree[parent].LChild != -1) {
            if (*s == '0') {
                parent = HTree[parent].LChild;
            }
            else {
                parent = HTree[parent].RChild;
            }
            s++;
        }
        *d = HCodeTable[parent].data;
        d++;
    }

}

Guess you like

Origin www.cnblogs.com/cimuhuashuimu/p/12131106.html