سوال:
سوال:
سلام.
من یه برنامه ای برای کد هافمن پیدا کردم و الآن چند روزه که باهاش کلنجار میرم ولی از توابعش اصلا سر در نیاوردم.
اگه امکان داشته باشه تو این زمینه کمکم کنید.
ممنون.
#include <conio.h>
#include <stdio.h>
#include <iostream.h>
#include <string.h>
#include <stdlib.h>
struct treenode{
int data;
int num;
treenode *next;
treenode *right;
treenode *left;
}*start=NULL,*end=NULL,*p;
struct code{
char bcode[12];
int ch;
}coding[256];
struct tree{
int data;
int num;
}atree[512];
int o=0;
int search(int i);
void insert(int i);
void insertm(struct treenode *l);
void sort();
void create();
void binary(struct treenode *n,char byte[],int i);
void tree(struct treenode *n,int i);
int numnode=0;
int numchar=0;
char filename[25];
void main()
{
clrscr();
FILE *fp1;
cout<<"\n Enter The Location And Name Of The Source File To Compress : ";
cin>>filename;
fp1=fopen(filename,"r+b");
if (fp1==NULL)
{
cout<<"\n Error In Open File...";
getch();
exit(1);
}
int i=getc(fp1);
while (i!=-1)
{
numchar++;
if (search(i)==-1)
insert(i);
i=getc(fp1);
}
fclose(fp1);
sort();
create();
char byte[20]={0};
binary(start,byte,0);
tree(start,1);
getch();
}
//*************************search******************************
int search(int i)
{
struct treenode *h;
h=start;
while(h)
{
if (h->data==i)
{
h->num=h->num+1;
return 0;
}
h=h->next;
}
return -1;
}
//**************************insert******************************
void insert(int i)
{
p=new treenode;
p->data=i;
p->num=1;
p->right=p->left=p->next=NULL;
if (start==NULL)
{
start=p;
end=p;
}
else
{
end->next=p;
end=p;
} }
//******************************Sort*******************
void sort()
{
struct treenode *h1,*h2,*n2,*right,*left;
int d,n;
n2=start;
while(n2)
{ h1=start;
h2=start->next;
while(h2)
{
if (h1->num > h2->num)
{
d=h1->data;
n=h1->num;
right=h1->right;
left=h1->left;
h1->data=h2->data;
h1->num=h2->num;
h1->right=h2->right;
h1->left=h2->left;
h2->data=d;
h2->num=n;
h2->right=right;
h2->left=left;
}
h1=h2;
h2=h2->next;
}
n2=n2->next;
} }
//****************Create*******************************
void create()
{
struct treenode *h;
while (start->next)
{
p=new treenode;
p->left=start;
p->right=start->next;
p->num=start->num+start->next->num;
p->data=-1;
p->next=NULL;
h=start;
start=start->next->next;
h->next->next=NULL;
h->next=NULL;
insertm(p);
sort();
}}
//********************Insertm****************************
void insertm(struct treenode *l)
{
if (start==NULL)
{
start=l;
end=l;
}
else
{
end->next=l;
end=l;
} }
//********************Binary****************************
void binary(struct treenode *n,char byte[],int i)
{
if (n->data==-1)
{
char b1[20]={0},b2[20]={0};
strcpy(b1,byte);
strcpy(b2,byte);
b1='1';
binary(n->right,b1,i+1);
b2='0';
binary(n->left,b2,i+1);
}
else
{
cout<<"Char :"<<(char)n->data<<" Byte : "<<byte<<"\n";
strcpy(coding[o].bcode,byte);
coding[o++].ch=n->data;
}
}
//*********************************************************
void tree(struct treenode *n,int i)
{
atree.data=n->data;
atree.num=n->num;
numnode=i;
if (n->left!=NULL)
tree(n->left,2*i);
if (n->right!=NULL)
tree(n->right,2*i+1);
}