public HuffmanTree(IDictionary <T, int> frequencyDictionary) { var sortedNodes = new SortedSet <HuffmanNode <T> >(); foreach (var item in frequencyDictionary.OrderBy(x => x.Value)) { var node = new HuffmanNode <T>() { Value = item.Key, Weight = item.Value }; sortedNodes.Add(node); _nodeDictionary.Add(node.Value, node); } while (sortedNodes.Count() > 1) { var left = sortedNodes.Min; sortedNodes.Remove(left); var right = sortedNodes.Min; sortedNodes.Remove(right); var parent = new HuffmanNode <T>() { Value = default(T), Weight = left.Weight + right.Weight, LeftNode = left, RightNode = right }; left.Parent = parent; right.Parent = parent; sortedNodes.Add(parent); } Root = sortedNodes.FirstOrDefault(); }
public int CompareTo(HuffmanNode <T> other) { return(Compare(this, other)); }