实现效果
压缩前
压缩后
经验证解压缩前与解压缩后文本一致,无出入
文件目录
binaryTreeNode.h
linkedBinaryTree.h
源.cpp
代码如下
binaryTreeNode.h
#ifndef binaryTreeNode_
#define binaryTreeNode_
#include<vector>
#include<bitset>
#include<cmath>
using namespace std;
template <class T>
struct binaryTreeNode
{
int *character;
string element;
binaryTreeNode<T> *leftChild, // left subtree
*rightChild; // right subtree
binaryTreeNode() {
element = "";
character = nullptr;
leftChild = rightChild = NULL;
}
binaryTreeNode(
binaryTreeNode *theLeftChild,
binaryTreeNode *theRightChild)
{
element = "";
character = nullptr;
leftChild = theLeftChild;
rightChild = theRightChild;
}
binaryTreeNode(int c) {
element = "";
character = new int(c);
leftChild = rightChild = NULL;
}
};
#endif
#pragma once
linkedBinaryTree.h
#include<bitset>
#include <iostream>
#include<sstream>
#include <stdio.h>
#include<map>
#include<iterator>
#include<fstream>
#include<iomanip>
#include<list>
#include "binaryTreeNode.h"
using namespace std;
template <class E>
class linkedBinaryTree {
public:
binaryTreeNode<E> *root;
int treeSize;
linkedBinaryTree() {
root = nullptr; treeSize = 0; }
bool empty() {
if (this->treeSize == 0)return 1;
return 0;
}
map<char, string> huffmanMap;
void makeTree(
linkedBinaryTree<E>&, linkedBinaryTree<E>&);
void makeTree(int i);
void visit2(binaryTreeNode<E>*t);
int count;
void preOrder(binaryTreeNode<E> *t);
void toMap(binaryTreeNode<E>*t);
static void output(binaryTreeNode<E> *t)
{
cout << t->element << ' ';
}
void printMap();
void zip(string a, string b);
};
template<class T>
class huffmanNode {
public:
linkedBinaryTree<int> *tree;
T weight;
operator T () const {
return weight; }
huffmanNode() {
weight = 0;
tree = new linkedBinaryTree<T>;
}
};
template<class E>
void visit(binaryTreeNode<E>*t) {
cout << t->element;
cout << endl;
}
template<class T>
class minHeap
{
public:
minHeap(int initialCapacity = 10);
~minHeap() {
delete[] heap; }
bool empty() const {
return heapSize == 0; }
int size() const
{
return heapSize;
}
const T& top()
{
// return min element
if (heapSize == 0)
{
cout << "empty" << endl;
}
return heap[1];
}
void pop();
void push(const T&);
void initialize(T *, int);
void deactivateArray()
{
heap = NULL; arrayLength = heapSize = 0;
}
void output(ostream& out) const;
private:
int heapSize; // number of elements in queue
int arrayLength; // queue capacity + 1
T *heap; // element array
};
template<class T>
minHeap<T>::minHeap(int initialCapacity)
{
// Constructor.
if (initialCapacity < 1)
{
ostringstream s;
s << "Initial capacity = " << initialCapacity << " Must be > 0";
}
arrayLength = initialCapacity + 1;
heap = new T[arrayLength];
heapSize = 0;
}
template<class T>
void minHeap<T>::push(const T& theElement)
{
// Add theElement to heap.
// increase array length if necessary
if (heapSize == arrayLength - 1)
{
// double array length
cout << "full" << endl;
return;
}
// find place for theElement
// currentNode starts at new leaf and moves up tree
int currentNode = ++heapSize;
while (currentNode != 1 && heap[currentNode / 2