64,647
社区成员
发帖
与我相关
我的任务
分享
#include"HuffmanTree.h"
#include<string>
#include <iostream>
#include<stdlib.h>
#include<cmath>
using namespace std;
int main()
{
HuffmanTree HT;
string test="abbcccdddd";
HT.encodeoutput(test);
HT.decodeoutput();
system("pause");
return 0;
}
#include<iostream>
#include<string>
#include"MinHeap.h"
struct HuffmanNode
{
float data;
int mark;
HuffmanNode* leftChild,* rightChild,* parent;
HuffmanNode():leftChild(NULL),rightChild(NULL),parent(NULL),mark(-1){}
HuffmanNode(float elem,HuffmanNode* left=NULL,HuffmanNode* right=NULL,HuffmanNode* pr=NULL,int i=-1)
: data(elem),leftChild(left),rightChild(right),parent(pr),mark(i){}
bool operator<=(HuffmanNode& R){return data<=R.data;}
bool operator>(HuffmanNode& R){return data>R.data;}
};
struct CharNode
{
char letter;
int counter;
string code;
CharNode():letter('\0'),counter(0),code(""){}
CharNode(char c,int i,string s):letter(c),counter(i),code(s){}
};
class HuffmanTree
{
public:
HuffmanTree(int n=32);
~HuffmanTree(){deleteTree(root);delete []T;hp.~MinHeap();}
void encodeoutput(string s);
void decodeoutput();
protected:
int len;
CharNode* T;
string codestr;
MinHeap<HuffmanNode> hp;
HuffmanNode* root;
void PlantTree( float w[],int n);
void decoding(string s,HuffmanNode* h,int& x);
void encoding(string s,HuffmanNode* h);
void deleteTree(HuffmanNode* t);
void mergeTree(HuffmanNode& ht1,HuffmanNode& ht2,HuffmanNode* &parent);
};
#include"HuffmanTree.h"
HuffmanTree::HuffmanTree(int n)
{
T=new CharNode[n];
for(int i=0;i<n;i++)
{
CharNode* temp = T;
temp=new CharNode();
temp++;
}
len=0;
}
void HuffmanTree::PlantTree( float w[],int n)
{
HuffmanNode* parent,first,second,work;
for(int i=0;i<n;i++)
{
work.data=w[i];
work.mark=i;
work.leftChild=NULL;
work.rightChild=NULL;
work.parent=NULL;
hp.Insert(work);
}
for(int i=0;i<n-1;i++)
{
hp.RemoveMin(first);
hp.RemoveMin(second);
mergeTree(first,second,parent);
hp.Insert(*parent);
}
root=parent;
}
void HuffmanTree::mergeTree(HuffmanNode& ht1,HuffmanNode& ht2,HuffmanNode* &parent)
{
parent=new HuffmanNode;
parent->mark=-1;
parent->leftChild=&ht1;
parent->rightChild=&ht2;
parent->data=ht1.data+ht2.data;
ht1.parent=ht2.parent=parent;
}
void HuffmanTree::deleteTree(HuffmanNode* t)
{
if(t!=NULL)
{
deleteTree(t->leftChild);
deleteTree(t->rightChild);
delete t;
}
}
void HuffmanTree::encodeoutput(string s)
{
len=s.length();
if(len==0)
{
cerr<<"Entering error!"<<endl;
exit(1);
}
static int point=0;
for(int i=0;i<len;i++)
{
bool alter=0;
for(int j=0;j<point;j++)
{
CharNode* n=T+j;
if(n->letter==s[i])
{
(n->counter)++;
alter=1;
break;
}
}
if(!alter)
{
CharNode* m=T+point;
m->letter=s[i];
(m->counter)++;
point++;
}
}
float* w=new float[point];
for(int k=0;k<point;k++)
{
CharNode* l=T+k;
w[k]=l->counter;
}
PlantTree(w,point);
encoding(codestr,root);
cout<<"\n\nEncoded output:\n";
cout<<codestr<<endl;
}
void HuffmanTree::decodeoutput()
{
if(len==0)
{
cerr<<"Entering error!"<<endl;
exit(1);
}
cout<<"\n\nDecoded output:\n";
static int position=0;
for(int i=0;i<len;i++)
{
decoding(codestr,root,position);
}
cout<<endl;
}
void HuffmanTree::decoding(string s,HuffmanNode* h,int& x)
{
if((s[x]=='0')&&(h->leftChild!=NULL))
{
x++;
decoding(s,h->leftChild,x);
}
if((s[x]=='1')&&(h->rightChild!=NULL))
{
x++;
decoding(s,h->rightChild,x);
}
if((h->leftChild==NULL)&&(h->rightChild==NULL))
{
int m=h->mark;
CharNode* Temp=(T+m);
char y=Temp->letter;
cout<<y;
return;
}
}
void HuffmanTree::encoding(string s,HuffmanNode* h)
{
if (h != NULL)
{
if (h->leftChild != NULL)
{
encoding( s+"0", h->leftChild);
encoding( s+"0",h->rightChild);
}
else
{
int m=h->mark;
CharNode* Temp=(T+m);
Temp->code=s;
}
}
}
#include<iostream>
using namespace std;
#ifndef MINHEAP_H
#define MINHEAP_H
static const int DefaultSize=64;
template<class E>
class MinHeap
{
public:
MinHeap(int sz=DefaultSize);
MinHeap(E arr[],int n);
~MinHeap(){delete []heap;}
bool Insert(const E& x);
bool RemoveMin(E& x);
bool IsEmpty()const
{
return (currentSize == maxHeapSize)?true:false;
}
void MakeEmpty(){currentSize=0;}
private:
E* heap;
int currentSize;
int maxHeapSize;
void siftDown(int start,int m);
void siftUp(int start);
};
template<class E>
MinHeap<E>::MinHeap(int sz)
{
maxHeapSize=(DefaultSize<sz)?sz:DefaultSize;
heap=new E[maxHeapSize];
if(heap==NULL)
{
cerr<<"堆存储分配失败!"<<endl;
exit(1);
}
currentSize=0;
}
template<class E>
MinHeap<E>::MinHeap(E arr[],int n)
{
maxHeapSize=(DefaultSize<n)?n:DefaultSize;
heap=new E[maxHeapSize];
if(heap==NULL)
{
cerr<<"堆存储分配失败!"<<endl;
exit(1);
}
for(int i=0;i<n;i++)
heap[i]=arr[i];
currentSize=n;
int currentPos=(currentSize-2)/2;
while(currentPos>=0)
{
siftDown(currentPos,currentSize-1);
currentPos--;
}
}
template<class E>
void MinHeap<E>::siftDown(int start,int m)
{
int i=start,j=2*i+1;
E temp=heap[i];
while(j<=m)
{
if(j<m && heap[j]>heap[j+1])
j++;
if(temp<=heap[j])
break;
else
{
heap[i]=heap[j];
i=j;
j=2*j+1;
}
}
heap[i]=temp;
}
template<class E>
void MinHeap<E>::siftUp(int start)
{
int j=start,i=(j-1)/2;
E temp=heap[j];
while(j>0)
{
if(heap[i]<=temp)
break;
else
{
heap[j]=heap[i];
j=i;
i=(i-1)/2;
}
}
heap[j]=temp;
}
template<class E>
bool MinHeap<E>::Insert(const E& x)
{
if(currentSize==maxHeapSize)
{
cerr<<"Heap Full"<<endl;
return false;
}
heap[currentSize]=x;
siftUp(currentSize);
currentSize++;
return true;
}
template<class E>
bool MinHeap<E>::RemoveMin(E& x)
{
if(!currentSize)
{
cout<<"Heap empty."<<endl;
return false;
}
x=heap[0];
heap[0]=heap[currentSize-1];
currentSize--;
siftDown(0,currentSize-1);
return true;
}
#endif