for a programming assignment, I have to load a tree into an array in order.
The recursive algorithm itself is no problem for me. The problem I’m having is keeping the subscript/element numbers increased periodically so that the data goes into the right array element, instead of overwriting each other after calls return.
void foo( currentNode, data[], element? )
{
// base case
if ( currentNode == NULL )
foo( currentNode->left, data, element? );
data[ element++ ] = currentNode->data; // what do I do in between the left and right subtree calls?
foo( currentNode->right, data, element? );
}
So, how do I increase the element so that it only increases to the next spot of the array for an inorder traversal?
Hints are nice, but I wouldn’t mind solutions.
Make your data parameter a reference to a pointer: