哈夫曼树c语言实现

由于哈夫曼树是最优二叉树,所以我们只要用两个指针变量表示其孩子,一个变量表示父亲。
结构体如下

{
	struct hahahahahaha* father;
	struct hahahahahaha* left;
	struct hahahahahaha* right;
	int data;
}left;

在生成小顶堆的时候,我用排序,直接塞到一个指针组的指针组里排,方便数据的新增与拿取。

left* built_tree(int* a, int n)
{
	int i = 0;
	left *b[205];
	while (i < n)
	{
		left *s = creat_left();
		s->data = a[i];
		b[i] = s;
		i++;
	}
	int j = 0;
	while (j < 150)
	{
		b[i + j] = creat_left();
		j++;
	}
	i = n / 2 -1;
	sort(b, n);//sort(b,b+n)
	left* s=NULL;
	while (n > 1)
	{
		int dad =b[0]->data+b[1]->data;
		s= creat_left();
		s->data = dad;
		b[0]->father = s;
		b[1]->father = s;
		s->left = b[0];
		s->right = b[1];
		b[0] = s;
		b[1] = b[n - 1];
		n--;
		sort(b, n);
	}
	return s;
}

源代码

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct hahahahahaha
{
	struct hahahahahaha* father;
	struct hahahahahaha* left;
	struct hahahahahaha* right;
	int data;
}left;
void swap(left* a, left* b)
{
	left t = *a; 
	left tt = *b;
	*a = tt; 
	*b = t;
}
left* creat_left()
{
	left* s = (left*)malloc(sizeof(left));
	s->father = NULL;
	s->left = NULL;
	s->right = NULL;
	return s;
}
typedef struct nnn1
{
	struct nnn1* next;
	left* data;
}link;
link* creat_link()
{
	link* s = (link*)malloc(sizeof(link));
	return s;
}
void sort(left* a[], int n)
{
	int i = 0, j = 0,t;
	left *tt;
	for (i = 0; i < n - 1; i++)
	{
		for (j = 0; j < n -1; j++)
		{
			if (a[j]->data >= a[j+1]->data)
			{
				tt = a[j], a[j] = a[j+1]; a[j+1] = tt;
			}
		}
	}// 
}
left* built_tree(int* a, int n)
{
	int i = 0;
	left *b[205];
	while (i < n)
	{
		left *s = creat_left();
		s->data = a[i];
		
		b[i] = s;//printf("%d ", b[i]->data);
		i++;
	}
	int j = 0;
	while (j < 150)
	{
		b[i + j] = creat_left();
		j++;
	}
	i = n / 2 -1;
	sort(b, n);
	//printf("%d", b[0]->data);
	left* s=NULL;
	while (n > 1)
	{
		int dad =b[0]->data+b[1]->data;
		s= creat_left();
		s->data = dad;
		b[0]->father = s;
		b[1]->father = s;
		s->left = b[0];
		s->right = b[1];
		b[0] = s;
		b[1] = b[n - 1];
		n--;
		sort(b, n);
	}
	return s;
}
void print_treef(left* root, int deep)
{
	int  i = 0;
	while (i < deep)
	{
		printf("-");
		i++;
	}
	printf("%d\n", root->data);
	if (root->left != NULL)
	{
		print_treef(root->left, deep * 2);
	}
	if (root->right != NULL)
	{
		print_treef(root->right, deep * 2);
	}
}
int main()
{
	int a[10] = { 5,8,4,11,9,13 };
	left* root = built_tree(a, 6);
	print_treef(root, 1);
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的哈夫曼树的 C 语言实现,不使用结构体: ```c #include <stdio.h> #include <stdlib.h> // 定义哈夫曼树节点结构体 typedef struct huf_node { int weight; // 权值 int parent; // 双亲节点下标 int lchild; // 左孩子节点下标 int rchild; // 右孩子节点下标 } huf_node; // 选择权值最小的两个节点 void select_min(huf_node *ht, int n, int *s1, int *s2) { int i; *s1 = *s2 = -1; for (i = 0; i < n; i++) { if (ht[i].parent == -1) { if (*s1 == -1 || ht[i].weight < ht[*s1].weight) { *s2 = *s1; *s1 = i; } else if (*s2 == -1 || ht[i].weight < ht[*s2].weight) { *s2 = i; } } } } // 构建哈夫曼树 void create_huffman(huf_node *ht, int *w, int n) { int i, s1, s2; for (i = 0; i < n; i++) { ht[i].weight = w[i]; ht[i].parent = -1; ht[i].lchild = -1; ht[i].rchild = -1; } for (i = n; i < 2 * n - 1; i++) { select_min(ht, i, &s1, &s2); ht[s1].parent = i; ht[s2].parent = i; ht[i].lchild = s1; ht[i].rchild = s2; ht[i].weight = ht[s1].weight + ht[s2].weight; } } int main() { int i, n; int *w; huf_node *ht; printf("请输入权值个数:"); scanf("%d", &n); w = (int *)malloc(n * sizeof(int)); ht = (huf_node *)malloc((2 * n - 1) * sizeof(huf_node)); printf("请输入%d个权值:", n); for (i = 0; i < n; i++) { scanf("%d", &w[i]); } create_huffman(ht, w, n); printf("哈夫曼树:\n"); for (i = 0; i < 2 * n - 1; i++) { printf("%d %d %d %d\n", ht[i].weight, ht[i].parent, ht[i].lchild, ht[i].rchild); } free(w); free(ht); return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值