Haffman.cpp:Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdio.h>
#include <string.h>
typedef char DataType;
struct element//node definition
{
DataType data;
Float weight;//The weight of this character
int Parent,lchild, rchild;//parent node, left child, right child storage location
};
#define MAXLEAF 8//maximum number of leaf nodes, number of characters to encode
#define MAXNODE maxleaf*2-1//Maximum node count
struct huffmancode{
DataType ch;//Store characters
Char bits[maxleaf];//a Huffman encoding for storing characters
};
Huffmancode Hcode[maxleaf];
Element ht[Maxnode];
This function is to select the location of the two smallest weight nodes, respectively, in pn[0],pn[1]
void Select (element *pt,int n, int *pn) {
int i,iposi=0;
float tmp;
for (i=0;i<n;i++) {
if (pt[i].parent==-1)
{
Tmp=pt[i].weight;pn[0]=i;
Iposi=i;
Break
}
}
for (i=iposi;i<n;i++) {
if (tmp>pt[i].weight && pt[i].parent==-1) {
Pn[0]=i; Tmp=pt[i].weight;
}
}
for (i=0;i<n;i++) {
if (Pt[i].parent==-1 && i!=pn[0])
{
Tmp=pt[i].weight;pn[1]=i;
Iposi=i;
Break
}
}
for (i=iposi;i<n;i++) {
if (tmp>pt[i].weight && pt[i].parent==-1 && i!=pn[0]) {
Pn[1]=i; Tmp=pt[i].weight;
}
}
Return
}
This function functions to create a Huffman tree
void Createhuffmantree (element *pt) {
int i,k=0;
int pn[2];
for (i=maxleaf; i<maxnode;i++) {
Select the location of the two smallest weight nodes, respectively, in pn[0],pn[1]
Select (PT,MAXLEAF+K,PN);
k++;
pt[pn[0]].parent=pt[pn[1]].parent=i;
PT[I].LCHILD=PN[0]; PT[I].RCHILD=PN[1];
Pt[i].weight=pt[pn[0]].weight+pt[pn[1]].weight;
}
}
This function functions to generate Huffman code
void Createhuffmancode (element *pt,int N) {
int I,p,j,start;
Char Cd[maxnode];
for (i=0;i<n;i++) {
start=n-1;
cd[start]=0;
P=pt[i].parent;
J=i;
From the leaf knot point, step by step to the root node, in reverse order to find the Huffman code of each node
while (p!=-1) {//when P is 1, it means traversing to the root node.
if (PT[P].LCHILD==J)
cd[--start]= ' 0 ';//left child coded to 0
Else
cd[--start]= ' 1 '; Right child coded to 1
I=s;
P=pt[p].parent;
}
strcpy (Hcode[i].bits,&cd[start]);
}
}
int main (int argc, char* argv[])
{
Element Ht[maxnode];
int i;
for (i=0;i<maxnode;i++) {
Ht[i].parent=-1;
Ht[i].lchild=-1;
Ht[i].rchild=-1;
Ht[i].data= ";
ht[i].weight=0;
}
Ht[0].data= ' A '; ht[0].weight=2; Hcode[0].ch=ht[0].data;
Ht[1].data= ' B '; ht[1].weight=4; Hcode[1].ch=ht[1].data;
Ht[2].data= ' C '; ht[2].weight=5; Hcode[2].ch=ht[2].data;
Ht[3].data= ' D '; ht[3].weight=3; Hcode[3].ch=ht[3].data;
H[0].data= ' A '; ht[0].weight=28; Hcode[0].ch=ht[0].data;
Ht[1].data= ' B '; ht[1].weight=13; Hcode[1].ch=ht[1].data;
Ht[2].data= ' C '; ht[2].weight=30; Hcode[2].ch=ht[2].data;
Ht[3].data= ' D '; ht[3].weight=10; Hcode[3].ch=ht[3].data;
Ht[4].data= ' E '; ht[4].weight=12; Hcode[4].ch=ht[4].data;
Ht[5].data= ' F '; ht[5].weight=7; Hcode[5].ch=ht[5].data;
Createhuffmantree (HT);//Generate Huffman Tree
Createhuffmancode (ht,maxleaf);//Generate Huffman code
Output encoding for each character
float weight=0;
for (i=0;i<maxleaf;i++) {
Weight +=ht[i].weight*strlen (hcode[i].bits);
printf ("Character =%c weights =%f encoded =%s\n", hcode[i].ch, ht[i].weight,hcode[i].bits);
}
printf ("weight=%f\n", weight);
return 0;
}
Tenth to tenth week data structure code