15,996,430 members
See more:
I am having an 2D array with having parent name in 0th index and child node in 1st index.

How to iterate through the array to get all the child of node.
{
{a,b},
{a,c},
{b,b1},
{b,b2},
{b,b3},
{b3,b31},
{b3,b32},
{b31,b311},
{b32,b321}
}

this is my array.

if I want to find all the nodes under the node b1 then how should I iterate.
Posted
Updated 6-Nov-12 1:32am
v3
OriginalGriff 6-Nov-12 6:51am
This is not a good question - we cannot work out from that little what you are trying to do.
Use the "Improve question" widget to edit your question and provide better information.
djrocks0101 6-Nov-12 7:34am
I have edited my question and added an sample array which i wanna iterate through
MT_ 7-Nov-12 0:10am
Where is B1 node ?

## Solution 1

As OriginalGriff has said in the comment, not much details are given.

Generally you traverse through 2D array as

C#
```for(int i = 0;i<array.length;i++)>
{
for(int j=0;j<array[i].length,j++)>
{
var value = array[i][j];
}
}```

Hope that helps.
Milind

psychic6000 6-Nov-12 7:32am
you may want to edit your solution for non zero start array...
MT_ 6-Nov-12 7:37am
psychic6000 6-Nov-12 12:23pm
you know c sharp allows to use an array which first item might not be at array[0], check my solution how to work with those...
djrocks0101 6-Nov-12 7:33am
Thanks Milind, I have added an sample array, which I want to iterate through..
MT_ 6-Nov-12 7:38am
I couldn't see b1 in the sample array :-(

## Solution 2

There are two ways to traverse each element of an array, one use a foreach loop, in this case you dont have to bother the depth of each dimension, or how many dimensions you have.

but in 2nd case you, you must know the dimensions of your array, using those dimension you get the lower and upper bound index of the array. (upper and lower bound of array is required when you are using an array which start index is not zero, c# allows to use such arrays. for zero based arrays you can simply use .Length() )

C#
``` string[,] str = {{"a","b"},{"a","c"},{"b","b1"},{"b","b2"},{"b","b3"},
{"b3","b31"},{"b3","b32"},{"b31","b311"},{"b32","b321"}};
foreach(string s in str)
{
Console.WriteLine(s);
}
Console.WriteLine("");
for (int i = str.GetLowerBound(0); i <= str.GetUpperBound(0); i += 1)
{
for (int j = str.GetLowerBound(1); j <= str.GetUpperBound(1); j += 1)
Console.Write(str[i, j] + " , ");
Console.WriteLine("");
}
```

Matt T Heffron 6-Nov-12 16:26pm
Correct but not really pertinent to the OP question.

## Solution 3

Personally I'd probably map the data representation to something closer to what you are actually representing.
C#
```Dictionary<string, List<string>> Tree;
public void BuildTree(string[,] a)
{
if (a == null)
throw new ArgumentNullException("a");
Tree = new Dictionary<string, List<string>>();
int rows = a.GetLength(0);
for (int r = 0; r < rows; r++)
{
string key = a[r, 0];
List<string> subTree;
if (!Tree.TryGetValue(key, out subTree))
{
subTree = new List<string>();
}
}
}

public IEnumerable<string> TraverseTree(string startNode)
{
if (startNode == null)
throw new ArgumentNullException("startNode");
yield return startNode;
List<string> subTree;
if (Tree.TryGetValue(startNode, out subTree))
{
foreach (var node in subTree.SelectMany(n => TraverseTree(n)))
{
yield return node;
}
}
}
```

`TraverseTree("b")` will return "b" and all of the nodes under it:
C#
```// Starting with:
string[,] str = {{"a","b"},
{"a","c"},
{"b","b1"},
{"b","b2"},
{"b","b3"},
{"b3","b31"},
{"b3","b32"},
{"b31","b311"},
{"b32","b321"}};

BuildTree(str); // Constructs the Tree
TraverseTree("b");  // will produce the sequence:
"b", "b1", "b2", "b3", "b31", "b311", "b32", "b321"```

djrocks0101 7-Nov-12 9:32am
thanks bro.
this code worked for me.
my 5 :)