public Symbol(Symbol firstTerm, Symbol secondTerm) { if (firstTerm == null || secondTerm == null) { throw new ArgumentNullException("firstTerm"); } Frequency = firstTerm.Frequency + secondTerm.Frequency; if (firstTerm?.Frequency > secondTerm?.Frequency) { RightChild = firstTerm; LeftChild = secondTerm; } else { LeftChild = firstTerm; RightChild = secondTerm; } }
public void MakeTableAndTree() { Queue<Symbol> currentQueue = new Queue<Symbol>(); Ensemble.OrderBy(o => o.Frequency).ToList().ForEach(p => currentQueue.Enqueue(p)); ProbabilityTable.Add(currentQueue.ToList()); while (currentQueue.Count > 1) { var newNode = new Symbol(currentQueue.Dequeue(), currentQueue.Dequeue()); var temporaryQueue = currentQueue.ToList(); if (currentQueue.Count == 0) { currentQueue.Enqueue(newNode); } else { int index = 0; currentQueue.ToList().ForEach(p => { if (newNode.Frequency <= p.Frequency) { index = currentQueue.ToList().IndexOf(p); } else { index++; } }); temporaryQueue.Insert(index, newNode); currentQueue.Clear(); temporaryQueue.ForEach(p => currentQueue.Enqueue(p)); } ProbabilityTable.Add(currentQueue.ToList()); } }