recursion - Time and space complexity of passing an array in Java -


suppose have recursive function works on balanced binary tree n nodes , height log(n) , call function below on root of tree.

void printpaths(treenode root, int[] array, int depth){     if (root == null){         print array;     }     array[depth] = root.data;     printpaths(root.left, array, depth + 1);     printpaths(root.right, array, depth + 1); }  array = new int[depthoftree] printpaths(root, array, 0); 

let array length log(n) (it store values along tree's paths). know recursion call stack max height log(n). i'm unsure of how "pass-by-value" nature of java , java garbage collection come play affect time , space complexity.

1) time complexity of passing array recursive call? if java "pass-by-value," each recursive call take o(log(n)) copy array before starting function execution?

2) upper bound of how many of these array copies floating around in memory @ 1 time? inclination o(log(n)). mean space complexity o(log(n)log(n))? i've read in book "the space complexity o(log(n)) since algorithm recurse o(log(n)) times , path parameter allocated once @ o(log(n)) space during recursion".

1) time complexity of passing array recursive call? if java "pass-by-value," each recursive call take o(log(n)) copy array before starting function execution?

just o(1). reference passed value. arrays reference types (even if element type of array value type, e.g. int[]).

so value of array expression isn't array object - it's reference.

2) upper bound of how many of these array copies floating around in memory @ 1 time?

exactly 1, same reason. you've got one array, , each level in stack has reference it.

the memory taken each recursive step enough space values of local variables on stack... constant amount of space per call. depth of o(log(n)), means space complexity of o(log(n)) too.


Comments

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

Does Firefox offer AppleScript support to get URL of windows? -

android - How to install packaged app on Firefox for mobile? -