Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. tree traversal from recursive to iterative

tree traversal from recursive to iterative

Scheduled Pinned Locked Moved C / C++ / MFC
data-structuresgraphicstutorialquestionannouncement
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Michele Bosi
    wrote on last edited by
    #1

    I have a tree data structure that I use for my scene graph that I traverse each frame to update 20.000 nodes, I am currently doing the typical recursive traversal but probably it would be quicker if I converted the recursive traversal in an iterative one, does anyone have any hint on how to do that? Here is how my data structure looks like:

    class Node
    {
    public:

    ... some data ...

    std::vector Children;
    };

    void do_something_before(Node*)
    {
    ...
    }

    void do_something_after(Node*)
    {
    ...
    }

    // function to translate into an iterative one

    void traverse(Node* node)
    {
    do_something_before(node);

    for(int i=0; iChildren.size(); ++i )
    traverse(node->Children[i]);

    do_something_after(node);
    }

    Thanks

    N M 2 Replies Last reply
    0
    • M Michele Bosi

      I have a tree data structure that I use for my scene graph that I traverse each frame to update 20.000 nodes, I am currently doing the typical recursive traversal but probably it would be quicker if I converted the recursive traversal in an iterative one, does anyone have any hint on how to do that? Here is how my data structure looks like:

      class Node
      {
      public:

      ... some data ...

      std::vector Children;
      };

      void do_something_before(Node*)
      {
      ...
      }

      void do_something_after(Node*)
      {
      ...
      }

      // function to translate into an iterative one

      void traverse(Node* node)
      {
      do_something_before(node);

      for(int i=0; iChildren.size(); ++i )
      traverse(node->Children[i]);

      do_something_after(node);
      }

      Thanks

      N Offline
      N Offline
      Nemanja Trifunovic
      wrote on last edited by
      #2

      Michele Bosi wrote:

      does anyone have any hint on how to do that?

      You can use a stack to simulate calling a function recursively. For instance, look here[^]

      Programming Blog utf8-cpp

      M 1 Reply Last reply
      0
      • N Nemanja Trifunovic

        Michele Bosi wrote:

        does anyone have any hint on how to do that?

        You can use a stack to simulate calling a function recursively. For instance, look here[^]

        Programming Blog utf8-cpp

        M Offline
        M Offline
        Michele Bosi
        wrote on last edited by
        #3

        I already saw that example of wikipedia, unfortunately it is for a binary tree meanwhile I would need a more general N-ary tree and with operations on the node performed before and after visiting the children. I thought there would be a "text-book" solution to this but it seems that this is a much more complex problem than I expected...

        N 1 Reply Last reply
        0
        • M Michele Bosi

          I already saw that example of wikipedia, unfortunately it is for a binary tree meanwhile I would need a more general N-ary tree and with operations on the node performed before and after visiting the children. I thought there would be a "text-book" solution to this but it seems that this is a much more complex problem than I expected...

          N Offline
          N Offline
          Nemanja Trifunovic
          wrote on last edited by
          #4

          Michele Bosi wrote:

          I thought there would be a "text-book" solution to this but it seems that this is a much more complex problem than I expected...

          It is not trivial, but it is still pretty straightforward: replace recursive calls with a loop and a stack. Local variables would take values from the stack, much like function parameters, and recursive function calls would translate to putting parameters on the stack and going to the beginning of the loop.

          Programming Blog utf8-cpp

          M 1 Reply Last reply
          0
          • N Nemanja Trifunovic

            Michele Bosi wrote:

            I thought there would be a "text-book" solution to this but it seems that this is a much more complex problem than I expected...

            It is not trivial, but it is still pretty straightforward: replace recursive calls with a loop and a stack. Local variables would take values from the stack, much like function parameters, and recursive function calls would translate to putting parameters on the stack and going to the beginning of the loop.

            Programming Blog utf8-cpp

            M Offline
            M Offline
            Michele Bosi
            wrote on last edited by
            #5

            In theory I would also do as you say, but when you actually try to translate it into practice everything becomes much more foggy, especially the management of the 2 functions "do_something_before()" and "do_soemthing_after()".

            1 Reply Last reply
            0
            • M Michele Bosi

              I have a tree data structure that I use for my scene graph that I traverse each frame to update 20.000 nodes, I am currently doing the typical recursive traversal but probably it would be quicker if I converted the recursive traversal in an iterative one, does anyone have any hint on how to do that? Here is how my data structure looks like:

              class Node
              {
              public:

              ... some data ...

              std::vector Children;
              };

              void do_something_before(Node*)
              {
              ...
              }

              void do_something_after(Node*)
              {
              ...
              }

              // function to translate into an iterative one

              void traverse(Node* node)
              {
              do_something_before(node);

              for(int i=0; iChildren.size(); ++i )
              traverse(node->Children[i]);

              do_something_after(node);
              }

              Thanks

              M Offline
              M Offline
              Michele Bosi
              wrote on last edited by
              #6

              Thanks to all the replies, it turned out that a easier way of optimizing such code is to make the data structures as cache friendly as possible making the nodes smaller and careful allocation, so one can forget about iterative traversal for another bit...

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups