Answer the question
In order to leave comments, you need to log in
How to write/read a tree from a file?
I am writing an algorithm for compressing a text document using the Huffman and Shannon-Fano methods.
Almost everything is ready - that is, a tree is created, codes for each character are created from it, and the text is encoded with their help.
It remains only to somehow write down and read the tree, according to which it will be possible to decrypt the message ..
Here is the structure for Shannon-Fano:
struct spisok//список
{
char ch;//символ
string code;//код
double k;//вероятность
spisok *next;
}*myspisok=NULL;
struct tree //дерево
{
tree *left,*right;
spisok *s;//содержит в себе список
}*mytree=NULL;
struct spisok { //список деревьев FailFish
char simvol;
string code;
int kol;
spisok *next;
spisok *prev;
spisok *left;
spisok *right;
}*head,*sortlist,*tree;
for(int i=0;i<final_code.length();i++)
{
if (k==size)//как только количество символов в расшифрованном файле будет равно количеству символов в исходном
{
break;
}
if (t->left == NULL && t->right == NULL)//дошли до листа
{
fwrite(&(t->s->ch),sizeof(char),1,decrypted);//записываем символ из листа
cout<<t->s->ch;//выводим его
t=head;//поднимаемся назад к голове
k++;
}
if (final_code[i]=='1') t=t->right;//если 1 - идем вправо
else t=t->left;//иначе - влево
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question