# Cod sursa(job #2207171)

Utilizator Data 25 mai 2018 00:12:45 Coduri Huffman 70 cpp done Arhiva educationala 2.3 kb
``````#include <iostream>
#include <fstream>
#include <queue>
#include <string>
#define NMAX 1000005

using namespace std;

ifstream f("huffman.in");
ofstream g("huffman.out");

//queue< long > Q, newQ;
long Q[NMAX], newQ[NMAX], sizeQ = 0, sizeNewQ = 0, startQ = 0, startNewQ = 0;

struct Node
{
long long value;
long next[2];
}node[2*NMAX];

long n, k, root, level[2 * NMAX];
long long cod[2 * NMAX], lg;

void calcLevel(long k, long nivel, long long c)
{
level[k] = nivel;
cod[k] = c;
if (node[k].next[0] != 0)
{
calcLevel(node[k].next[0], nivel + 1, 2 * c);
calcLevel(node[k].next[1], nivel + 1, 2 * c + 1);
}
}

void solve()
{
k = n;

//while (Q.size() + newQ.size() >= 2)
while (sizeQ + sizeNewQ - startQ - startNewQ >= 2)
{
long x1, x2;

//if (!newQ.empty() && (Q.empty() || node[Q.front()].value >  node[newQ.front()].value))
if (sizeNewQ > startNewQ && (sizeQ <= startQ || node[Q[startQ]].value > node[newQ[startNewQ]].value))
{
//x1 = newQ.front();
x1 = newQ[startNewQ++];
//newQ.pop();
}
else
{
//x1 = Q.front();
//Q.pop();
x1 = Q[startQ++];
}

//if (!newQ.empty() && (Q.empty() || node[Q.front()].value >  node[newQ.front()].value))
if (sizeNewQ > startNewQ && (sizeQ <= startQ || node[Q[startQ]].value > node[newQ[startNewQ]].value))
{
x2 = newQ[startNewQ++];
}
else
{
x2 = Q[startQ++];
}

k++;
node[k].value = node[x1].value + node[x2].value;
lg += node[k].value;
node[k].next[0] = x1;
node[k].next[1] = x2;
//newQ.push(k);
newQ[sizeNewQ++] = k;
}
root = k;
}

{
f >> n;
for (int i = 1; i <= n; i++)
{
f >> node[i].value;
node[i].next[0] = node[i].next[1] = 0;
//Q.push(i);
Q[i] = i;
}
startQ = 1;
sizeQ = n + 1;
startNewQ = 1;
sizeNewQ = 1;
}

int main()
{