基本的哈夫曼编码算法的实现

huffman.h文件

#include<stdio.h>

typedef int bool;
#define false 0
#define true 1
typedef struct _CharNode{
	int count;
	char name;
	int weight;
	bool flag;
	struct _CharNode *left;
	struct _CharNode *right;
}CharNode,*PcharNode;

PcharNode Huffman(PcharNode *C, int size);
PcharNode extract_min(PcharNode *Q);

huffman.c文件

#include<stdio.h>
#include<string.h>
#include"huffman.h"
#include<malloc.h>

int countNum = 0;
int iter = 0;

void Swap(PcharNode *Q,int pos,int largest)
{
	PcharNode tmp = Q[pos];
	Q[pos] = Q[largest];
	Q[largest] = tmp;
}

int LeftPos(int pos)
{
	return 2 * pos + 1;
}

int RightPos(int pos)
{
	return 2 * pos + 2;
}

int Parent(int pos)
{
	return (pos - 1) / 2;
}

void max_heapify(PcharNode *Q,int pos)
{
	int left = LeftPos(pos);
	int right = RightPos(pos);
	int largest = pos;
	if(left < countNum && Q[pos]->count > Q[left]->count)
	{
		largest = left;
	}
	else
	{
		largest = pos;
	}
	if(right < countNum && Q[largest]->count > Q[right]->count)
	{
		largest = right;
	}	
	if(largest != pos)
	{
		Swap(Q,pos,largest);
		max_heapify(Q,largest);
	}
}

void heap_increase_key(PcharNode *Q,PcharNode node)
{
	int pos = countNum - 1;
	Q[pos] = node;
	while(pos >= 0 && Q[Parent(pos)]->count > Q[pos]->count)
	{
		Swap(Q,pos,Parent(pos));
		pos = Parent(pos);
	}
}

void max_heap_insert(PcharNode *Q,PcharNode node)
{
	countNum++;
	heap_increase_key(Q,node);
}

PcharNode extract_min(PcharNode *Q)
{
	PcharNode node = Q[0];
	Q[0] = Q[--countNum];
	max_heapify(Q,0);
	return node;
}

PcharNode Huffman(PcharNode *C,int size)
{
	int count = size;
	PcharNode *Q = C;
	int i;
	for(i = 0; i <= count - 2; i++)
	{
		PcharNode node = malloc(sizeof(CharNode));	
		memset(node,0,sizeof(CharNode));
		node->flag = false;
 		node->left = extract_min(Q);
		node->left->weight = 0;
		printf("this count is %d \n",node->left->count);
		node->right = extract_min(Q);
		node->right->weight = 1;
		printf("this count is %d \n",node->right->count);
		int z = node->left->count + node->right->count;
		node->count = z;
		max_heap_insert(Q,node);
	}

	return extract_min(Q);
}
	
void visitTree(PcharNode root,PcharNode Q,int *a)
{
	if(root != Q)
	{
		a[iter++] = Q->weight;
	}
	if(Q->flag == true)
	{
		int i;
		printf("%c ---> ",Q->name);
		for(i = 0; i < iter; i++)
		{
			printf("%d",a[i]);
		}
		printf("\n");
		iter--;
		return;
	}
	
	visitTree(root,Q->left,a);
	visitTree(root,Q->right,a);
	iter--;
}
void main(void)
{
	PcharNode *Q = malloc(sizeof(PcharNode) * 6);

	PcharNode node1 = malloc(sizeof(CharNode));
	node1->count = 5;
	node1->name = 'f';
	node1->flag = true;
	
	max_heap_insert(Q,node1);
		
	PcharNode node2 = malloc(sizeof(CharNode));
	node2->count = 9;
	node2->name = 'e';
	node2->flag = true;
	max_heap_insert(Q,node2);

	PcharNode node3 = malloc(sizeof(CharNode));
	node3->count = 12; 
	node3->name = 'c';
	node3->flag = true;
	max_heap_insert(Q,node3);

	PcharNode node4 = malloc(sizeof(CharNode));
	node4->count = 13;
	node4->name = 'b';
	node4->flag = true;
	max_heap_insert(Q,node4);

	PcharNode node5 = malloc(sizeof(CharNode));
	node5->count = 16;
	node5->name = 'd';
	node5->flag = true;
	max_heap_insert(Q,node5);

	PcharNode node6 = malloc(sizeof(CharNode));
	node6->count = 45; 
	node6->name = 'a';
	node6->flag = true;
	max_heap_insert(Q,node6);
	
	PcharNode root = Huffman(Q,countNum);
	PcharNode tmp = root;	
	printf("root->count = %d\n", root->count);
	//printf("root->left->count = %d\n",root->left->count);
		
	int a[10] = {-1};
	visitTree(root,tmp,a);
}

    主要思想是1、选择最小出现频率的两个字符合并成一个2、建立huffman数。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值