Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Space complexity on creating an object on recursion

checkIfBalanced() method in the code below returns true if the tree is balanced and false otherwise. However in each recursion it creates TreeData object. In my opinion space complexity is O(1) as after each stack frame is popped, the reference of the object created on that stack frame is lost and garbage collected. Am I right ?

Please note:

  1. I am not looking for suggestions to improve/change my code. The following example of code is tailor made to ask my question.

  2. Also, please ignore space-complexity adding stack frames. I am looking for space complexity of number 'TreeData' objects created. It looks to me that at any point there would be only 3 TreeData objects. Thats what I want to verify. Thanks.

   private static class TreeData {
        private int height;
        private boolean isBalanced; 

        TreeData(int height, boolean isBalanced) {
            this.height = height;
            this.isBalanced = isBalanced;
        }
    }

    public boolean checkIfBalanced() {
        if (root == null) {
            throw new IllegalStateException();
        }
        return checkBalanced(root).isBalanced;
    }

    public TreeData checkBalanced(TreeNode node) {
        if (node == null) return new TreeData(-1, true);

        TreeData tdLeft = checkBalanced(node.left);
        TreeData tdRight = checkBalanced(node.right);

        if (tdLeft.isBalanced && tdRight.isBalanced && Math.abs(tdLeft.height - tdRight.height) <= 1) {
            return new TreeData(Math.max(tdLeft.height, tdRight.height) + 1, true);
        } 

        return new TreeData(Math.max(tdLeft.height, tdRight.height) + 1, false);
    }
like image 342
JavaDeveloper Avatar asked Feb 07 '15 17:02

JavaDeveloper


People also ask

What is the space complexity of recursion?

To conclude, space complexity of recursive algorithm is proportinal to maximum depth of recursion tree generated. If each function call of recursive algorithm takes O(m) space and if the maximum depth of recursion tree is 'n' then space complexity of recursive algorithm would be O(nm).

Does recursion stack count in space complexity?

Stack space in recursive calls counts too as extra space required by a program. For example : C++

What is space complexity with example?

Space complexity includes both Auxiliary space and space used by input. For example, if we want to compare standard sorting algorithms on the basis of space, then Auxiliary Space would be a better criterion than Space Complexity.


1 Answers

In my opinion space complexity is O(1) as after each stack frame is popped, the reference of the object created on that stack frame is lost and garbage collected. Am I right ?

No, you are wrong in assuming that. Ray Toal has very beautifully explained this. At any point in the recursion, you must count all those internal stack frames which are being saved into memory, as Space-Complexity considers into account all the auxiliary space(extra space) alongwith the input(not emphasised here).

Next,

I am looking for space complexity of number 'TreeData' objects created.

The maximum space consumed by a recursive program is proportional to the maximum depth of the recursion tree framed. The maximum depth of the recursion tree is defined as the length of the longest path in the tree.

For the given program, the program will start from root of the tree, and first start creating the left sub-tree and then checking the right-subtree. Finally, it will return the length of the longest path and whether the tree is balanced OR not.

It looks to me that at any point there would be only 3 TreeData objects. That's what I want to verify.

No, at any particular execution time, first all the left subtrees are verified and then the right-subtrees are verified, so the number of objects of TreeData in the memory would be only 1. It will every time check for it's left OR right child. And, hence, all those (log n --- in case of balanced tree, OR n --- in case of degenerate tree) nodes except one will keep getting stacked in the memory. At a particular moment of time, only 1 TreeData object would be in the active state, others would be paused and stacked in the memory and waiting for their turn to pop out.

like image 177
Am_I_Helpful Avatar answered Oct 15 '22 23:10

Am_I_Helpful