0

I use function to solve Huffman coding algorithm but the function take string variable as data but i want it to take array when i write array.ToString() the function out print some unneeded result because i put array no variable. this is function code

public static void GenerateCode(Node parentNode, string code)
{
    if (parentNode != null)
    {
        GenerateCode(parentNode.leftChild, code + "0");

        if (parentNode.leftChild == null && parentNode.rightChild == null)
        Console.WriteLine(parentNode.data + "{" + code + "}");

        GenerateCode(parentNode.rightChild, code + "1");

    }
}

This is my calling for the function in the main:

GenerateCode(parentNode1,array.ToString());

This is photo with the result:

image

I need don't show System.Int[]

3 Answers 3

1

The problem is that array.ToString() isn't overridden by the language. Instead, you probably want to use another function, like string.Concat:

GenerateCode(parentNode1, string.Concat(array));

The result you were getting is because the default implementation of ToString() (object.ToString(), that is) prints out type information.


Although if you do have control over the GenerateCode method, I would strongly suggest modifying its parameter type to accept an IEnumerable<T> or array. Converting to a string, unless necessary, will lose some major benefits of strong-typing. I would make that conversion in the method, not before it. That will allow you to change the implementation later on without worrying about what parameters you pass.

Furthermore, I'd be tempted to write your values to a StringBuilder rather than the console, since that will be way more useful in a majority of real-world applications.

public static void GenerateCode(StringBuilder builder, Node parentNode, IEnumerable<int> values)
{
    if (parentNode != null)
    {
        GenerateCode(builder, parentNode.leftChild, Concat(values, 0));

        if (parentNode.leftChild == null && parentNode.rightChild == null)
            builder.AppendLine(string.Format("{0}{{{1}}}", parentNode.data, string.Concat(values));

        GenerateCode(builder, parentNode.rightChild, Concat(values, 1));
    }
}

private static IEnumerable<T> Concat(IEnumerable<T> coll, T obj)
{
    foreach (var v in coll)
        yield return v;

    yield return obj;
}

Then you can call it, of course, like this.

StringBuilder builder = new StringBuilder();
GenerateCode(builder, parentNode1, array);
Console.Write(builder.ToString());
Sign up to request clarification or add additional context in comments.

Comments

1

You want to use string.Join().

GenerateCode(parentNode1, string.Join("", array));

6 Comments

Not using commas, its a binary representation I believe
Now it's just my answer :)
@Mangist Yes, but linking to MSDN is always recommended when you're referencing a particular method in .NET.
@SpikeX but the result show with the data i put in the array and i don't need that i need only the result of the function
Sure. I think the example is enough, the asker can Google the .NET method if they want more info. 99% of the time they will just copy/paste your answer into their code.
|
1

What you need to do is pass in a string representation of your int array.

You can use:

GenerateCode(parentNode1, String.Join("", array));

Which will concatenate all your ints into a string like 10001010101010111011101

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.