c# - TreeNode stack Depth First Search -


i saw topic: depth first search without stack [closed]. have question related function. want use stack. c# beginner , wondering if can explain me in detail 1) following program goal , 2) expected output.

void foo(treenode root) {     stack nodes = new stack();     nodes.push(root);      while (nodes.count > 0)     {         treenode node = (treenode) nodes.pop();         console.writeline(node.text);         (int = node.nodes.count - 1; >= 0; i--)             nodes.push(node.nodes[i]);     } } 

try it.

        foo(new treenode()         {             text = "test",             nodes = new list<treenode>()             {                  new treenode(){text = "a"},                 new treenode(){                     text = "b",                     nodes = new list<treenode>()                     {                          new treenode(){text = "c"},                         new treenode(){text = "d"},                     }                 },             }         } ); 

result:

test b c d 

basically on each trip in loop gets first item in stack , adds stack children (if any). re-fetches loop again.


Comments

Popular posts from this blog

java - Could not locate OpenAL library -

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

sorting - opencl Bitonic sort with 64 bits keys -