Permalink #1 
fongmingyun's Avatar
Join Date: Aug 2008
Posts: 318
Senior Member
    My Mood: Cheerful
    Thanks: 0
    Thanked 16 Times in 16 Posts

    Default Tree Traversal Algorithms


    Ugh, I've been having a decent bit of trouble with this. Does anyone have an algorithm for returning the next node in a tree using preorder traversal? Do I need to to use a stack structure?

    (Btw, coding a scene graph structure in C++ if you're curious.)
      Permalink #2 
    Shaderhacker's Avatar
    Join Date: Aug 2008
    Posts: 58
    Member
      My Mood: Busy
      Thanks: 0
      Thanked 6 Times in 6 Posts

      Quote:
      Originally Posted by fongmingyun View Post
      Ugh, I've been having a decent bit of trouble with this. Does anyone have an algorithm for returning the next node in a tree using preorder traversal?
      What kind of tree? Binary? Red/Black? Arbitrary?
        Permalink #3 
      fongmingyun's Avatar
      Join Date: Aug 2008
      Posts: 318
      Senior Member
        My Mood: Cheerful
        Thanks: 0
        Thanked 16 Times in 16 Posts

        Arbitrary. Any number of children, basically.

        I finished this assignment awhile ago - I ended up writing something relatively complicated that goes "down" or "up and then right" recursively. I wonder if there's a more efficient method.
          Permalink #4 
        geckzilla's Avatar
        Join Date: Sep 2008
        Posts: 161
        Member
          Thanks: 1
          Thanked 23 Times in 22 Posts

          Reminds me of a function I wrote in php once to traverse directory structures. What I ended up doing was having the function call itself within itself whenever it hit a directory. This is super simplified way of describing what I did:

          Code:
          function traverseNode(node) {
            for(x in node) {
              //do whatever with x here
          
              //then see if it has children
              if(x.children > 0) {
                traverseNode(x);
              }
            }
          }
          Though I don't really have any context. You mentioned going right, up, and down so maybe I'm completely misunderstanding what this is.


          Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
           

          Posting Rules
          Smilies are On
          [IMG] code is On
          HTML code is Off

          Similar Threads
          Thread Thread Starter Forum Replies Last Post
          Tobi and the Jelly Bean Tree BobbyChiu Concept & Illustration 3 06-24-2009 08:18 AM
          2D- Tree Man in the Forest xeni Taron's "Natives of the Lifeless Forest" 13 03-28-2009 03:58 PM