【问题标题】:Tree creation using generics in c#在 C# 中使用泛型创建树
【发布时间】:2014-03-19 16:53:34
【问题描述】:

我是 c# 的新手,我刚刚完成了 huffman tree,现在下一步是使其成为 generic,我的意思是 symbol 应该适用于每个 data type。因为我是 c# 初学者,所以我需要一些基本的想法来做到这一点。

我的霍夫曼树由 3 个类组成。类 huffman、node 和 MyClass(包含 main 函数)其中 freqsymbol 重复的次数,它们的结构如下所示:

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


【解决方案1】:

如果我对你的理解正确,你基本上会做类似的事情

namespace final_version_Csharp
{
    public Class Huffman<K> where K :  IComparable<K>
    {
        public classNode<K> 
        {
            public Node next, left, right;
            public K symbol;
            public int freq;
        }
        public Node root;
    }
...
    public class MyClass 
    {
        public static void Main(string[] args) 
          {
            Huffman ObjSym = new Huffman<int>(); 
            //All other methods are here
            ObjSym.huffman_node_processing(); //this for adding the two minimum nodes
            ObjSym.GenerateCode(ObjSym.root, ""); //this for encoding
           }
    }
}

【讨论】:

  • 服务器,感谢您的帮助,但是如果频率是浮动的呢?
  • 你有,比如说,一个特定的符号出现了 2.5 次?
  • @505 我不明白你为什么在 "new Huffman" ."Huffman ObjSym = new Huffman();" 之后在对象创建中保留 ?
  • 如果您的符号实际上是整数,您会这样做。你可以在那里提供任何类型,只要它实现了 IComparable。
  • 你的意思是假设如果我读取一个二进制文件,然后我读取了“int”类型的符号,然后我读取了“short”或“ulong 类型”的符号,那么你的 将输入将其强制转换为“int 类型”?我理解正确吗?我想为所有“int”、“short”、“ulong”等制作“symbol”。你的代码也这样做吗?
【解决方案2】:

您只需要在这里使用interface

public interface IMyType
{
    int Symbol { get; set; }
    int Freq { get; set; }
}

然后只需将它用于您希望能够通用使用的所有类。所以

public class ClassA : IMyType
{
    ...
    public int Symbol { get; set; }
    public int Freq { get; set; }
    ...
}

public class ClassB : IMyType
{
    ...
    public int Symbol { get; set; }
    public int Freq { get; set; }
    ...
}

然后你可以在这样的方法中使用这些对象

void SomeMethod(IMyType o)
{
    o.Symbol = 1;
    o.Freq = 2;
    ...
}

IMyType a = new ClassA();
IMyType b = new ClassB();
SomeMethod(a);
SomeMethod(b);

我希望这会有所帮助。

【讨论】:

  • 感谢您的帮助,但我不必使用接口。
  • 那我恐怕没看懂这个问题……对不起。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-05
相关资源
最近更新 更多