C++实现哈夫曼树编码解码

作者:qq_36795903 时间:2022-02-13 15:48:09 

本文实例为大家分享了C++实现哈夫曼树的编码解码,供大家参考,具体内容如下

代码:


#pragma once
#include<iostream>
#include<stack>
using namespace std;
#define m 20
stack<int> s;
/*哈夫曼树结点类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();//创建哈夫曼树
HuffmanNode<T> * getRoot() { return root; }
void setRoot(HuffmanNode<T> * t) { root = t; }
void inputNodes();//输入各个结点的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());
//将新的结点插入结点数组中的合适位置,并将被合并的两个结点删除
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;
}

测试数据:

n 1
s 3
t 4
a 10
i 12
p 13
e 15
Ctrl+Z 回车结束输入
输入时权值由小到大排序…也可以在程序里加一个排序

结果:

C++实现哈夫曼树编码解码

来源:https://blog.csdn.net/qq_36795903/article/details/89916203

标签:C++,哈夫曼树
0
投稿

猜你喜欢

  • JAVA多线程知识汇总

    2021-08-03 09:04:47
  • 最值得Java开发者收藏的网站

    2022-03-09 15:57:08
  • spring的Cache注解和redis的区别说明

    2023-10-20 02:01:33
  • C# JsonHelper 操作辅助类,拿来直接用

    2023-11-28 18:55:42
  • android实现简单的画画板实例代码

    2023-02-21 05:46:02
  • Java实现矩阵加减乘除及转制等运算功能示例

    2023-07-05 04:49:53
  • hibernate 中 fetch=FetchType.LAZY 懒加载失败处理方法

    2023-11-08 09:40:31
  • 算法证明每一位都相同十进制数不是完全平方数

    2022-06-21 23:00:46
  • Android开发之删除项目缓存的方法

    2021-07-22 17:15:45
  • Android ViewPager2 使用及自定义指示器视图实现

    2022-06-16 09:35:26
  • VisualStudio Community2019在安装的过程中无法进入安装界面的解决方法

    2023-02-09 11:24:34
  • C#中DataTable排序、检索、合并等操作实例

    2022-08-22 04:58:03
  • Java Calendar类使用案例详解

    2023-07-09 14:03:22
  • Java数据结构 递归之迷宫回溯案例讲解

    2023-04-01 11:16:38
  • c#创建windows服务入门教程实例

    2023-02-24 11:31:31
  • Spring Cloud Stream消息驱动组件使用方法介绍

    2022-05-30 04:30:52
  • Android实现截屏方式整理(总结)

    2023-12-07 05:10:18
  • Java多线程ThreadPoolExecutor详解

    2023-11-23 18:39:32
  • 详解C#读取Appconfig中自定义的节点

    2022-07-16 08:30:38
  • spring中的BeanFactory与FactoryBean的讲解

    2023-03-13 11:18:09
  • asp之家 软件编程 m.aspxhome.com