霍夫曼树构造和霍夫曼编码打印代码C#

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
using namespace std;
typedef char **huffmancode;
typedef struct 
{
	int weight;
	int patent,lchild,rchild;
}htnode,*huffmantree;
void select(huffmantree ht,int n,int &s1,int &s2)
{
	int i=0;
	int min1,min2;
	for(int i=1;i<=n;i++)
	{
		if(ht[i].patent==0)
		{
			min1=i;
			break;
		}
	}
	for(int i=1;i<=n;i++)
	{
		if((ht[i].patent==0)&&(ht[i].weight<ht[min1].weight))
		{
			min1=i;
		}
	}
	s1=min1;
	for(int i=1;i<=n;i++)
	{
		if(ht[i].patent==0&&i!=s1)
		{
			min2=i;
			break;
		}
	}
	for(int i=1;i<=n;i++)
	{
		if((ht[i].patent==0)&&(i!=s1)&&(ht[i].weight<ht[min2].weight))
		min2=i;
	}
	s2=min2;
}
void createhuffmantree(huffmantree &ht,int n)
{
	if(n<=1)
	return;
	int s1,s2;
	int m=2*n-1;
	ht=new htnode[m+1];
	for(int i=1;i<=m;i++)
	{
		ht[i].patent=0;
		ht[i].rchild=0;
		ht[i].lchild=0;
	}
	for(int i=1;i<=n;i++)
	{
		cin>>ht[i].weight;
	}
	for (int i=n+1;i<=m;i++)
	{
		
		select(ht,i-1,s1,s2);
		ht[s1].patent=i;ht[s2].patent=i;
		ht[i].lchild=s1;ht[i].rchild=s2;
		ht[i].weight=ht[s1].weight+ht[s2].weight;		
	}
}
void createhuffmancode(huffmantree ht,huffmancode &hc,int n)
{
	hc=new char*[n+1];
	char *cd=new char[n];
	cd[n-1]='\0';
	int start,c,f;
	for(int i=1;i<=n;i++)
	{
		start=n-1;
		c=i;
		f=ht[i].patent;
		while(f!=0)
		{
			--start;
			if(ht[f].lchild==c)
			cd[start]='0';
			else
			cd[start]='1';
			c=f;
			f=ht[f].patent;
		}
		hc[i]=new char[n-start];
		strcpy(hc[i],&cd[start]);
		
	}
	delete cd;
}

int main()
{
	huffmantree ht;
	huffmancode hc;
	int n;
	cout<<"请输入元素个数"<<endl;
	cin>>n;

	createhuffmantree(ht,n);
	createhuffmancode(ht,hc,n);
	for(int i=1;i<=n;i++)
	{
		cout<<hc[i]<<endl;
	}
	
	return 0;		
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
霍夫曼树霍夫曼编码是一种常用的数据压缩算法,以下是用C语言实现霍夫曼树霍夫曼编码代码。 首先,定义霍夫曼树的节点结构体: ```c typedef struct node { int weight; // 权重 int parent, lchild, rchild; // 父节点、左右孩子节点的下标 }HuffmanNode, *HuffmanTree; ``` 定义霍夫曼编码的结构体: ```c typedef struct { int bit[MaxBit]; // 存放霍夫曼编码的数组 int start; // 编码的起始位置 }HuffmanCode; ``` 接下来,定义霍夫曼树的创建函数: ```c void createHuffmanTree(HuffmanTree ht, int n) { int i, j, min1, min2; // 初始化 for (i = 0; i < 2 * n - 1; i++) { ht[i].parent = ht[i].lchild = ht[i].rchild = -1; } // 输入权值 for (i = 0; i < n; i++) { scanf("%d", &ht[i].weight); } // 构造霍夫曼树 for (i = n; i < 2 * n - 1; i++) { min1 = min2 = MaxValue; ht[i].weight = 0; for (j = 0; j < i; j++) { if (ht[j].parent == -1) { if (ht[j].weight < min1) { min2 = min1; min1 = ht[j].weight; ht[i].lchild = j; } else if (ht[j].weight < min2) { min2 = ht[j].weight; ht[i].rchild = j; } } } ht[i].weight = ht[ht[i].lchild].weight + ht[ht[i].rchild].weight; ht[ht[i].lchild].parent = i; ht[ht[i].rchild].parent = i; } } ``` 接下来,定义霍夫曼编码的生成函数: ```c void getHuffmanCode(HuffmanTree ht, HuffmanCode* hc, int n) { int i, j, c, p; HuffmanCode hc_tmp; for (i = 0; i < n; i++) { hc[i].start = n; c = i; p = ht[c].parent; while (p != -1) { if (ht[p].lchild == c) { hc_tmp.bit[hc_tmp.start--] = 0; } else { hc_tmp.bit[hc_tmp.start--] = 1; } c = p; p = ht[c].parent; } hc[i].start = hc_tmp.start + 1; for (j = hc[i].start; j < n; j++) { hc[i].bit[j] = hc_tmp.bit[j]; } } } ``` 最后,可以在主函数中调用以上函数来实现霍夫曼树霍夫曼编码的创建: ```c int main() { int n; scanf("%d", &n); HuffmanTree ht = (HuffmanTree)malloc(sizeof(HuffmanNode) * 2 * n - 1); HuffmanCode* hc = (HuffmanCode*)malloc(sizeof(HuffmanCode) * n); createHuffmanTree(ht, n); getHuffmanCode(ht, hc, n); // 输出霍夫曼编码 for (int i = 0; i < n; i++) { printf("%d: ", ht[i].weight); for (int j = hc[i].start; j < n; j++) { printf("%d", hc[i].bit[j]); } printf("\n"); } return 0; } ``` 这样,就可以实现霍夫曼树霍夫曼编码的创建了。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值