-4
def percolate(int i):
  l = 2*i + 1
  r = 2*i + 2
  max = i
  temp = new node()

  if (l!=-1 and arr[l] > arr[i]):
    max = l

  if (r!=-1 and arr[r] > arr[max]):
    max = r

  if (max != i):
    temp = arr[i]
    arr[i] = arr[max]
    arr[max] = temp

  percolate(max)

Time Complexity = O(logn)

But what is the space complexity? O(1) or O(logn)

Thanks, Kuldeep

1 Answers1

1

percolate is a recursive function whose implicit input is the global array arr of size n. The function may call itself recursively O(lg n) times, with each invocation doing O(1) work, leading to your O(lg n) running time. However, each call allocates a constant amount of temporary storage.

If the implementation language allocates this memory on a stack and reuses stack frames via tail-recursion optimization, then the total memory usage of the algorithm is indeed O(1).

However, if there is no tail-recursion optimization, the memory allocated for each call remains in use and not reclaimed until that particular call returns, which will not be until after all the recursive calls below it return. For a call tree with O(lg n) height, that means O(lg n) memory used by the function.

All this goes to show that an algorithm which uses O(1) memory in theory can be implemented inefficiently to use O(lg n) memory in practice.

chepner
  • 497,756
  • 71
  • 530
  • 681
  • Thanks got it! you are right about limit for infinite looping. My next question is about simple PreOrder traversal: preorder(node root): if root != null: print root->data preorder(root->left) preorder(root->right) – user3931027 Aug 19 '14 at 23:49