0

I am new to c# i have just completed a huffman tree and now next step is to make it generic i mean this symbolshould work for every data type. Since i am c# beginner i need some basic idea to do that.

My huffman tree consists of 3 classes. Class huffman, node and MyClass(which contains the main function) where freq is the number of times the symbol repeats they are structured as given below:

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
           }
    }
}

Could some one please help me in making this "symbol" work for all data types like "short","long" etc.

Sss
  • 1,519
  • 8
  • 37
  • 67
  • When you say generic do you mean that one node could have an int symbol and one a string symbol within the same tree or do you want to be able to create a tree where symbol is always the same (for example string) – Bob Vale Mar 19 '14 at 16:58
  • @BobVale actually i am reading a binary file in order to create freq of the symbols (which could be of the form 1110111..). The "symbol" must work for 1 byte or 2 byte etc. Have you understood now ? – Sss Mar 19 '14 at 17:03

2 Answers2

2

If I am understanding you correctly, you would basically do something like

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
           }
    }
}
  • Server, Thanks for the help but what if the freq is float ? – Sss Mar 19 '14 at 17:05
  • 1
    You have, say, 2.5 occurrences of a particular symbol? – 500 - Internal Server Error Mar 19 '14 at 17:06
  • @505 I couldn't understand why you have kept in object creation after "new Huffman" ."Huffman ObjSym = new Huffman();" ? – Sss Mar 19 '14 at 17:13
  • That's what you would do if your symbols are actually ints. You can supply any type there as long as it implements IComparable. – 500 - Internal Server Error Mar 19 '14 at 17:15
  • You mean suppose if i read a binary file and then i read symbol which of "int" type after that i read it of "short" or "ulong type", so your will type caste it to "int type" ? Am i understanding correctly ? I wanted to make "symbol" for all "int", "short", "ulong" etc. Is your code doing the same? – Sss Mar 19 '14 at 17:21
  • So your input contains multiple types of data? Since you have to be able to compare them in order to find out which are duplicates I think you will need to settle on a datatype that can encompass them all. – 500 - Internal Server Error Mar 19 '14 at 17:23
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/50058/discussion-between-user234839-and-500-internal-server-error) – Sss Mar 19 '14 at 17:24
1

All you need to use here is an interface

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

then just use this for all classes you want to be able to work with generically. So

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; }
    ...
}

Then you can use these object in methods like this

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

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

I hope this helps.

MoonKnight
  • 23,214
  • 40
  • 145
  • 277