public HuffmanTree(ByteCount[] bytesFreqs)
{
var nodes = new List<HuffmanTreeNode>();
for (int i = 0; i < bytesFreqs.Length; i++)
nodes.Add(new HuffmanTreeNode
{
ByteCount = new ByteCount
{
Byte = bytesFreqs[i].Byte,
Count = bytesFreqs[i].Count
}
});
HuffmanTreeNode left, right;
while (nodes.Count > 1)
{
int min = nodes[0].ByteCount.Count;
int minInd = 0;
for (int j = 1; j < nodes.Count; j++)
if (min > nodes[j].ByteCount.Count)
{
min = nodes[j].ByteCount.Count;
minInd = j;
}
left = nodes[minInd];
nodes.RemoveAt(minInd);
min = nodes[0].ByteCount.Count;
minInd = 0;
for (int j = 1; j < nodes.Count; j++)
if (min > nodes[j].ByteCount.Count)
{
min = nodes[j].ByteCount.Count;
minInd = j;
}
right = nodes[minInd];
nodes.RemoveAt(minInd);
nodes.Add(new HuffmanTreeNode(left, right));
}
Root = nodes[0];
CalculateBytes();
}