【发布时间】:2014-03-19 16:53:34
【问题描述】:
我是 c# 的新手,我刚刚完成了 huffman tree,现在下一步是使其成为 generic,我的意思是 symbol 应该适用于每个 data type。因为我是 c# 初学者,所以我需要一些基本的想法来做到这一点。
我的霍夫曼树由 3 个类组成。类 huffman、node 和 MyClass(包含 main 函数)其中 freq 是 symbol 重复的次数,它们的结构如下所示:
namespace final_version_Csharp
{
public Class Huffman
{
public classNode
{
public Node next, left, right;
public int symbol;
public int freq;
}
public Node root;
}
public void huffman_node_processing()
{
//done the addition of two minimum freq here
}
public void GenerateCode(Node parentNode, string code)
{
//done the encoding work here
}
public class MyClass
{
public static void Main(string[] args)
{
Huffman ObjSym = new Huffman(args); //object creation by reading the data fron a file at sole argument
//All other methods are here
ObjSym.huffman_node_processing(); //this for adding the two minimum nodes
ObjSym.GenerateCode(ObjSym.root, ""); //this for encoding
}
}
}
请有人帮助我使这个“符号”适用于所有数据类型,如“短”、“长”等。
【问题讨论】:
-
当您说通用时,您的意思是在同一棵树中一个节点可以有一个 int 符号和一个字符串符号,或者您是否希望能够创建一个符号始终相同的树(例如字符串)
-
@BobVale 实际上我正在读取一个二进制文件以创建符号的频率(可能是 1110111.. 的形式)。 “符号”必须适用于 1 字节或 2 字节等。你现在明白了吗?
标签: c# generics data-structures generic-programming huffman-code