For maximum speed, you need to arrange the nodes in memory so that they are stored in a continuous block in the order in which you visit them.
For example, if you have a tree defined as follows.
1 / \ 2 3 / \ /\ 4 5 6 7 /\ / /\ 8 9 10 11 12 / \ \ 13 14 15
Then the visit function, as described, will visit the nodes in the following order
1 2 4 8 13 14 9 5 3 6 10 7 11 12 15
Now, if you order nodes in memory as a continuous block of 15 distributions and store the nodes in the order shown above, you usually visit a node that has spatial locality . βThis can improve your cache hits, depending on the size of your node structure and, thus speed up the work.
To create a fast iterative method of visiting all nodes in the tree only once and without recursion.
unsigned int g_StackDepth = 0; BaseNodePtr* g_Stack[MAX_STACK_DEPTH]; void processTree (BaseNodePtr root, unsigned int & cnt ) { g_Stack[g_StackDepth++] = root; while( g_StackDepth > 0 ) { BaseNodePtr curr = g_Stack[--g_StackDepth]; cnt++; if ( curr->HasNext() ) { g_Stack[g_StackDepth++] = curr->Next(); } if ( curr->HasChild() ) { g_Stack[g_StackDepth++] = curr->Child(); } } }
In combination with the above order, as far as I know, you need to get the maximum speed that you CAN get.
Obviously, this has limitations, since you need to know how big your stack can grow in advance. Although you can get around this using std :: vector. However, using std :: vector will eliminate all the advantages offered by the iterative method above.
Hope this helps :)
source share