简体   繁体   English

Ocaml 帮助树遍历

[英]Ocaml help tree traversal

Problem Description:问题描述:

Professor R. Borist studies trees. R. Borist 教授研究树木。 He has kept a record of the preorder, inorder, and postorder traversals of all of his favorite trees.他记录了所有他最喜欢的树的前序、中序和后序遍历。 However, a fire in his office has destroyed the file cabinet where he stored the inorder traversals.然而,他办公室的一场火灾摧毁了他存放 inorder 遍历的文件柜。 He still has the preorder and postorder traversals of all of his favorite trees, is this enough information to reconstruct the missing inorder traversals?他仍然拥有所有他最喜欢的树的前序和后序遍历,这些信息是否足以重建丢失的中序遍历?

You must design and implement a program for the following task: The input will consist of two lists of numbers.您必须为以下任务设计并实现一个程序: 输入将由两个数字列表组成。 The first list is the preorder traversal of some tree T. The second list is the postorder traversal of the same tree T. The output should be the inorder traversal of T. If the input does not determine a unique tree, then any consistent inorder traversal can be returned.第一个列表是某棵树 T 的前序遍历。第二个列表是同一棵树 T 的后序遍历。输出应该是 T 的中序遍历。如果输入没有确定唯一树,则任何一致的中序遍历可以退货。

If it helps in designing your implementation, you can assume that:如果它有助于设计您的实现,您可以假设:

  • No tree has more than 1,000 nodes.没有一棵树有超过 1,000 个节点。
  • No tree uses the same label for multiple nodes.没有树对多个节点使用相同的标签。
  • Labels of nodes are numbers from 0 to 10,000.节点的标签是从 0 到 10,000 的数字。

Sample data样本数据

Input:
2 6 7 1 11 8 5 10 3 4 9
7 8 5 11 10 1 6 4 9 3 2
Output:
7 6 8 11 5 1 10 2 4 3 9

Hints提示

Given the preorder & postorder traversals of a tree, can you deduce which element was the root?给定树的前序和后序遍历,你能推断出哪个元素是根吗? which elements must have come from the left subtree?哪些元素一定来自左子树? from the right subtree?从右子树? Recurse.递归。

First solve the problem when every node in the tree is guaranteed to have either 2 or 0 children.首先解决当树中的每个节点都保证有 2 个或 0 个子节点时的问题。 The case in which some nodes have only 1 child is a little trickier.某些节点只有 1 个子节点的情况有点棘手。

Notes笔记

In your writeup, you do not need to analyze the efficiency / running time of your solution (this will be a requirement in future projects).在您的文章中,您不需要分析解决方案的效率/运行时间(这将是未来项目的要求)。 But do analyze its correctness;但是一定要分析它的正确性; ie, explain clearly why your algorithm is correct.即,清楚地解释为什么你的算法是正确的。

Even if the problem description does not explicitely says so, it is clear from the hints that we are dealing with binary trees.即使问题描述没有明确说明,从提示中可以清楚地看出我们正在处理二叉树。

The hint is good: find the root of the tree, then find the root of the left subtree and the root of the right one.提示很好:找到树的根,然后找到左子树的根和右子树的根。

After removing the root, look for a position in the list of nodes where you can cut the list: the nodes before the cut are from the left subtree, the nodes after the cut are from the right subtree.删除根后,在节点列表中寻找可以剪切列表的位置:剪切前的节点来自左子树,剪切后的节点来自右子树。

Working out small examples like you did is a good idea.像你一样计算小例子是个好主意。

To answer specifically "understanding how I can build the inorder traversal from the pre and post": first rebuild the tree from the pre and post traversals, then build the inorder traversal from the tree.要具体回答“理解我如何从前后遍历构建中序遍历”:首先从前后遍历中重建树,然后从树中构建中序遍历。

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

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