using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; namespace haffman { class Program { public static List<Entry> CountingRepetition(string str, int param = 1) { var result = new List<Entry>(); for (var i = 0; i <= (str.Length - param); i++) { var currentSegment = str.Substring(i, param); if (!result.Any(x => x.Data == currentSegment)) { result.Add(new Entry { Data = currentSegment, Count = new Regex(Regex.Escape(currentSegment)).Matches(str).Count }); } } return result; } static void DisplayTable(List<Entry> param) { int sum = 0; foreach (var disp in param) { Console.Write(disp.Data + "\t"); } Console.Write("\n"); foreach (var disp in param) { Console.Write(disp.Count + "\t"); sum += disp.Count; } Console.Write("\n"); foreach (var disp in param) { Console.Write($"{disp.Count}/{sum}" + "\t"); } } public static List<Entry> BuildThree(List<Entry> param) { // List<Entry> result = new List<Entry>(); var sorted = param.OrderBy(u => u.Count); for (int i = 0; i < sorted.Count(); i++) { for (int j = i+1; j <= i+1; j++) { if (param[i].Count >= param[j].Count) { Entry result = new Entry { Data = "*", Count = param[i].Count + param[j].Count, LeftSymbol = param[i].Data, RightSymbol = param[j].Data, LeftByte = 1, RightByte = 0 }; param.Remove(param[i]); j = 0; param.Remove(param[j]); param.Insert(0, result); j++; } else if (param[i].Count < param[j].Count) { Entry result = new Entry { Data = "*", Count = param[i].Count + param[j].Count, LeftSymbol = param[j].Data, RightSymbol = param[i].Data, LeftByte = 0, RightByte = 1 }; param.Remove(param[i]); j = 0; param.Remove(param[j]); param.Insert(0,result); } } i = -1; } return param; } static void Main(string[] args) { Console.Write("Исходная строка: "); string sourceString = Console.ReadLine(); //Подсчет кол-ва повторяющихся символов var countValues = CountingRepetition(sourceString, 1); Console.WriteLine("H(X)"); DisplayTable(countValues); // BuildThree(countValues); Console.ReadLine(); } } class Entry { public string Data { get; set; } public int Count { get; set; } public string LeftSymbol { get; set; } public string RightSymbol { get; set; } public byte LeftByte { get; set; } public byte RightByte { get; set; } } } The written tree building function does not work correctly, you could tell what I'm doing wrong, thanks in advance!
The problem is the param collection in which the tree is created. With the first two expressions, everything is fine. As soon as I try to add a new value to the created branch, Entry, Entry ... is displayed in the left symbol ...