简体   繁体   English

找出给定前序遍历的二叉搜索树的结构

[英]figure out the structure of a binary search tree given preorder traversal

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.二叉搜索树的前序遍历为: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谢谢

It is not a programming question.这不是一个编程问题。

Do it recursively.递归执行。 The root of the tree is of course the first element in the traversal (15).树的根当然是遍历 (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.然后所有小于 15(9 到 13)的元素都是左分支,rest(23 到 32)是右分支。 Apply the same algorithm recursively (ie 9 is the root for the left branch, and so on).递归应用相同的算法(即 9 是左分支的根,依此类推)。

As the input is a preorder traversal, we can insert the values into a (growing) BST as leaves in that order:由于输入是一个前序遍历,我们可以将这些值插入到(增长的)BST 中,作为叶子顺序:

  • 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.根据上述观察,您可以使用通常的插入算法,按照从输入中读取它们的顺序将值插入 BST。

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).因此,通过使用堆栈(或递归),我们可以以自下而上的方式(后序) output 该子树。

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;
}

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

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