经典算法——Huffuman树(Huffman编码)

来源:互联网 发布:java和javaweb哪个好 编辑:程序博客网 时间:2024/05/30 21:58
#include<iostream>#include<cstdlib>#include<string>using namespace std;//definition of the Huffuman tree node.struct TreeNode{       TreeNode *left;       TreeNode *right;       string operatorMark;       string huffmanCode;       float probability;   };//definition of the liststruct LinkNode{       LinkNode *next;       string operatorMark;       float probability;       TreeNode *treeNodePointer;};//definition of function createLinkstruct LinkNode* createLink(int number){     struct LinkNode *head;     struct LinkNode *pointer;     head = new LinkNode;     head->next = NULL;     head->treeNodePointer = NULL;     int count = 1;     cout<<"Please input the data of the No.1 LinkNode.\n";     cout<<"Please input the operatorMark : ";     cin>>head->operatorMark;     cout<<"Please input the probability : ";     cin>>head->probability;     while(count!=number)     {         pointer = new LinkNode;         pointer->next = NULL;         cout<<"Please input the data of the No."<<count+1<<" LinkNode.\n";         cout<<"Please input the operatorMark : ";         cin>>pointer->operatorMark;         cout<<"Please input the probability : ";         cin>>pointer->probability;         pointer->treeNodePointer = NULL;         pointer->next = head;         head = pointer;         count++;     }     return head;}//definition of the function of linkSort which is used to sort the link.struct LinkNode* linkSort(struct LinkNode *head){       LinkNode *current;       LinkNode *beforeCurrent;       LinkNode *newHead;       LinkNode *newNode;       LinkNode *minimum;       newHead = NULL;       while(head != NULL)       {           for(current=head,minimum=head;current->next!=NULL;current=current->next)           {               if((current->next->probability) > (minimum->probability))               {                   beforeCurrent = current;                   minimum = current->next;               }           }           if(newHead == NULL)           {              newHead = new LinkNode;              newHead->treeNodePointer = NULL;              newHead->next = NULL;              newHead->operatorMark = minimum->operatorMark;              newHead->probability = minimum->probability;              newHead->treeNodePointer = minimum->treeNodePointer;              newHead->next = NULL;           }           else{                  newNode = new LinkNode;                  newNode->treeNodePointer = NULL;                  newNode->next = NULL;                  newNode->operatorMark = minimum->operatorMark;                  newNode->probability = minimum->probability;                  newNode->treeNodePointer = minimum->treeNodePointer;                  newNode->next = newHead;                  newHead = newNode;               }           if(minimum == head)           {               head = head->next;           }           else           {               beforeCurrent->next = minimum->next;           }       }       return newHead;}//definition of the function of outputLink.void outputLink(struct LinkNode *head){     int count = 1;     struct LinkNode *pointer;     cout<<"----------------------------The data of the Link.-------------------------------\n";     pointer = head;     if(head != NULL)     while(pointer != NULL)     {         cout<<"The operatorMark and the probability of No."<<count++<<" node are : ";         cout<<pointer->operatorMark<<" and "<<pointer->probability;         cout<<endl;         pointer = pointer->next;     }}//definition of the function of deleteNodestruct LinkNode* deleteNode(struct LinkNode *head,struct LinkNode *deletionNode){       if(head != NULL)    {       deletionNode->operatorMark = head->operatorMark;       deletionNode->probability = head->probability;       deletionNode->treeNodePointer = head->treeNodePointer;       head = head->next;    }    return head;}struct LinkNode* addNode(struct LinkNode *head,struct LinkNode *addtionNode){       addtionNode->next = head;       head = addtionNode;       return head;}//definition of the function of build the linkstruct TreeNode* createHuffmanTree(struct LinkNode *head,int flag){       struct TreeNode *parentNode;       struct TreeNode *leftNode;       struct TreeNode *rightNode;       struct LinkNode *firstNode;       struct LinkNode *secondNode;       struct LinkNode *composeNode;       while(flag>0)       {          firstNode = new LinkNode;           //The First LinkNode deleted from the Link.          firstNode->next = NULL;          firstNode->treeNodePointer = NULL;          secondNode = new LinkNode;          //The Second LinkNode deleted from the Link.          secondNode->next = NULL;          secondNode->treeNodePointer = NULL;          head = deleteNode(head,firstNode);          head = deleteNode(head,secondNode);          composeNode = new LinkNode;          composeNode->next = NULL;          composeNode->treeNodePointer = NULL;          composeNode->operatorMark = firstNode->operatorMark + secondNode->operatorMark;          composeNode->probability = firstNode->probability + secondNode->probability;          parentNode = new TreeNode;          parentNode->left = NULL;          parentNode->right = NULL;          parentNode->operatorMark = composeNode->operatorMark;          parentNode->probability = composeNode->probability;          if(firstNode->treeNodePointer == NULL)          {              leftNode = new TreeNode;              leftNode->left = NULL;              leftNode->right = NULL;              leftNode->probability = firstNode->probability;              leftNode->operatorMark = firstNode->operatorMark;              parentNode->left = leftNode;          }          else          {              parentNode->left = firstNode->treeNodePointer;          }          if(secondNode->treeNodePointer == NULL)          {              rightNode = new TreeNode;              rightNode->left = NULL;              rightNode->right = NULL;              rightNode->probability = secondNode->probability;              rightNode->operatorMark = secondNode->operatorMark;              parentNode->right = rightNode;          }          else          {              parentNode->right = secondNode->treeNodePointer;          }          composeNode->treeNodePointer = parentNode;          head = addNode(head,composeNode);          head = linkSort(head);          flag--;       }       return parentNode;}//The function that can be used to traval the Huffman Tree.float visitHuffmanTree(struct TreeNode *root,float &total){         if(root != NULL)     {             if(root->left != NULL)             {                root->left->huffmanCode = root->huffmanCode + "0";             }             if(root->right != NULL)             {                root->right->huffmanCode = root->huffmanCode + "1";             }             if((root->left == NULL)&&(root->right == NULL))             {                cout<<"The Node "<<root->operatorMark<<"'s huffmanCode is : ";                cout<<root->huffmanCode<<endl;                total += (root->huffmanCode.size())*(root->probability);             }             visitHuffmanTree(root->left,total);             visitHuffmanTree(root->right,total);     }     return total;}//The main function.int main(){    int number;    float total = 0;    struct LinkNode *head;    struct TreeNode *root;    cout<<"-----------------------The Huffman Tree Coding Program.-------------------------\n";    cout<<"Please input the number of the Link : ";    cin>>number;    head = createLink(number);    outputLink(head);    head = linkSort(head);    cout<<"-------------------------------After Sorting.-----------------------------------\n";    outputLink(head);    root = createHuffmanTree(head,number-1);    root->huffmanCode = "";    cout<<"---------------------------The Data Of Huffman Code.----------------------------\n";    total = visitHuffmanTree(root,total);    cout<<"\nThe Huffman Code's Average Length is : "<<total<<endl;    cout<<"\nPlease Enter Or Press Any Key To Continue.\n";    cin.get();    system("PAUSE");    return 0;}


运行结果如下:




原创粉丝点击