开发者

Will I need a recursive function to iterate through a Dictionary<String, Object> of Dictionary<String, Object>s?

And there could be many levels, who knows how deep it could go!开发者_运维问答 Also let's say for this specific question that the String for another Dictionary will be "Dictionary" and a value that I want to access/modify will be "Data".


Will I need a recursive function to iterate through a Dictionary<String, Object> of Dictionary<String, Object>s?

No. Any recursive algorithm can be rewritten to use an explicit stack rather than the call stack.

Is it easier to use recursion to do so?

Perhaps. However, if the structure is very deeply nested (or contains cycles) you can run the risk of a stack overflow exception.

The non-recursive implementation is not particularly hard to implement. It would require you to maintain a list (a stack or queue, depending on what order you want to visit children in) that keeps track of the sub-dictionaries yet to be visited.

A prototypical (non-recursive) implementation would look something like:

public IEnumerable<string> GetAllKeys( Dictionary<string,object> dictionary )
{
    var stackDictionariesToVisit = new Stack<Dictionary<string,object>>();

    stackDictionariesToVisit.Push( dictionary );

    // keep visiting iterating until the stack of dictionaries to visit is empty
    while( stackDictionariesToVisit.Count > 0 )
    {
        var nextDictionary = stackDictionariesToVisit.Pop();
        foreach( var keyValuePair in nextDictionary )
        {
            if( keyValuePair.Value is Dictionary<string,object> )
            {
                stackDictionariesToVisit.Push( 
                     keyValuePair.Value as Dictionary<string,object> );
            }
            else
            {
                yield return keyValuePair.Key;
            }
        }
    }
}

The implementation above does no error checking and it doesn't check for cycles, but it replaces recursion with an explicit stack.

The choice of whether to use recursion (or not) for visiting a hierarchical data structure should depend on an understanding of the kind of data being stored rather than which approach is easier. If you have a deeply nested structure, you are better off NOT using recursion since you can't control how much stack space you'll have available. On the other hand, if you are confident that the data will never nest more than a few level, a recursive implementation may be (slightly) easier to understand and maintain.


Basically, yes. You'd have to test each value against the types you're willing to iterate through, by casting them with as.


Recursion is generally the most code-efficient way to traverse a tree structure of unknown depth. Is there some reason you can't use recursion? It sounds as if you're looking for some other option as if a recursive algorithm doesn't meet some requirement.


I am going to have to go with "yes" this would be the quickest and easiest to write. something like this....

string WalkIt( Dictionary<string,object> data )
{
    if ( data == null ) yield return null;

    foreach (KeyValuePair<string, object> kvp in data)
    {
        if (kvp.Value is string)
        {
            yield return kvp.Value;
        }

        if (kvp.Value is Dictionary<string, object>)
        {
            foreach( string str in walk(kvp.Value as Dictionary<string, object>))
                 yield return str;
        }
            }
        }
       yield return null;
}


It's not essential to recurse. You can do this using Linq as shown by concrete extension methods to make virtually any tree searchable by Linq.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜