🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Vector question

Started by
14 comments, last by chatch1111 4 years, 11 months ago

You really ought to take a look at depth first search preorder traversal. Essentially, you'll just start at the initial parent node, then print in cascading fashion all of that "parents" descendants, then recurse or iterate to the next parent and so on.

Essentially, you are going to want to have a pointer to each nodes child left and right, then you can use a recursive call to consume the data (print or whatever task you are trying to accomplish from the scenegraph) call left, print, right.

dfs(SceneNode* head) {

     if(head == nullptr)

    //exit condition usually return if function is void

     dfs(head->left);

     //task to be completed

     dfs(head->right);
}

Advertisement
6 hours ago, chatch1111 said:

Essentially, you are going to want to have a pointer to each nodes child left and right, then you can use a recursive call to consume the data (print or whatever task you are trying to accomplish from the scenegraph) call left, print, right.

dfs(SceneNode* head) {

     if(head == nullptr)

    //exit condition usually return if function is void

     dfs(head->left);

     //task to be completed

     dfs(head->right);
}

Unless I'm missing something, the above is for a binary tree, but DividedByZero is working with a general tree. (The discussion of recursive traversal is of course relevant either way though.)

On 7/15/2019 at 10:35 AM, Zakwayda said:

Unless I'm missing something, the above is for a binary tree, but DividedByZero is working with a general tree. (The discussion of recursive traversal is of course relevant either way though.)

You aren't, I only mentioned it because the way I've seen a scenegraph constructed was as a tree, where the OP is just using a vector and iterating it in a seemingly inefficient manner which also seems error prone as it is not working how they want it to. Like anything else, there are a million ways to skin this cat, maybe not literally, but that's what I would lean towards because it's a proven way to accomplish the task at hand.

On 7/15/2019 at 4:35 AM, chatch1111 said:

You really ought to take a look at depth first search preorder traversal. Essentially, you'll just start at the initial parent node, then print in cascading fashion all of that "parents" descendants, then recurse or iterate to the next parent and so on.

Essentially, you are going to want to have a pointer to each nodes child left and right, then you can use a recursive call to consume the data (print or whatever task you are trying to accomplish from the scenegraph) call left, print, right.

dfs(SceneNode* head) {

     if(head == nullptr)

    //exit condition usually return if function is void

     dfs(head->left);

     //task to be completed

     dfs(head->right);
}

Also note that this is not a pre-order traversal, but an in-order one.

4 minutes ago, _Silence_ said:

Also note that this is not a pre-order traversal, but an in-order one.

Can confirm.. all nighter posting clearly I should stay away from

This topic is closed to new replies.

Advertisement