真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

C++如何實(shí)現(xiàn)哈夫曼樹(shù)編碼解碼

小編這次要給大家分享的是C++如何實(shí)現(xiàn)哈夫曼樹(shù)編碼解碼,文章內(nèi)容豐富,感興趣的小伙伴可以來(lái)了解一下,希望大家閱讀完這篇文章之后能夠有所收獲。

創(chuàng)新互聯(lián)公司主營(yíng)應(yīng)城網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,成都app軟件開(kāi)發(fā)公司,應(yīng)城h5微信小程序搭建,應(yīng)城網(wǎng)站營(yíng)銷推廣歡迎應(yīng)城等地區(qū)企業(yè)咨詢

代碼:

#pragma once
#include
#include
using namespace std;
#define m 20
stack s;
/*哈夫曼樹(shù)結(jié)點(diǎn)類HuffmanNode聲明*/
template
class HuffmanNode {
private:
 HuffmanNode * left;
 HuffmanNode * right;
 T data;
 int weight;
public:
 HuffmanNode(){}
 HuffmanNode(const T& item, HuffmanNode * l, HuffmanNode * r):data(item),left(l),right(r){}
 HuffmanNode * getLeft() { return left; }
 void setLeft(HuffmanNode * left) { this->left = left; }
 HuffmanNode * getRight() { return right; }
 void setRight(HuffmanNode * right) { this->right=right; }
 T& getData() { return data; }
 void setData(const T& item) { data = item; }
 int getWeight() { return weight; }
 void setWeight(int weight) { this->weight = weight; }
};

/*哈夫曼樹(shù)類HuffmanTree聲明*/
template
class HuffmanTree {
private:
 HuffmanNode * root;
 T data[m];
 int weight[m];
 int count;
public:
 HuffmanTree(HuffmanNode * t=NULL):root(t){}
 void CreateHuffmanTree();//創(chuàng)建哈夫曼樹(shù)
 HuffmanNode * getRoot() { return root; }
 void setRoot(HuffmanNode * t) { root = t; }
 void inputNodes();//輸入各個(gè)結(jié)點(diǎn)的data和weight值
 HuffmanNode * deCode(HuffmanNode*t,T item);
 void showCode();
};

template
inline void HuffmanTree::CreateHuffmanTree()
{
 inputNodes();
 HuffmanNode ** H = new HuffmanNode*[count];
 HuffmanNode * p1 = NULL, *p2 = NULL, * p,* t;
 for (int i = 0; i < count; i++) {
 H[i] = new HuffmanNode();
 H[i]->setData(data[i]);
 H[i]->setWeight(weight[i]);
 H[i]->setLeft(NULL);
 H[i]->setRight(NULL);
 }

 for (int i = 0; i < count - 1; i++) {
 p = new HuffmanNode();
 p1 = H[i];
 p2 = H[i + 1];
 p->setLeft(p1);
 p->setRight(p2);
 p->setWeight(p1->getWeight() + p2->getWeight());
 //將新的結(jié)點(diǎn)插入結(jié)點(diǎn)數(shù)組中的合適位置,并將被合并的兩個(gè)結(jié)點(diǎn)刪除
 t = p;
 int j = i + 2;
 while ( j < count &&H[j]->getWeight() < t->getWeight()) {
  H[j - 1] = H[j];
  j++;
 }
 H[j - 1] = t;
 }
 root = H[count - 1];
}

template
inline void HuffmanTree::inputNodes()
{
 int i = 0;
 for (int j = 0; j < m; j++) {
 data[j] = NULL;
 weight[j] = 0;
 }
 while (cin >> data[i] >> weight[i]) {
 i++;
 }
 count = i;
}

template
inline HuffmanNode* HuffmanTree::deCode( HuffmanNode * t,T item)
{
 HuffmanNode * p, * q;
 if(t==NULL)
 return NULL;
 if (t->getData() == item)
 return t;
 if ((p = deCode(t->getLeft(), item))!=NULL) {
 s.push(0);
 return p;
 }
 else if((q = deCode(t->getRight(), item))!=NULL){
 s.push(1);
 return q ;
 }
}

template
inline void HuffmanTree::showCode()
{
 cout << root->getWeight() << endl;
 cout << (root->getLeft())->getRight()->getData() << endl;
 for (int i = 0; i < count; i++) {
 cout << data[i] << "——";
 deCode(root, data[i]);
 while (!s.empty()) {
  cout << s.top();
  s.pop();
 }
 cout << endl;
 }
}

測(cè)試程序:

#include
#include"HuffmanTree.h"
using namespace std;

int main() {
 HuffmanTree * tree = new HuffmanTree();
 tree->CreateHuffmanTree();
 tree->showCode();
 system("pause");
 return 0;
}

測(cè)試數(shù)據(jù):

n 1
s 3
t 4
a 10
i 12
p 13
e 15
Ctrl+Z 回車結(jié)束輸入
輸入時(shí)權(quán)值由小到大排序…也可以在程序里加一個(gè)排序

結(jié)果:

C++如何實(shí)現(xiàn)哈夫曼樹(shù)編碼解碼

看完這篇關(guān)于C++如何實(shí)現(xiàn)哈夫曼樹(shù)編碼解碼的文章,如果覺(jué)得文章內(nèi)容寫(xiě)得不錯(cuò)的話,可以把它分享出去給更多人看到。


文章標(biāo)題:C++如何實(shí)現(xiàn)哈夫曼樹(shù)編碼解碼
分享鏈接:http://weahome.cn/article/pgisgs.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部