/// <summary> /// Последовательное преобразование всех узлов из currentNodes в дерево кодирования /// </summary> private void BuildTree() { while (currentNodes.Count > 1) { CreateNewNode(); } root = currentNodes[0]; }
public CodeTreeNode(double frequency, string codeLetter, CodeTreeNode leftLink, CodeTreeNode rightLink) { this.Frequency = frequency; this.Letter = codeLetter; this.leftLink = leftLink; this.rightLink = rightLink; this.Code = ""; }
public CodeTreeNode(double frequency, CodeTreeNode leftLink, CodeTreeNode rightLink) { this.Frequency = frequency; this.Letter = null; this.leftLink = leftLink; this.rightLink = rightLink; this.Code = ""; }
/// <summary> /// Создание нового узла из двух с минимальной суммарной частотой /// </summary> private void CreateNewNode() { currentNodes.Sort(new FrequencyComparer()); double newFrequency = currentNodes[0].Frequency + currentNodes[1].Frequency; var newNode = new CodeTreeNode(newFrequency, currentNodes[1], currentNodes[0]); currentNodes.RemoveAt(0); currentNodes.RemoveAt(0); currentNodes.Add(newNode); }
/// <summary> /// Рекурсивный обход дерева с записью кода в каждый узел lastNodes /// </summary> /// <param name="node"></param> /// <param name="code"></param> private void GenerateCodes(CodeTreeNode node, string code) { if (node.leftLink == null && node.rightLink == null) { return; } else { string leftCode = code + "0"; string rightCode = code + "1"; node.leftLink.Code = leftCode; node.rightLink.Code = rightCode; GenerateCodes(node.leftLink, leftCode); GenerateCodes(node.rightLink, rightCode); } }