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

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

A variation without static. Still wrong: if you call size() more than once on any particular object instance, the value returned will be wrong for that instance!

Here is a trace of an execution.

Previous pageContentsNext page