Algorithm 需要找出我的算法的时间和空间复杂度
不知何故,我成功地编写了一个算法,用它的按序和按序遍历数据构造二叉树 我不知道如何计算这个算法的时间和空间复杂度 我猜是Algorithm 需要找出我的算法的时间和空间复杂度,algorithm,binary-tree,time-complexity,tree-traversal,Algorithm,Binary Tree,Time Complexity,Tree Traversal,不知何故,我成功地编写了一个算法,用它的按序和按序遍历数据构造二叉树 我不知道如何计算这个算法的时间和空间复杂度 我猜是 first pass --> n(findInNode) + n/2 (constructTree) + n/2 (constructTree) second pass --> n/2(findInNode) + n/4 (constructTree) + n/4 (constructTree) etc.. 所以它应该是大约(3logn) 如果我错了,请纠正我
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;
}
}
公共类构造树{
公共静态void main(字符串[]args){
int[]预序=新的int[]{1,2,3,4,5};
int[]inoorder=newint[]{2,1,4,3,5};
int start=0;
int end=inOrder.length-1;
节点根=构造树(前序、顺序、开始、结束);
System.out.println(“顺序树”);root.inoorder(root);
System.out.println(“”);
System.out.println(“预订单树”);root.preOrder(root);
System.out.println(“”);
}
公共静态int preInd=0;
公共静态节点构造树(int[]pre,int[]in,int start,int end){
如果(开始>结束){
返回null;
}
int nodeVal=pre[preInd++];
节点=新节点(nodeVal);
如果(开始!=结束){
int ind=findInNode(nodeVal、in、start、end);
node.left=构造树(pre、in、start、ind-1);
node.right=constructTree(pre、in、ind+1、end);
}
返回节点;
}
公共静态int findInNode(int nodeVal,int[]in,int start,int end){
int i=0;
对于(i=start;iTime complexity=O(n^2)。2递归调用使用O(n)构造二叉树,每个节点构造使用O(n)进行顺序搜索,即O(n^2)
空间复杂度=常数忽略2个输入数组和作为输出的构造的二叉树时间复杂度=O(n^2)。2个递归调用使用O(n)构造二叉树,每个节点构造使用O(n)进行顺序搜索,即O(n^2)
空间复杂度=常数忽略2个输入数组和作为输出的构造的二叉树要估计运行时复杂度,让我们从简单的一个开始,findInNode
:
TfindInNode=⑩(n)
由于我们有递归调用,因此估算constructTree有点困难。但我们可以使用此模式将…分为本地成本和递归成本:
对于constructTree
的每次调用,我们都有TfindInNode=Ⅹ(n)的局部代价,以及两次递归调用constructTree
,使用n-1代替n。因此
T构造树(n)=TfindInNode(n)+2·t构造树(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
因此:
T构造树(n)=(2n+1-1)·n∈ ⑩(2n)
因此,您的算法具有指数时间复杂度
由于每次递归调用constructTree
的局部空间开销为1,因此空间复杂度也为Ⅹ(2n)。为了估计运行时复杂度,让我们从简单的一个开始,findInNode
:
TfindInNode=⑩(n)
由于我们有递归调用,因此估算constructTree有点困难。但我们可以使用此模式将…分为本地成本和递归成本:
对于constructTree
的每次调用,我们都有TfindInNode=Ⅹ(n)的局部代价,以及两次递归调用constructTree
,使用n-1代替n。因此
T构造树(n)=TfindInNode(n)+2·t构造树(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
因此:
T构造树(n)=(2n+1-1)·n∈ ⑩(2n)
因此,您的算法具有指数时间复杂度
由于每次递归调用constructTree
的局部空间开销为1,因此空间复杂度也为Ⅹ(2n)。如果闭上眼睛在树中插入所有元素,这怎么可能是次线性的???至少O(N)。如果在树中插入所有元素,这怎么可能是次线性的???至少O(N),闭上眼睛。我不同意推导复杂度的分析。2个递归调用正在构造二叉树,因此最终它们构造了n个节点。搜索对每个构造的节点都需要O(n)。因此复杂度为O(n^2).我不同意推导复杂度的分析。2个递归调用正在构造二叉树,因此最终它们构造了n个节点。搜索对每个构造的节点都取O(n)。因此复杂度为O(n^2)。