简体   繁体   中英

Preorder tree traversal works but postorder doesn't

I have two functions that traverse a tree in preorder and postorder , each inserting the values in the nodes into an array, and returning the array.

However, my postorder function does not work. I get a segmentation fault when the function is called.

When the following code is compiled and run, but after calling the postorder method I get a segmentation fault.

This is my code:

int* preorder_recursive(node *root, int* dataArray)
{

  if (root == NULL)
    return dataArray;

  for (int i = 0; i < 512; i++)
  {
    if (dataArray[i] == INT_MIN)
    {
      dataArray[i] = root->data;
      printf("%d is being inserted to the preorder array at pos %d\n", root->data, i);
      break;
    }
  }

  preorder_recursive(root->left, dataArray);
  preorder_recursive(root->right, dataArray);
}

int* postorder_recursive(node *root, int *dataArray)
{
  if (root == NULL)
    return dataArray;

  postorder_recursive(root->left, dataArray);

  postorder_recursive(root->right, dataArray);

  for (int i = 0; i < 512; i++)
  {
    // any "empty" spots in the array should contain INT_MIN
    if (dataArray[i] == INT_MIN)
    {
      dataArray[i] = root->data;
      printf("%d is being inserted to the postorder array at pos %d\n", root->data, i);
      break;
    }
  }
}

When calling:

int * complete_pre_b = preorder_recursive(b, pre_order_b);
for(int i = 0; i < 3; i++)
{
    printf("pre b is %d\n", complete_pre_b[i]);
}

int * complete_post_b = postorder_recursive(b, post_order_b);
// here is the last print I see - code get till here fine
for(int i = 0; i < 3; i++)
{
    printf("post b is %d\n", complete_post_b[i]);
}

( Notice - I have tree with 3 node that why I loop for i from 0 to 3)

What can be the issue? What is the different between my post and pre order?

Notice that you do: complete_post_b = postorder_recursive(b, post_order_b); when complete_post_b is define at the begin of main as: int* complete_post_b;

However, at the postorder_recursive you do not returning any data. so when assiging to complete_post_b it actually void.

Your for loop should be as:

for(int i = 0; i < 3; i++)
{
   printf("post b is %d\n", post_order_b[i]); // and not complete_post_b 
}

Or you can return the dataArray and use the complete_post_b .

For the interest part: why does it happens only on postOrder ?

Notice, that the only time you return the dataArray is when the node is null. My guess is in that case, the register for return value will contain the data array. When having the recursive function call at the end of your function the address of the data array stays in the register and transfer forward - but you can not count on that and you need to actually return the address if you want to use it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM