今天接触哈弗曼函数,对于一些代码自己实现还是比较困难的,算法思想还是很简单的,但是一些相关的代码实现自己仍不能实现,所以,比较纠结!!弄了一天还是出不来,所以,还是自己想想办法,看看网上的了,不过看到一些关于文件存储的方式,感觉好像没这个必要,我再研究研究,争取明天更新博客的时候,将代码奉上!!
C语言:
Codee#11683
#include <stdio.h>
#include<malloc.h>
#include <string.h>
struct node
{
int weight;
int parent , ilchid , rchild;
};
typedef struct node haffman , * haffmantree;
typedef char * haffmancode;
void select( haffmantree * ht , int n , int * s1 , int * s2)
{
int i;
int min;
for( i = 1; i <=n; i ++)
{
if(( * ht )[ i ]. parent == 0)
{
min = i;
break;
}
}
for( i = 1; i <=n; i ++)
{
if(( * ht )[ i ]. parent == 0 &&( * ht )[ min ]. weight >( * ht )[ i ]. weight)
{
min = i;
}
}
* s1 = min;
for( i = 1; i <=n; i ++)
{
if(( * ht )[ i ]. parent == 0 && i !=* s1)
{
min = i;
break;
}
}
for( i = 1; i <=n; i ++)
{
if(( * ht )[ i ]. parent == 0 &&( * ht )[ min ]. weight >( * ht )[ i ]. weight && i !=* s1)
{
min = i;
}
}
* s2 = min;
}
void createhaffmantree( haffmantree * ht , int * w , int n)
{
int m , i , s1 , s2;
m = 2 *n - 1;
* ht =( haffmantree) malloc( sizeof( struct node));
for( i = 1; i <=n; i ++)
{
( * ht )[ i ]. weight = w [ i ];
( * ht )[ i ]. rchild = 0;
( * ht )[ i ]. ilchid = 0;
( * ht )[ i ]. parent = 0;
}
for( i =n + 1; i <= m; i ++)
{
( * ht )[ i ]. weight = 0;
( * ht )[ i ]. ilchid = 0;
( * ht )[ i ]. rchild = 0;
( * ht )[ i ]. parent = 0;
}
printf( " /n 哈弗曼树为: /n ");
for( i =n + 1; i <= m; i ++)
{
select( ht , i - 1 , & s1 , & s2);
( * ht )[ s1 ]. parent = i;
// (*ht)[i].ilchid=s1;
( * ht )[ s2 ]. parent = i;
// (*ht)[i].rchild=s2;
( * ht )[ i ]. weight =( * ht )[ s1 ]. weight +( * ht )[ s2 ]. weight;
printf( "%d (%d,%d) /n " ,( * ht )[ i ]. weight ,( * ht )[ s1 ]. weight ,( * ht )[ s2 ]. weight);
}
printf( " /n ");
}
void crthaffmantree( haffmantree * ht , haffmantree * hc , int n)
{
char * cd;
int a [ 100 ];
int i , start ,p , w = 0;
int c;
hc =( haffmancode *) malloc((n + 1) * sizeof( char *));
cd =( char *) malloc(n * sizeof( char));
cd [n - 1 ] = '/0';
for( i = 1; i <=n; i ++)
{
a [ i ] = 0;
start =n - 1;
for( c = i ,p =( * ht )[ i ]. parent;p != 0; c =p ,p =( * ht )[p ]. parent)
{
if(( * ht )[p ]. ilchid == c)
{
cd [ -- start ] = '1';
a [ i ] ++;
}
else
{
cd [ -- start ] = '0';
a [ i ] ++;
}
}
hc [ i ] =( char *) malloc((n - start) * sizeof( char));
strcpy( hc [ i ], & cd [ start ]);
}
free( cd);
for( i = 1; i <=n; i ++)
{
printf( "权值为%d的哈弗曼编码为:%s /n " ,( * ht )[ i ]. weight , hc [ i ]);
}
for( i = 1; i <=n; i ++)
{
w +=( * ht )[ i ]. weight * a [ i ];
}
printf( "带权路径为:%d /n " , w);
}
int main()
{
int i;
haffmantree ht;
haffmantree hc;
int * w ,n , wei;
printf( "**哈弗曼编码** /n ");
printf( "请输入结点的个数:");
scanf( "%d" , &n);
w =( int *) malloc( sizeof( int) *(n + 1));
printf( "请输入%d个点的权值 /n " ,n);
for( i = 1; i <=n; i ++)
{
printf( "%d:" , i);
getchar();
scanf( "%d" , & wei);
w [ i ] = wei;
}
createhaffmantree( & ht , w ,n);
crthaffmantree( & ht , & hc ,n);
}
#include<malloc.h>
#include <string.h>
struct node
{
int weight;
int parent , ilchid , rchild;
};
typedef struct node haffman , * haffmantree;
typedef char * haffmancode;
void select( haffmantree * ht , int n , int * s1 , int * s2)
{
int i;
int min;
for( i = 1; i <=n; i ++)
{
if(( * ht )[ i ]. parent == 0)
{
min = i;
break;
}
}
for( i = 1; i <=n; i ++)
{
if(( * ht )[ i ]. parent == 0 &&( * ht )[ min ]. weight >( * ht )[ i ]. weight)
{
min = i;
}
}
* s1 = min;
for( i = 1; i <=n; i ++)
{
if(( * ht )[ i ]. parent == 0 && i !=* s1)
{
min = i;
break;
}
}
for( i = 1; i <=n; i ++)
{
if(( * ht )[ i ]. parent == 0 &&( * ht )[ min ]. weight >( * ht )[ i ]. weight && i !=* s1)
{
min = i;
}
}
* s2 = min;
}
void createhaffmantree( haffmantree * ht , int * w , int n)
{
int m , i , s1 , s2;
m = 2 *n - 1;
* ht =( haffmantree) malloc( sizeof( struct node));
for( i = 1; i <=n; i ++)
{
( * ht )[ i ]. weight = w [ i ];
( * ht )[ i ]. rchild = 0;
( * ht )[ i ]. ilchid = 0;
( * ht )[ i ]. parent = 0;
}
for( i =n + 1; i <= m; i ++)
{
( * ht )[ i ]. weight = 0;
( * ht )[ i ]. ilchid = 0;
( * ht )[ i ]. rchild = 0;
( * ht )[ i ]. parent = 0;
}
printf( " /n 哈弗曼树为: /n ");
for( i =n + 1; i <= m; i ++)
{
select( ht , i - 1 , & s1 , & s2);
( * ht )[ s1 ]. parent = i;
// (*ht)[i].ilchid=s1;
( * ht )[ s2 ]. parent = i;
// (*ht)[i].rchild=s2;
( * ht )[ i ]. weight =( * ht )[ s1 ]. weight +( * ht )[ s2 ]. weight;
printf( "%d (%d,%d) /n " ,( * ht )[ i ]. weight ,( * ht )[ s1 ]. weight ,( * ht )[ s2 ]. weight);
}
printf( " /n ");
}
void crthaffmantree( haffmantree * ht , haffmantree * hc , int n)
{
char * cd;
int a [ 100 ];
int i , start ,p , w = 0;
int c;
hc =( haffmancode *) malloc((n + 1) * sizeof( char *));
cd =( char *) malloc(n * sizeof( char));
cd [n - 1 ] = '/0';
for( i = 1; i <=n; i ++)
{
a [ i ] = 0;
start =n - 1;
for( c = i ,p =( * ht )[ i ]. parent;p != 0; c =p ,p =( * ht )[p ]. parent)
{
if(( * ht )[p ]. ilchid == c)
{
cd [ -- start ] = '1';
a [ i ] ++;
}
else
{
cd [ -- start ] = '0';
a [ i ] ++;
}
}
hc [ i ] =( char *) malloc((n - start) * sizeof( char));
strcpy( hc [ i ], & cd [ start ]);
}
free( cd);
for( i = 1; i <=n; i ++)
{
printf( "权值为%d的哈弗曼编码为:%s /n " ,( * ht )[ i ]. weight , hc [ i ]);
}
for( i = 1; i <=n; i ++)
{
w +=( * ht )[ i ]. weight * a [ i ];
}
printf( "带权路径为:%d /n " , w);
}
int main()
{
int i;
haffmantree ht;
haffmantree hc;
int * w ,n , wei;
printf( "**哈弗曼编码** /n ");
printf( "请输入结点的个数:");
scanf( "%d" , &n);
w =( int *) malloc( sizeof( int) *(n + 1));
printf( "请输入%d个点的权值 /n " ,n);
for( i = 1; i <=n; i ++)
{
printf( "%d:" , i);
getchar();
scanf( "%d" , & wei);
w [ i ] = wei;
}
createhaffmantree( & ht , w ,n);
crthaffmantree( & ht , & hc ,n);
}