IDE:Visual Studio 2019
声明:为了方便书写代码,用到了C++的引用调用特性和iostream作为输入输出,读者可以使用指针调用和scanf_s/print语句实现相同效果
tips:有疑问可以在下面交流,我会尽量回复的
头文件heads.h
#pragma once
#include "stdio.h"
#include "iostream"
#define OK 1
#define TRUE 1
#define FALSE 0
#define ERROR 0
#define OVERFLOW -1
using namespace std;
typedef short int Status;
头文件Treefunctions.h
#include "heads.h"
//霍夫曼树结构体
typedef struct {
unsigned int weight;
unsigned int parent, lchild, rchild;
}HTNode, * HuffmanTree;
typedef char** HuffmanCode;
//给定要编码的语句的每个元素在权重数组中的位置进行编码
Status Coding(int* n, HuffmanCode HC);
//建立霍夫曼树及其编码表
void HuffmanCoding(HuffmanTree& HT, HuffmanCode& HC, unsigned int* w, int n);
//创造权重函数
Status Huffweight(unsigned int* w, int n);
//编码展示函数
Status Codingshow(HuffmanCode HC);
//解码
Status DisCoding(HuffmanTree HT, char n[], int num);
源文件HuffmanCode.cpp
#include "Treefunctions.h"
//创造权重函数
Status Huffweight(unsigned int* w,int n) {
unsigned int* y = w;
unsigned int a = n + 1;
while (n) {
//依次输入权重
cout << "第" << a - n << "个字符的权重为:" << endl;
cin >> *y++;
n--;
}
return OK;
}//Huffweight
//树中无parent的元素中权重最小的两个
Status Select(HuffmanTree HT, int n, int &s1, int &s2) {
int* t = (int*)malloc(sizeof(int) * (n + 1));//存储没有双亲的结点所在的位置
int* y = t;
int a=