经典算法——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;}
运行结果如下:
- 经典算法——Huffuman树(Huffman编码)
- 数据结构和算法——Huffman树和Huffman编码
- 算法——Huffman编码
- 贪心算法——Huffman编码(哈夫曼编码)
- 贪心算法——Huffman编码(哈夫曼编码) 收藏
- Huffman编码(Huffman树)
- Huffman树&&Huffman编码
- Huffuman Coding(哈夫曼编码)
- 【数据结构与算法】Huffman树&&Huffman编码(附完整源码)
- 【数据结构与算法】Huffman树&&Huffman编码(附完整源码)
- 【数据结构与算法】Huffman树&&Huffman编码(附完整源码)
- huffuman编码
- 贪心算法 赫夫曼编码问题(Huffman)
- Huffman 编码压缩算法
- huffman编码压缩算法
- Huffman 编码压缩算法
- Huffman 编码压缩算法
- Huffman 编码压缩算法
- 猴子吃桃问题
- 参加的活动---百度开放云·创客马拉松:跟手机联动的数字灯泡
- poj 1679 The Unique MST(次小生成树)
- 计算机网络协议模拟程序——滑动窗口协议
- 帮妹子修电脑最装逼的命令
- 经典算法——Huffuman树(Huffman编码)
- SpringMVC之json数据传递
- 参加的活动---【创客马拉松·人物】罗未:走运营商路线是最有效的落地方式
- Linux下进入图形模式后,光标卡住无法进入的问题
- python脚本溢出Windows 2000 Professional的漏洞ms08_067
- 实现tomcat启动时自动运行代码
- Unity3D GUI.Toolbar用 javaScript
- 2013年7月7日
- 如何正确确定网站的浏览量、访问量