ACM模版
赫夫曼编码
main
main.cpp
#include <iostream>
#include "huffman.hpp"
int main(int argc, const char * argv[])
{
char S[] = "I love Golden Dream!";
char s[] = "01101101001111111001011100111111110000001010010110010011101101000100110101";
htTree *codeTree = buildTree(S);
hlTable *codeTable = buildTable(codeTree);
encode(codeTable, S);
decode(codeTree, s);
return 0;
}
queue
queue.hpp
#ifndef queue_hpp
#define queue_hpp
#include <stdio.h>
#include "huffman.hpp"
#define TYPE htNode *
#define MAX_SZ 256
typedef struct _pQueueNode
{
TYPE val;
unsigned int priority;
struct _pQueueNode *next;
}pQueueNode;
typedef struct _pQueue
{
unsigned int size;
pQueueNode *first;
}pQueue;
void initPQueue(pQueue **queue);
void addPQueue(pQueue **queue, TYPE val, unsigned int priority);
TYPE getPQueue(pQueue **queue);
#endif /* queue_hpp */
queue.cpp
#include "queue.hpp"
#include <stdlib.h>
void initPQueue(pQueue **queue)
{
(*queue) = (pQueue *)malloc(sizeof(pQueue));
(*queue)->first = NULL;
(*queue)->size = 0;
return ;
}
void addPQueue(pQueue **queue, TYPE val, unsigned int priority)
{
if ((*queue)->size == MAX_SZ)
{
printf("\nQueue is full.\n");
return ;
}
pQueueNode *aux = (pQueueNode *)malloc(sizeof(pQueueNode));
aux->priority = priority;
aux->val = val;
if ((*queue)->size == 0 || (*queue)->first == NULL)
{
aux->next = NULL;
(*queue)->first = aux;
(*queue)->size = 1;
return ;
}
else
{
if (priority <= (*queue)->first->priority)
{
aux->next = (*queue)->first;
(*queue)->first = aux;
(*queue)->size++;
return ;
}
else
{
pQueueNode *iterator = (*queue)->first;
while (iterator->next != NULL)
{
if (priority <= iterator->next->priority)
{
aux->next = iterator->next;
iterator->next = aux;
(*queue)->size++;
return ;
}
iterator = iterator->next;
}
if (iterator->next == NULL)
{
aux->next = NULL;
iterator->next = aux;
(*queue)->size++;
return ;
}
}
}
}
TYPE getPQueue(pQueue **queue)
{
TYPE returnValue;
if ((*queue)->size > 0)
{
returnValue = (*queue)->first->val;
(*queue)->first = (*queue)->first->next;
(*queue)->size--;
}
else
{
printf("\nQueue is empty.\n");
}
return returnValue;
}
huffman
huffman.hpp
#ifndef huffman_hpp
#define huffman_hpp
#include <stdio.h>
typedef struct _htNode
{
char symbol;
struct _htNode *left, *right;
}htNode;
typedef struct _htTree
{
htNode *root;
}htTree;
typedef struct _hlNode
{
char symbol;
char *code;
struct _hlNode *next;
}hlNode;
typedef struct _hlTable
{
hlNode *first;
hlNode *last;
}hlTable;
htTree *buildTree(char *inputString);
hlTable *buildTable(htTree *huffmanTree);
void encode(hlTable *table, char *stringToEncode);
void decode(htTree *tree, char *stringToDecode);
#endif /* huffman_hpp */
huffman.cpp
#include "huffman.hpp"
#include "queue.hpp"
#include <string>
#include <stdlib.h>
htTree *buildTree(char *inputString)
{
int *probability = (int *)malloc(sizeof(int) * 256);
for (int i = 0; i < 256; i++)
{
probability[i] = 0;
}
for (int j = 0; inputString[j] != '\0'; j++)
{
probability[(unsigned char)inputString[j]]++;
}
pQueue *huffmanQueue;
initPQueue(&huffmanQueue);
for (int k = 0; k < 256; k++)
{
if (probability[k] != 0)
{
htNode *aux = (htNode *)malloc(sizeof(htNode));
aux->left = NULL;
aux->right = NULL;
aux->symbol = (char)k;
addPQueue(&huffmanQueue, aux, probability[k]);
}
}
free(probability);
while (huffmanQueue->size != 1)
{
int priority = huffmanQueue->first->priority;
priority += huffmanQueue->first->next->priority;
htNode *left = getPQueue(&huffmanQueue);
htNode *right = getPQueue(&huffmanQueue);
htNode *newNode = (htNode *)malloc(sizeof(htNode));
newNode->left = left;
newNode->right = right;
addPQueue(&huffmanQueue, newNode, priority);
}
htTree *tree = (htTree *)malloc(sizeof(htTree));
tree->root = getPQueue(&huffmanQueue);
return tree;
}
void traverseTree(htNode *treeNode, hlTable ** table, int k, char code[256])
{
if (treeNode->left == NULL && treeNode->right == NULL)
{
code[k] = '\0';
hlNode *aux = (hlNode *)malloc(sizeof(hlNode));
aux->code = (char *)malloc(sizeof(char) * (strlen(code) + 1));
strcpy(aux->code, code);
aux->symbol = treeNode->symbol;
aux->next = NULL;
if ((*table)->first == NULL)
{
(*table)->first = aux;
(*table)->last = aux;
}
else
{
(*table)->last->next = aux;
(*table)->last = aux;
}
}
if (treeNode->left != NULL)
{
code[k] = '0';
traverseTree(treeNode->left, table, k + 1, code);
}
if (treeNode->right != NULL)
{
code[k] = '1';
traverseTree(treeNode->right, table, k + 1, code);
}
return ;
}
hlTable *buildTable(htTree *huffmanTree)
{
hlTable *table = (hlTable *)malloc(sizeof(hlTable));
table->first = NULL;
table->last = NULL;
char code[256];
int k = 0;
traverseTree(huffmanTree->root, &table, k, code);
return table;
}
void encode(hlTable *table, char *stringToEncode)
{
hlNode *traversal;
printf("Encoding......\nInput string:\n%s\nEncoded string:\n", stringToEncode);
for (int i = 0; stringToEncode[i] != '\0'; i++)
{
traversal = table->first;
while (traversal->symbol != stringToEncode[i])
{
traversal = traversal->next;
}
printf("%s", traversal->code);
}
printf("\n");
return ;
}
void decode(htTree *tree, char *stringToDecode)
{
htNode *traversal = tree->root;
printf("\nDecoding......\nInput string:\n%s\nDecoded string:\n", stringToDecode);
for (int i = 0; stringToDecode[i] != '\0'; i++)
{
if (traversal->left == NULL && traversal->right == NULL)
{
printf("%c", traversal->symbol);
traversal = tree->root;
}
if (stringToDecode[i] == '0')
{
traversal = traversal->left;
}
else
{
traversal = traversal->right;
}
}
if (traversal->left == NULL && traversal->right == NULL)
{
printf("%c", traversal->symbol);
}
putchar('\n');
return ;
}