DS --Huffman binary encoding and decoding (excluding the code frame)

Title Description

1. Description of the problem

 

Given n characters and their corresponding weights, configured Huffman tree, and huffman coding and decoding (de) code.

 

Configuration when the Huffman tree, a claim left subtree root is smaller than, equal weights of the right child of the root.

 

Performed when Huffman coding is assumed that Huffman coding the left branch of the tree is '0', the right branch is encoded '1'.

 

2, algorithm

 

Construction Huffman tree algorithm:

 

The ⑴ given n weight values configuration (W1, w2 of, ..., Wn of) n binary trees set F = {T1, T2, ... , Tn}, where each of them binary Ti with a weight of only a root of wi Node.

 

In ⑵ Select F minimum weight tree root of two, as the left, right subtree of the configuration of a new binary tree, and the root node is set to the right which the left and right subtree, and the weighted sum .

 

⑶ in deleting two trees F while the resulting binary join the new F in.

 

(4) repeating ⑵ , ⑶, until F with only up a tree.

 

3, Huffman coding algorithm:

 

⑴ from the beginning of each leaf node Huffman tree.

 

⑵ successively along the path to the root node determines the node is the father node left child or right child, is obtained if the left child is coded '0', or to obtain encoded '1', the first code obtained on the back .

 

⑶ until reaching the root node, the coding sequence is the leaf node corresponding to the Huffman coding.

 

. 4, Huffman translation (solution) code algorithm:

 

⑴ pointer to the root node of the Huffman tree, taking a first Huffman code.

 

⑵ If the Huffman code is '0', the pointer to the root of the left subtree of the current node; if the Huffman code is '1', the pointer to the right child of the root node of the current node.

 

⑶ If the current node pointer is a leaf node, the leaf node corresponding to the character is output; otherwise, removing a Huffman code, and returns ⑵.

 

⑷ If the Huffman code sequence is not ended, the process returns to continue decoding ⑴.

 

Entry

The first line test times

 

The second line 2: the number of characters in the first set of test data n, followed by n characters.

 

On line 3: a first set of test data character weights weight

 

A string to be coded s1

 

Code string s2

 

Other groups of test data and so on

 

Export

The first line of first to n-th row , a first set of test data for each character code value

 

The first row n + 1, the string code value of s1

 

Of the n + 2 line, the string s2 decoded value, if the decoding is not successful, the output error!

 

Other groups of test data and so on

 

Sample input

2 5 A B C D E 15 4 4 3 2 ABDEC 00000101100 4 A B C D 7 5 2 4 ABAD 1110110

Sample Output

A :1 B :010 C :011 D :001 E :000 1010001000011 error! A :0 B :10 C :110 D :111 0100111 DAC

prompt

#include<iostream>
#include<string>
using namespace std;
#define maxw 9999
class HuffmanNode
{
public:
    int weight;
    int parent;
    int left;
    int right;
    HuffmanNode()
    {
        weight=0;
        parent=left=right=-1;
    }
};
 
class HuffmanTree
{
public:
    int len;///总长
    int num;///元素个数
    HuffmanNode *Tree;
    string *HuffmanCode;
    char *message;
    HuffmanTree(int n,char *c,int *w)
    {
        num=n;
        len=2*num-1;
        Tree=new HuffmanNode[len];
        HuffmanCode=new string[num];
        message=new char[num];
        CreateHuffmanTree(c,w);
    }
    void CreateHuffmanTree(char *c,int *w)
    {
        int s1=0,s2=0;
        for(int i=0;i<num;i++)
        {
            Tree[i].weight=w[i];
        }
        for(int i=0;i<num;i++)
        {
            message[i]=c[i];
        }
        for(int i=0;i<len;i++)
        {
            if(i>=num)
                Tree[i].weight=0;
            Tree[i].parent=Tree[i].left=Tree[i].right=-1;
        }
        for(int i=num;i<len;i++)
        {
            SelectMin(i-1,s1,s2);
            Tree[s1].parent=i;
            Tree[s2].parent=i;
            Tree[i].left=s1;
            Tree[i].right=s2;
            Tree[i].weight=Tree[s1].weight+Tree[s2].weight;
        }
    }
    void SelectMin(int pos,int &s1,int &s2)
    {
        int w1=maxw,w2=maxw;
        for(int i=0;i<=pos;i++)
        {
            if(Tree[i].weight<w1&&Tree[i].parent==-1)
            {
                w1=Tree[i].weight;
                s1=i;
            }
        }
        for(int i=0;i<=pos;i++)
        {
            if(i==s1)
                continue;
            if(Tree[i].weight<w2&&Tree[i].parent==-1)
            {
                w2=Tree[i].weight;
                s2=i;
            }
        }
    }
    void HuffmanCoding()
    {
        char *cd=new char[num];
        cd[num-1]='\0';
        int start;
        for(int i=0;i<num;i++)
        {
            start=num-1;
            for(int j=i,k=Tree[i].parent;k!=-1;j=k,k=Tree[k].parent)
            {
                if(Tree[k].left==j)
                {
                    start--;
                    cd[start]='0';
                }
                else
                {
                    start--;
                    cd[start]='1';
                }
                HuffmanCode[i].assign(&cd[start]);
            }
        }
    }
    string code(string str)
    {
        string coding="";
        int length=str.size();
        for(int i=0;i<length;i++)
        {
            char c=str[i];
            for(int j=0;j<num;j++)
            {
                if(c==message[j])
                {
                    coding+=HuffmanCode[j];
                }
            }
        }
        return coding;
    }
    string decode(string str)
    {
        string decoding="";
        string iter="";
        int length=str.size();
        int judge1=0;
        for(int i=0;i<length;i++)
        {
            iter+=str[i];
            for(int j=0;j<num;j++)
            {
                if(iter==HuffmanCode[j])
                {
                    decoding+=message[j];
                    judge1+=(int)iter.size();
                    iter="";
                }
            }
        }
        int judge2=iter.size();
        if(judge1!=length)
            return "error!";
        if(judge2!=0)
            return "error!";
        return decoding;
    }
};
 
int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        int n;
        cin>>n;
        char *c=new char[n];
        for(int i=0;i<n;i++)
            cin>>c[i];
        int *w=new int[n];
        for(int i=0;i<n;i++)
            cin>>w[i];
        HuffmanTree Tree(n,c,w);
        Tree.HuffmanCoding();
        string codeneeding,decodeneeding;
        cin>>codeneeding>>decodeneeding;
        for(int i=0;i<Tree.num;i++)
        {
            cout<<Tree.message[i]<<" "<<":"<<Tree.HuffmanCode[i]<<endl;
        }
        cost<<Tree.code(codeneeding)<<endl;
        cout<<Tree.decode(decodeneeding)<<endl;
        delete []c;
        delete []w;
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/SZU-DS-wys/p/12180826.html