Heffman code Implementation _ Two fork Tree

Source: Internet
Author: User

Define its storage structure, a typical two-fork tree, just a weighted value.

typedef struct
{
	int weight;
	int parent, lchild, rchild;
} Htnode, *huffmantree;

typedef char **huffmancode;

Huffmancode is an array of strings used to preserve the final coding results of the leaf nodes.

How much is the storage space? In the previous chapter, the process of constructing the Huffman tree can find the law, and the Huffman trees constructed by n leaf nodes have 2n-1 nodes, which is the space we want to allocate.

int m = 2 * n-1;
*ht = (Huffmantree) malloc ((m+1) *sizeof (Htnode));/Allocate one more space, No. No. 0

The interface form of the function is as follows:

BOOL Huffmancoding (Huffmantree *ht, Huffmancode *hc, int *w, int n)

HT is the final constructed Huffman tree, is an output parameter, HC is also an output function, is a string array, output the final encoding. W Store the weight of n leaf nodes, of course, are more than 0 integers, n is the number of leaf nodes, the last two are input parameters.


The process code to construct the Huffman tree is actually very simple:

Constructs the Huffman tree for
	(i = n + 1; I <= m; i++)
	{
		if (! Select (*ht, I-1, &S1, &S2)) return false;
		(*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;
	}

The Select function selects the parent as 0 from the ht[1...nend] and weight the smallest two nodes, ordinal numbers are returned by S1 and S2, which are implemented as follows:

static bool Select (Huffmantree HT, int nend, int *s1, int *s2)
{
	int i = 0;
	int ncomp = 0;
	
	Ncomp = MAX;
	*S1 = MAX;
	*S2 = MAX;

	First round loop, find the smallest give S1 for
	(i = 1; I <= nend i++)
	{
		if (Ht[i].weight < Ncomp) && (ht[i].parent = 0) )
		{
			ncomp = ht[i].weight;
			*S1 = i
		}
			
	}

	Second round cycle, find the second small to s2
	ncomp = MAX;

	for (i = 1; I <= nend i++)
	{
		if (Ht[i].weight < Ncomp) && (i!= *s1) && (ht[i].parent = 0)
		{
			ncomp = ht[i].weight;
			*S2 = i
		}
			
	}
	if ((*s1 = MAX) | | (*s2 = MAX))
	{return
		false;
	}
	return true;
}

The result of encoding is stored in HC, in order to facilitate the use of reverse-saving form, that is, from the leaf to the root of the code, and then output is positive.

for (i = 1; I <= n; i++)
	{
		start = n-1;
		for (c = i, F = (*HT) [I].parent f!= 0; C = f, f = (*ht) [f].parent)
		{
			//left 0 right 1
			if ((*ht) [f].lchild = = c)
			{ C7/>cd[--start] = ' 0 ';
			}
			else
			{
				Cd[--start] = ' 1 ';
			}
		}
		(*HC) [I] = (char *) malloc ((N-start));
		strcpy ((*HC) [i], &cd[start]);
	}


Code Download Address:

http://download.csdn.net/detail/pony_maggie/8209175

Or

Https://github.com/pony-maggie/HuffmanCode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.