简体   繁体   English

预序树遍历有效,但后序无效

[英]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.我有两个函数以preorderpostorder遍历树,每个函数将节点中的值插入到一个数组中,并返回该数组。

However, my postorder function does not work.但是,我的postorder功能不起作用。 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.编译并运行以下代码时,但在调用postorder方法postorder出现分段错误。

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) 注意- 我有 3 个节点的树,为什么我从 0 到 3 循环)

What can be the issue?可能是什么问题? What is the different between my post and pre order?我的post和pre order有什么不同?

Notice that you do: complete_post_b = postorder_recursive(b, post_order_b);请注意,您这样做: 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;complete_post_bmain的开头定义为: int* complete_post_b;

However, at the postorder_recursive you do not returning any data.但是,在postorder_recursive返回任何数据。 so when assiging to complete_post_b it actually void.所以当分配给complete_post_b它实际上是无效的。

Your for loop should be as:你的 for 循环应该是:

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 .或者您可以返回dataArray并使用complete_post_b

For the interest part: why does it happens only on postOrder ?对于兴趣部分:为什么它只发生在 postOrder 上

Notice, that the only time you return the dataArray is when the node is null.请注意,只有当节点为空时才返回dataArray 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在函数末尾进行递归函数调用时,数据数组的地址保留在寄存器中并向前传输 - 但您不能指望这一点,如果您想使用它,您需要实际返回地址

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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