# GÓC SÁNG TẠO > Khu vực lập trình > .NET >  Help me ! Cài đặt thuật toán mã hóa huffman

## chucvn

Mình đang muốn cài đặt thuật toán huffman trên C#.
Mình có tham khảo trong cuốn  >> Data Structures And Algorithms Using C# << ,nhưng đoạn code có rất nhiều lỗi . Rất mong các bạn sửa giúp mình ... Xin cảm ơn rất nhiều !

*Node.cs*

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Huffman1
{
class Node
{
HuffmanTree data;
Node link;
public Node(HuffmanTree newData)
{
data = newData;
}
}
} 
*HuffmanTree.cs*

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Huffman1
{
class HuffmanTree
{
private HuffmanTree leftChild;
private HuffmanTree rightChild;
private string letter;
private int freq;

public HuffmanTree()
{
this.letter = letter;
}

public void SetLeftChild(HuffmanTree newChild)
{
leftChild = newChild;
}

public void SetRightChild(HuffmanTree newChild)
{
rightChild = newChild;
}

public void SetLetter(string newLetter)
{
letter = newLetter;
}

public void IncFreq()
{
freq++;
}

public void SetFreq(int newFreq)
{
freq = newFreq;
}

public HuffmanTree GetLeftChild()
{
return leftChild;
}

public HuffmanTree GetRightChild()
{
return rightChild;
}

public int GetFreq()
{
return freq;
}
}
} 
*TreeList.cs*

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Huffman1
{
class TreeList
{
private int count = 0;
Node first;
public void AddLetter(string letter)
{
HuffmanTree hTemp = new HuffmanTree();
Node eTemp = new Node(hTemp);
if (first == null)
first = eTemp;
else
{
eTemp.link = first;
first = eTemp;
}
count++;
}

public void SortTree() 
{
TreeList otherList = new TreeList();
HuffmanTree aTemp;
while (!(this.first == null)) {
aTemp = this.RemoveTree();
otherList.InsertTree(aTemp);
}
this.first = otherList.first;
}


public void MergeTree() 
{
if (!(first == null))
if (!(first.link == null)) 
{
HuffmanTree aTemp = RemoveTree();
HuffmanTree bTemp = RemoveTree();
HuffmanTree sumTemp = new HuffmanTree();
sumTemp.SetLeftChild(aTemp);
sumTemp.SetRightChild(bTemp);
sumTemp.SetFreq(aTemp.GetFreq() + bTemp.GetFreq());
InsertTree(sumTemp);
}
}

public HuffmanTree RemoveTree()
{
if (!(first == null))
{
HuffmanTree hTemp;
hTemp = first.data;
first = first.link;
count--;
return hTemp;
}
return null;
}


public void InsertTree(HuffmanTree hTemp) 
{
Node eTemp = new Node(hTemp);
if (first == null)
first = eTemp;
else 
{
Node p = first;
while (!(p.link == null)) 
{
if ((p.data.GetFreq()<= hTemp.GetFreq()) && (p.link.data.GetFreq() >= hTemp.GetFreq()))
break;
p = p.link;
}
eTemp.link = p.link;
p.link = eTemp;
}
count++; 
}
public int Length()
{
return count;
}

}
} 
*Program.cs*

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Huffman1
{
class Program
{
static void Main(string[] args)
{
string input;

Console.Write("Enter a string to encode: ");
input = Console.ReadLine();

TreeList treeList = new TreeList();
for(int i = 0; i < input.Length; i++)
treeList.AddLetter(input.Chars(i));

//treeList.AddSign(input.Chars(i));
treeList.SortTree();


int[] signTable = new int[input.Length];
int[] keyTable = new int[input.Length];

while (treeList.Length() > 1) treeList.MergeTree();
MakeKey(treeList.RemoveTree(), "");
string newStr = translate(input, signTable, keyTable);

for(int i = 0; i <= signTable.Length - 1; i++)
Console.WriteLine(signTable_ + ": " + keyTable);
Console.WriteLine("The original string is " + input.Length * 16 + " bits long.");
Console.WriteLine("The new string is " + newStr.Length + " bits long.");
Console.WriteLine("The coded string looks like this:" + newStr);
}

static string translate(string original, int[] signTable, int[] keyTable) 
{
string newStr = "";
for(int i = 0; i <= original.Length-1; i++)
for(int j = 0; j <= signTable.Length-1; j++)
if (original.Chars(i) == signTable[j])
newStr += keyTable[j];
return newStr;
}
static void MakeKey(HuffmanTree tree, string code)
{
int pos = 0;
if (tree.GetLeftChild == null)
{
signTable[pos] = tree.GetSign();
keyTable[pos] = code;
pos++;
}
else
{
MakeKey(tree.GetLeftChild, code + "0");
MakeKey(tree.GetRightChild, code + "1");
}
}

}
} 
[IMG]data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAA  l21bKAAAAA1BMVEXh5PJm+yKVAAAAAXRSTlMAQObYZgAAAApJR  EFUCNdjYAAAAAIAAeIhvDMAAAAASUVORK5CYII=[/IMG]_

----------

