【问题标题】:Need to find Time and Space complexity of my algorithm需要找到我的算法的时间和空间复杂度
【发布时间】:2012-07-02 18:45:14
【问题描述】:

不知何故,我设法编写了一个算法,用于从它的中序和前序遍历数据构造二叉树。

我不确定如何计算该算法的时间和空间复杂度

我的猜测是

first pass  --> n(findInNode) + n/2 (constructTree) + n/2 (constructTree)
second pass --> n/2(findInNode) + n/4 (constructTree) + n/4 (constructTree)
etc..

所以应该是大约(3logn)

如果我错了,请纠正我。

public class ConstructTree {
    public static void main(String[] args) {
        int[] preOrder = new int[] { 1, 2, 3, 4, 5 };
        int[] inOrder = new int[] { 2, 1, 4, 3, 5 };

        int start = 0;
        int end = inOrder.length -1;
        Node root =constructTree(preOrder, inOrder, start, end);

        System.out.println("In order Tree"); root.inOrder(root);
        System.out.println("");
        System.out.println("Pre order Tree"); root.preOrder(root);
        System.out.println("");

    }

    public static int preInd = 0;
    public static Node constructTree(int[] pre, int[] in, int start, int end) {
        if (start > end) {
            return null;
        }

        int nodeVal = pre[preInd++];
        Node node = new Node(nodeVal);
        if (start != end) {
            int ind = findInNode(nodeVal, in, start, end);
            node.left = constructTree(pre, in, start, ind-1);
            node.right = constructTree(pre, in, ind+1, end);
        }
        return node;
    }

    public static int findInNode(int nodeVal, int[] in, int start, int end) {
        int i = 0;
        for (i = start; i <= end; i++) {
            if(in[i] == nodeVal)
            {
                return i;
            }
        }
        return -1;
    }

}

【问题讨论】:

  • 如果您在树中插入所有元素,这怎么可能是次线性的?至少 O(N),闭上眼睛。

标签: algorithm binary-tree time-complexity tree-traversal


【解决方案1】:

要估计运行时复杂度,让我们从简单的开始,findInNode

TfindInNode = Ο(n)

估计constructTree 有点困难,因为我们有递归调用。但是我们可以使用这种模式将 ... 拆分为本地成本和递归成本:

每次调用constructTree,我们的本地成本为TfindInNode = Ο(n) 和两次递归调用@987654324 @ 用 n-1 代替 n。所以

TconstructTree(n) = TfindInNode(n) + 2 · TconstructTree(n-1))

由于constructTree 的递归调用次数随着constructTree 的每次调用而翻倍,递归调用树随着每个递归步骤的增长如下:

                  n                    | 2^0·n = 1·n
         _________|_________           |
        |                   |          |
       n-1                 n-1         | 2^1·n = 2·n
    ____|____           ____|____      |
   |         |         |         |     |
  n-2       n-2       n-2       n-2    | 2^2·n = 4·n
  / \       / \       / \       / \    |
n-3 n-3   n-3 n-3   n-3 n-3   n-3 n-3  | 2^3·n = 8·n

所以constructTree第一次调用后constructTree的总调用次数为n,第一步递归调用后为n+2 ·n调用,第二步后为n+2·n+4·n,以此类推在。由于这个递归调用树的总深度为 n(每次递归 n 减 1),constructTree 的总调用次数总计为:

20 + 21 + 22 + … + 2n = 2n+1-1

因此:

TconstructTree(n) = (2n+1-1)· n ∈ Ο(2n).

所以你的算法具有指数时间复杂度。

空间复杂度也是 Ο(2n),因为每次递归调用 constructTree 的本地空间成本为 1。

【讨论】:

  • 我不同意推导复杂性的分析。 2个递归调用正在构造二叉树,因此最终它们构造了n个节点。对于每个构造的节点,搜索需要 O(n)。因此复杂度为 O(n^2)。
【解决方案2】:

时间复杂度 = O(n^2)。 2 次递归调用需要 O(n) 来构造二叉树,每个节点构造需要 O(n) 进行顺序搜索,即 O(n^2)。

空间复杂度 = 常量忽略 2 个输入数组和构造的二叉树,即输出

【讨论】:

    猜你喜欢
    • 2021-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-03
    • 2023-03-04
    • 1970-01-01
    • 2015-11-17
    相关资源
    最近更新 更多