简体   繁体   English

从inorder和preorder遍历构造二叉树的时间复杂度

[英]Time complexity of construction of a binary tree from inorder and preorder traversals

Given Here is the code for constructing a tree from the inorder and preorder traversals. 给定是从inorder和preorder遍历构造树的代码。 I cant figure out how they arrived at an O(n^2) time complexity. 我无法弄清楚他们是如何达到O(n ^ 2)时间复杂度的。 Any ideas? 有任何想法吗? I see that the search for an index in the inorder sequence would be O(n), how is the rest of it computed? 我看到在顺序序列中搜索索引将是O(n),其余的如何计算?

The O(N^2) complexity results from the fact that for every item in the Preorder traversal (of which there are N ), you have to search for its partition in the Inorder traversal, (again there are N of these). O(N^2)复杂度是由于对于Preorder遍历中的每个项目(其中有N ),您必须在Inorder遍历中搜索其分区(同样还有N个)。

Roughly speaking, you can consider this algorithm as placing the nodes on a grid, where the Inorder traversal provides the x co-ordinates and the Preorder traversal provides the y co-ordinates: 粗略地说,您可以将此算法视为将节点放置在网格上,其中Inorder遍历提供x坐标,Preorder遍历提供y坐标:

Take the example they gave, with the following traversals (Inorder then Preorder): 以他们给出的示例为例,进行以下遍历(Inorder then Preorder):

Inorder: DBEAFC
Preorder: ABDECF

Now this is the grid they are being put on: 现在这是他们正在使用的网格:

     D    B    E    A    F    C
A    +    +    +    A    |    |
     |    +--------------+    |
B|F  +    B    |         F    |
     +---------+         -----+
DE|C D         E              C

Now, the algorithm needs to know where in the grid to place each node, which it does simply by putting the node at the position in the grid where the x and y co-ordinates are the same. 现在,算法需要知道网格中放置每个节点的位置,只需将节点放在网格中x和y坐标相同的位置即可。

It looks as though the size of the grid is actually NlogN in this case, which would result in an NlogN complexity for traversing the grid (and so an NlogN time complexity for the algorithm) but this tree is balanced . 在这种情况下,看起来网格的大小实际上是NlogN ,这将导致遍历网格的NlogN复杂性(因此算法的NlogN时间复杂度), 但是这棵树是平衡的 In the worst case, your tree might in fact be a linked list. 在最坏的情况下,您的树实际上可能是一个链表。

Eg consider this tree, where the preorder and inorder traversals are the same: 例如,考虑这个树,其中preorder和inorder遍历是相同的:

Inorder: DBEAFC
Preorder: DBEAFC

     D    B    E    A    F    C
D    D    |    |    |    |    |
     -----+    |    |    |    |
B         B    |    |    |    |
          -----+    |    |    |
E              E    |    |    |
               -----+    |    |
A                   A    |    |
                    -----+    | 
F                        F    |
                         -----+
C                             C

This is the worst case, and you see, the grid has N*N positions in it to check. 这是最糟糕的情况,你看,网格中有N*N位置需要检查。 So in the worst case, there is an N*N time complexity. 因此在最坏的情况下,存在N*N时间复杂度。

you are traversing the whole preorder array inside the recursion and in each stack frame you are searching a number in inorder traversal array. 你正在遍历递归内的整个preorder数组,并在每个堆栈帧中搜索inorder遍历数组中的数字。 So O(N*N) = o(N^2) . 所以O(N*N) = o(N^2)

you are absolutely right, because search in inorder array will take O(n) time 你是绝对正确的,因为在inorder数组中搜索将花费O(n)时间

In worst case T(n)= T(n-1) + O(n) 在最坏的情况下,T(n)= T(n-1)+ O(n)

solving this we get T(n)=O(n²) 解决这个问题我们得到T(n)= O(n²)

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

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