CSC403: More Storage: Is this correct? [7/7] Previous pageContents

    public int size () {
        sz = 0;
        return size (root);
    }
    private static int sz;
    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 your code is single threaded. If two threads call size() on any instance of the containing class the same time, the sz field could become a mess.

Previous pageContents