RubbishBin/zip-unzip-search/include/huffman.h

104 lines
2.0 KiB
C
Raw Permalink Normal View History

2024-10-30 17:23:52 +08:00
//
// Created by ricardo on 22-12-11.
//
#ifndef ZIP_UNZIP_SEARCH_HUFFMAN_H
#define ZIP_UNZIP_SEARCH_HUFFMAN_H
#include "vector"
#include "array"
#include "const.h"
/**
*
*/
struct HuffmanNode
{
/**
*
*/
int id;
/**
*
*
* -1
*/
char data;
/**
*
*
*/
int frequency;
/**
*
*/
int lIndex;
/**
*
*/
int rIndex;
};
class HuffmanCode
{
public:
/**
*
*/
std::vector<HuffmanNode>* nodes = new std::vector<HuffmanNode>();
/**
*
*/
int root = -1;
/**
*
* @param frequencyArray
*/
explicit HuffmanCode(const int * frequencyArray);
/**
*
* @param nodeArray
* @param length
*/
HuffmanCode(HuffmanNode *nodeArray, int length);
~HuffmanCode();
/**
*
*/
void createHuffmanTree();
/**
*
*/
void printHuffmanTree();
/**
*
* @return
*/
std::array<std::vector<char>, ASCII_LENGTH> * getHuffmanCode();
/**
*
* @param dictionary
*/
static void printHuffmanCode(const std::array<std::vector<char>, ASCII_LENGTH>& dictionary);
private:
/**
*
* @param forests
*/
static void sortForests(std::vector<HuffmanNode>& forests);
void printHuffmanTreeR(int nodeId);
void getHuffmanCodeR(std::array<std::vector<char>, ASCII_LENGTH> &dictionary, int nodeId, std::vector<char> &code);
};
#endif //ZIP_UNZIP_SEARCH_HUFFMAN_H