-1

Hi I am struggling to figure this out. The pre-order traversal of a binary search tree is: 15, 9, 6, 1, 7, 13, 23, 19, 39, 32.

what would be the postorder traversal of it?

to figure out the post order traversal, we need to obtain the structure of the binary tree first, but I am struggling to figure this out.

Thanks

  • 1
    You can draw the structure yourself by recognising that 15 is the root node (printed first), then construct the nodes of the left and right branches... hint: you have to backtrack to place node '7'... EDIT: Once you have your own diagram that makes sense, apply "postorder" traversal to that sketch... – Fe2O3 Aug 09 '22 at 06:29
  • @Fe2O3 i am pretty lost on where to place 7 in the tree – ferocioussprouts122 Aug 09 '22 at 06:33
  • Is 7 < 6 (to the left of 6)? Is 7 > 6 and < 9??? Yes, there's 'backtracking' involved with some empty 'leafs'... EDIT: I mistakenly typed "13"... I meant "9"... Sorry... – Fe2O3 Aug 09 '22 at 06:35
  • @Fe2O3 would i place the 13 to the right of 7 or right of 9? – ferocioussprouts122 Aug 09 '22 at 06:41
  • If you've done well, 7 is 'left' of 9 (being less-than)... You don't want to put 13 as a child of 7 because that would mean that 13 is less-than 9... But, 13 might be greater-than 9! :-) – Fe2O3 Aug 09 '22 at 06:44

2 Answers2

1

It is not a programming question.

Do it recursively. The root of the tree is of course the first element in the traversal (15). Then all elements that follow less than 15 (9 to 13) are the left branch, the rest (23 to 32) are the right branch. Apply the same algorithm recursively (i.e. 9 is the root for the left branch, and so on).

MMZK1526
  • 654
  • 2
  • 16
1

As the input is a preorder traversal, we can insert the values into a (growing) BST as leaves in that order:

  • They would need to be leaves at the moment of insertion, as otherwise they would be a parent of an earlier inserted node, which is in contradiction with the preorder sequence of the input.

  • In a binary search tree there is only one spot to add a new value as a leaf.

Following the above observations, you can just insert the values into a BST in the order you read them from the input, using the usual insertion algorithm.

But actually building the binary search tree is not necessary. We can derive the post order traversal from the input using a stack, or an (equivalent) recursive procedure.

The main idea is that the preorder values arrive below a node as long as its values are not crossing a maximum value -- a maximum that is determined by an ancestor node's value where we currently are in the left subtree of that ancestor. So by the use of a stack (or recursion) we can output that subtree in a bottom up fashion (postorder).

Here is an implementation:

#include <stdio.h>
#include <limits.h>

void dfsHelper(int preOrder[], int n, int *i, int high, int postOrder[], int *j) {
    if (*i >= n) return;
    int value = preOrder[*i];
    if (value > high) return;
    (*i)++;
    dfsHelper(preOrder, n, i, value, postOrder, j); // Output left subtree
    dfsHelper(preOrder, n, i, high, postOrder, j);  // Output right subtree
    postOrder[(*j)++] = value;                      // Output node
}

void preToPostOrder(int preOrder[], int n, int postOrder[]) {
    int i = 0, j = 0;
    dfsHelper(preOrder, n, &i, INT_MAX, postOrder, &j);
}

int main() {
    // Example input as given in the question:
    int preOrder[] = {15, 9, 6, 1, 7, 13, 23, 19, 39, 32};
    int n = sizeof(preOrder) / sizeof(int);
    int postOrder[n]; // Target for the output of this algorithm
    preToPostOrder(preOrder, n, postOrder);
    // Output post order result
    for (int i = 0; i < n; i++) printf("%d ", postOrder[i]);
    printf("\n");
    return 0;
}
trincot
  • 317,000
  • 35
  • 244
  • 286