CSC403: More Storage: Is this correct? [4/7] Previous pageContentsNext page

    public int size () {
        return size (root);
    }
    private static int sz = 0;
    private static int size (Node x) {
        if (x == null) return sz;
        sz = sz + 1;
        size (x.left);
        size (x.right);
        return sz;
    }

Is this correct?

Only if you call size exactly once, no matter which instance you call it on. If size() is called more than one time (regardless of the instance of the containing class) it gives the wrong answer.

Here is a trace of an execution.

Previous pageContentsNext page