private void DumpNode(HuffmanNode node,string tag, int i, TextWriter writer) { for (int j = 0; j < i; j++) { writer.Write(" "); } writer.Write('-'); if (node.IsBranch) writer.Write(" {0} '{2}' x {1:4} = ", tag, node.Freq, (char)node.Symbol); else writer.Write(" {1} Freq {0} = ", node.Freq, tag); if (node.IsBranch == false) { foreach (var bit in node.Bits) { writer.Write(bit ? "1" : "0"); } } writer.WriteLine(); if (node.IsBranch == false) return; DumpNode(node.Left, "left", i+1, writer); DumpNode(node.Right, "right", i + 1, writer); }
public HuffmanTable Build() { var leaves = new Queue<HuffmanNode>(_leaves.Values.OrderBy(node => node.Freq == 0 ? 1 : node.Freq)); var branches = new Queue<HuffmanNode>(); while (leaves.Count + branches.Count > 1) { Queue<HuffmanNode> q1 = null, q2 = null; int candidateWeight = int.MaxValue; if (leaves.Count > 0 && branches.Count > 0) { var weight = leaves.Peek().Freq + branches.Peek().Freq; if (weight < candidateWeight) { candidateWeight = weight; q1 = leaves; q2 = branches; } } if (leaves.Count > 1) { var weight = leaves.Peek().Freq + leaves.Peek().Freq; if (weight < candidateWeight) { candidateWeight = weight; q1 = q2 = leaves; } } if (branches.Count > 1) { var weight = branches.Peek().Freq + branches.Peek().Freq; if (weight< candidateWeight) { candidateWeight = weight; q1 = q2 = branches; } } System.Diagnostics.Debug.Assert(q1 != null && q2 != null); var left = q1.Dequeue(); var right = q2.Dequeue(); var parent = new HuffmanNode(-1, candidateWeight) { Left = left, Right = right }; left.Parent = parent; right.Parent = parent; branches.Enqueue(parent); } foreach (var huffmanNode in _leaves.Values) { huffmanNode.SetupBitPattern(); } return new HuffmanTable(_leaves, branches.Dequeue()); }
public static HuffmanNode Load(BinaryReader reader, Dictionary<int, HuffmanNode> leaves) { var branch = reader.ReadBoolean(); if (branch == false) { var huffmanNode = new HuffmanNode(BinaryWriterExtensions.Read7BitEncodedInt(reader), -2/* we don't actually need this, so we don't save it*/); leaves.Add(huffmanNode.Symbol, huffmanNode); return huffmanNode; } var left = Load(reader, leaves); var right = Load(reader,leaves); var parent = new HuffmanNode(-1, -2) { Left = left, Right = right }; left.Parent = parent; right.Parent = parent; return parent; }
public static HuffmanNode Load(BinaryReader reader, Dictionary <int, HuffmanNode> leaves) { var branch = reader.ReadBoolean(); if (branch == false) { var huffmanNode = new HuffmanNode(BinaryWriterExtensions.Read7BitEncodedInt(reader), -2 /* we don't actually need this, so we don't save it*/); leaves.Add(huffmanNode.Symbol, huffmanNode); return(huffmanNode); } var left = Load(reader, leaves); var right = Load(reader, leaves); var parent = new HuffmanNode(-1, -2) { Left = left, Right = right }; left.Parent = parent; right.Parent = parent; return(parent); }
public HuffmanTable(Dictionary <int, HuffmanNode> leaves, HuffmanNode root) { _leaves = leaves; _root = root; }
public HuffmanTable(Dictionary<int, HuffmanNode> leaves, HuffmanNode root) { _leaves = leaves; _root = root; }
public void Add(int symbol, int freq) { var node = new HuffmanNode(symbol, freq); _leaves.Add(symbol, node); }