哈夫曼树的构造

Huffman_encoding (s,f)

input:s(字符串)和f(字频数组)

output:T(S的哈夫曼树)

begin:

insert all characters into a heap H according to their frequncy

while H is not empty do

    if H contains only only character X then

         make X the root of T

   else

       pick two characters X and Y with lowest frequencies

             and delete them from h;

     replace X and Y with a new character Z whose frequency is

          the sum of the frequencies of X and Y

     insert  Z  to H

    make X and Y children of   Z in T

end

代码如下:题意如下:6个字符A B C D E F,其字频分别为 5 2 3 4 10 1  它们的哈夫树的构造。

代码:

 

// 哈弗曼编码.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
using namespace std;
int K =0;//当前堆的大小 从0开始的
class NODE
{
public:
	 NODE *left;
	 NODE *right;
	 int value;

	bool operator<(const NODE &other)const//按x排序的时候能用到   
    {  
        return value<other.value;  
    }  
	const NODE& operator=(NODE &b)
	{
		left=b.left;
		right=b.right;
		value=b.value;
		return *this;
	}
	bool operator==(NODE &b)
	{
		return value==b.value;
	}
	

};
void build_heap(NODE arr[],int n,NODE x)//建最小堆
{
	arr[n]=x;
	NODE tmp=x;
	if(n==0)
	{
		cout<<"n--"<<n<<"----";cout<<tmp.value<<endl;
		return;
	}
	while(n>0)
	{
		if(tmp<arr[((n+1)/2)-1]){ arr[n]=arr[((n+1)/2)-1];n=((n+1)/2)-1;}
		else break;
		
		
	}
	if(n>=0)
		arr[n]=tmp;
	cout<<"n--"<<n<<"----";cout<<tmp.value<<endl;
	
}
void delete_heap_1(NODE arr[],NODE x)//调整从堆顶元素
{
	
	int j;
	
	NODE t;
	arr[0]=x;
	int i=0;
	
	while(i<K)//还有孩子
	{
		j=2*i+1;
		if(j>=K) break;
		if((arr[j+1]<arr[j])&&(j+1<K)) j=j+1;
		//if((arr[i]<arr[j])||(arr[i]==arr[j])) break;
		
		if(arr[j]<arr[i])
		{
			t=arr[j];
			arr[j]=arr[i];
			arr[i]=t;
			i=j;
		}else break;
	}
	

	
}




int _tmain(int argc, _TCHAR* argv[])
{
	NODE node[6];
	NODE arr[20];
	int i;
	for(i=0;i<20;i++)
	{
		arr[i].left=NULL;
		arr[i].right=NULL;
		arr[i].value=-1;
	}
	for(i=0;i<6;i++)
	{
		node[i].left=NULL;
		node[i].right=NULL;
	}
	node[0].value=5;
	node[1].value=2;
	node[2].value=3;
	node[3].value=4;
	node[4].value=10;
	node[5].value=1;
	for(i=0;i<6;i++)
	build_heap(arr,i,node[i]);
	K=5;

	/*
	for(i=5;i>=0;i--)
	{
		cout<<"----------"<<endl;
		cout<<arr[0].value<<endl;
		delete_heap_1(arr,arr[i]);
		K--;
	}*/

	
	cout<<arr[0].value<<endl;

	NODE *root=new NODE();
	root->right=NULL;
	root->right=NULL;
	root->value=-1;
	

	while(K>=0)
	{
		if(K==0)//只有一个元素
		{
			root=&arr[0];
			break;
		}else
		{
			NODE x;
			x=arr[0];
			cout<<endl<<endl;
			cout<<"x----"<<x.value<<endl;
			delete_heap_1(arr,arr[K]);
			K--;
			
			NODE y;
			y=arr[0];
			delete_heap_1(arr,arr[K]);
			K--;
			cout<<"y----"<<y.value<<endl;

			NODE z;
			z.left=&x;
			z.right=&y;
			z.value=x.value+y.value;
			cout<<"z---"<<z.value<<endl;
			cout<<endl<<endl;
			K++;
			build_heap(arr,K,z);





		}
	}
	cout<<"value=="<<root->value<<endl;
	
	return 0;
}


 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值