哈夫曼编码

哈夫曼编码

#include
#include
#include
using namespace std; 

typedef struct HuffmanTree* Huffman; 
struct HuffmanTree{
	int data;
	Huffman Left,Right; 
	string k;
};

//小根堆的结构 
typedef struct HeapStruct* MinHeap;
struct HeapStruct{
	Huffman* Data;
	int Size;
	int Capacity;
}; 

//1.创建一个最大容量为MaxSize的空堆 
MinHeap Create(int MaxSize); 
//2.判断堆是否为空 
bool IsEmpty(MinHeap H); 
//3.判断堆是否已满
bool IsFull(MinHeap H); 
//4.向最小堆中插入一个结点 
void Insert(MinHeap H,Huffman item);
//5.删除最小堆中的根节点 
Huffman DeleteMin(MinHeap H); 

//6.创造最小堆
void PercDown(MinHeap H,int p); 
void BuildHeap(MinHeap H);

//7.哈夫曼树的构造
Huffman CreateHuf(MinHeap H); 

//8.层次遍历 
void LevelOrderTraversal(Huffman H); 

void LevelOrderTraversal1(MinHeap H); 




int main(){
	int N;
	scanf("%d",&N);
	MinHeap H=Create(N);
	HuffmanTree k[N];
	for(int i=0;i<N;i++){
		cin>>k[i].data;
		k[i].Left=k[i].Right=NULL;
		H->Data[i+1]=&k[i]; 
		H->Size++;
	}
	
	BuildHeap(H);
	LevelOrderTraversal1(H);
	printf("\n");
	Huffman Huf=CreateHuf(H);
	LevelOrderTraversal(Huf);
	
	return 0;
}


//1.创建一个最大容量为MaxSize的空堆 
MinHeap Create(int MaxSize){
	MinHeap H=new HeapStruct;
	H->Data=new Huffman[MaxSize+1];
	H->Size=0;
	H->Capacity=MaxSize;
	return H;
}

//2.判断堆是否为空 
bool IsEmpty(MinHeap H){
	return(H->Size==0); 
} 

//3.判断堆是否已满
bool IsFull(MinHeap H){
	return (H->Size==H->Capacity);
}

//4.向最小堆中插入一个结点 
void Insert(MinHeap H,Huffman item){
	int i;
	if(IsFull(H)){
		printf("堆已满无法继续插入!");
		return ;
	}else if(IsEmpty(H)){
		H->Data[1]=item;
		H->Size++;
		return ;
	}
	i=++H->Size;
	
	for(;i>1&&H->Data[i/2]->data>item->data;i=i/2){
		H->Data[i]=H->Data[i/2];
	}
	H->Data[i]=item;
}

//5.删除最小堆中的根节点 
Huffman DeleteMin(MinHeap H){
	int Parent,Child;
	if(IsEmpty(H)){
		printf("堆为空无法删除");
		return NULL;
	}
	Huffman MinData=H->Data[1],temp;
	temp=H->Data[H->Size--];
	for(Parent=1;2*Parent<=H->Size;Parent=Child){
		Child=2*Parent;
		if((Child!=H->Size)&&(H->Data[Child+1]->data<H->Data[Child]->data))
			Child++;
		if(H->Data[Child]->data>temp->data)break;
		else H->Data[Parent]=H->Data[Child];
	}
	H->Data[Parent]=temp;
	return MinData;
}

//6.创造最小堆
void PercDown(MinHeap H,int p){
	int Parent,Child;
	Huffman temp=H->Data[p];
	for(Parent=p;2*Parent<=H->Size;Parent=Child){
		Child=2*Parent;
		if((Child!=H->Size)&&(H->Data[Child]->data>H->Data[Child+1]->data))
			Child++;
		if(temp->data<H->Data[Child]->data)break;
		else H->Data[Parent]=H->Data[Child];
	}
	H->Data[Parent]=temp;
} 

void BuildHeap(MinHeap H){
	int i;
	for(i=H->Size/2;i>0;i--){
		PercDown(H,i);
	}
}
//7.哈夫曼树的构造
Huffman CreateHuf(MinHeap H){
	Huffman T,p,q;
	int k=H->Size; 
	for(int i=1;i<k;i++){
		T=new HuffmanTree;
		T->Left=DeleteMin(H);
		T->Right=DeleteMin(H);
		T->data=T->Left->data+T->Right->data;
		Insert(H,T);
	}
	T=DeleteMin(H);
	return T;
}

//层次遍历,给叶子结点赋值哈夫曼编码 
void LevelOrderTraversal(Huffman H){
	queue<Huffman>Q;
	Huffman temp=H;
	if(!H){
		printf("哈夫曼树为空!");
		return ;
	}
	else{
			Q.push(H);
			H->k="";
			while(!Q.empty()){
				Huffman T=Q.front();
				Q.pop();
				if(T->Left){
					Q.push(T->Left);
					T->Left->k=T->k+"0";
				}
				if(T->Right){
					Q.push(T->Right);
					T->Right->k=T->k+"1";
				}
				if(!(T->Left)&&!(T->Right))
					cout<<T->data<<":"<<T->k<<endl;
			}
		}
	
}

void LevelOrderTraversal1(MinHeap H){
	queue<int>Q;
	if(IsEmpty(H)){
		printf("最小堆为空!");
		return ;
	}
	else{
		int i=1;
		Q.push(i);
		while(!Q.empty()){
			int x=Q.front();
			Q.pop();
			printf("%d ",H->Data[x]->data);
			x*=2; 
			if(x<=H->Size)Q.push(x);
			if(x+1<=H->Size)Q.push(x+1);
		}
	}
} 

你可能感兴趣的:(哈夫曼编码)