溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

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

發(fā)布時間:2020-07-29 14:57:06 來源:億速云 閱讀:241 作者:小豬 欄目:編程語言

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

代碼:

#pragma once
#include<iostream>
#include<stack>
using namespace std;
#define m 20
stack<int> s;
/*哈夫曼樹結(jié)點類HuffmanNode聲明*/
template<class T>
class HuffmanNode {
private:
 HuffmanNode<T> * left;
 HuffmanNode<T> * right;
 T data;
 int weight;
public:
 HuffmanNode(){}
 HuffmanNode(const T& item, HuffmanNode<T> * l, HuffmanNode<T> * r):data(item),left(l),right(r){}
 HuffmanNode<T> * getLeft() { return left; }
 void setLeft(HuffmanNode<T> * left) { this->left = left; }
 HuffmanNode<T> * getRight() { return right; }
 void setRight(HuffmanNode<T> * 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; }
};

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

template<class T>
inline void HuffmanTree<T>::CreateHuffmanTree()
{
 inputNodes();
 HuffmanNode<T> ** H = new HuffmanNode<T>*[count];
 HuffmanNode<T> * p1 = NULL, *p2 = NULL, * p,* t;
 for (int i = 0; i < count; i++) {
 H[i] = new HuffmanNode<T>();
 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<T>();
 p1 = H[i];
 p2 = H[i + 1];
 p->setLeft(p1);
 p->setRight(p2);
 p->setWeight(p1->getWeight() + p2->getWeight());
 //將新的結(jié)點插入結(jié)點數(shù)組中的合適位置,并將被合并的兩個結(jié)點刪除
 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<class T>
inline void HuffmanTree<T>::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<class T>
inline HuffmanNode<T>* HuffmanTree<T>::deCode( HuffmanNode<T> * t,T item)
{
 HuffmanNode<T> * 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<class T>
inline void HuffmanTree<T>::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;
 }
}

測試程序:

#include<iostream>
#include"HuffmanTree.h"
using namespace std;

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

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

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

結(jié)果:

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

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

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI