manu
manu

Reputation: 1817

recursive function in collection

I am using c# and List collection and loaded the values. Once it is done I am trying to read them recursively but some how I am not able to achieve this.

the following is my main code.

    private static void Main(string[] args)
            {
                var node = new Node
                    {
                        Name = "N1",
                        Nodes =
                            new List<Node>
                                {
                                    new Node { Name = "N1a" },
                                    new Node { Name = "N1b", Nodes = new List<Node> { new Node { Name = "N1B1" } } },
                                    new Node
                                        {
                                            Name = "N1c",
                                            Nodes =
                                                new List<Node> { new Node { Name = "N1C1", Nodes = new List<Node> {new Node{Name = "N1C1A"} } } }
                                        }
                                }
                    };
                GetNodes( node );
                Console.ReadLine();
            }

 public class Node
        {
            public string Name { get; set; }

            public IList<Node> Nodes { get; set; }
        }

and the function call is following

public static IEnumerable<Node> GetNodes(Node node)
        {
            if (node == null)
            {
                return null;
            }

            Console.WriteLine(node.Name);

            foreach (var n in node.Nodes)
            {
                return GetNodes(n);
            }

            return null;
        }
    }  

Could any one please help me to fix the recursive function?

Upvotes: 2

Views: 10604

Answers (4)

cuongle
cuongle

Reputation: 75306

public static IEnumerable<Node> GetNodes(Node node)
{
    if (node == null) return null;

    var nodes = new List<Node>();
    nodes.Add(node);

    Console.WriteLine(node.Name);

    if (node.Nodes != null)
    {
        foreach (var n in node.Nodes)
        {
            nodes.AddRange(GetNodes(n));
        }
    }

    return nodes;
}}

Upvotes: 2

ElDog
ElDog

Reputation: 1377

You return from your method inside a loop immediately, on teh first iteration. No other iterations is performed.

Upvotes: 0

Rawling
Rawling

Reputation: 50104

If you just want to print the names of all the nodes,

public static void GetNodes(Node node)
{
    if (node == null)
    {
        return;
    }
    Console.WriteLine(node.Name);
    foreach (var n in node.Nodes)
    {
        GetNodes(n);
    }
}

If you want to flatten the tree,

public static IEnumerable<Node> GetNodes(Node node)
{
    if (node == null)
    {
        yield break;
    }
    yield return node;
    foreach (var n in node.Nodes)
    {
        foreach(var innerN in GetNodes(n))
        {
            yield return innerN;
        }
    }
}

Upvotes: 7

James Gaunt
James Gaunt

Reputation: 14783

Your method only every returns null, or calls itself and then either returns null, or calls itself..... so at the end of the day it returns null or doesn't terminate. If you want to seralize the values you can write them into a list at the same point in the code as you write them to the console.

public static void GetNodes(Node node, List<Node> output)
{
    if (node == null)
        return;

    output.Add(node);
    Console.WriteLine(node.Name);

    foreach (var n in node.Nodes)
    {
        GetNodes(n, output);
    }
}

Upvotes: 1

Related Questions