need an idea to break through

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <iostream.h>
#include <stdio.h>
#include <conio.h>
const int maxsize = 100;
const int null = 0;

struct huff_node{
char symbol;
int freq;
huff_node *parent;
char childtype;
};

typedef huff_node *ptr;
ptr node[maxsize];

void create(int k); 
void print(int k); 
void twosmall(ptr &p, ptr &q, int numnode); 

int main()
{
int numsymbols;
ptr p, q, r;
cout << "Introduce el numero de simbolos: ";
cin >> numsymbols;
for (int i = 0; i < numsymbols; i++)
create(i);


for (int j = numsymbols; j < 2*numsymbols - 1; j++)
{
r = new huff_node;
node[j] = r;
r->parent = null;
twosmall(p, q, j); 
p->parent = r;
q->parent = r;
p->childtype = 'L';
q->childtype = 'R';
r->symbol = ' ';
r->freq = p->freq + q->freq;
}
cout << endl << endl;
cout <<"simbolo *-------* codigo: " << endl;
for (int k = 0; k < numsymbols; k++)
print(k);
getch();
return 0;
}

void create(int k)
{
ptr t = new huff_node;
cout << "introduce el simbolo numero " << k+1 << ": ";
cin >> t->symbol;
cout << "introduce su frecuencia: ";
cin >> t->freq;
t->parent = null;
node[k] = t;
}

void print(int k)
{
ptr t = node[k];
char code[10];
int size = 0;
cout << t->symbol << " - ";

while (t->parent != null)
{
if (t->childtype == 'L')
code[size] = '0';
else
code[size] = '1';
t = t->parent;
size++;
}

for (int j = size-1; j >= 0; j--)
cout << code[j];
cout << endl;
}

void twosmall(ptr &p, ptr &q, int numnodes)
{
int min1 = 9999;
int min2 = 9999;
p = null;
q = null;

for (int i = 0; i < numnodes; i++)
{
if (node[i]->parent == null)
{
if (node[i]->freq < min1)
{
min2 = min1;
min1 = node[i]->freq;
q = p;
p = node[i];
}
else
if (node[i]->freq < min2)
{
min2 = node[i]->freq;
q = node[i];
}
}
}
}


this code here makes the huffman encoding. Every step is to create the node and link it to an array that will represent my tree, and then at each iteration seeks the 2 lowest probability nodes and creates a new one whose probability is the sum of the 2 lowest

is there any way to start decoding using the same tree? if so, how can i start?


thanks!
Topic archived. No new replies allowed.