CSC403: Tree code: Local variables don't matter [18/29] |
public int size () { return size (root); } private static int size (Node x) { if (x == null) return 0; int szl = size (x.left); int szr = size (x.right); return szl + szr + 1; }
Local variables don't matter.
Here is a trace of an execution.
This is static single assignment (SSA) form: each variable is assigned on exactly one line of code.
Most compilers translate your code into SSA as part of the compilation process.
In SSA, the right-hand-side of the assignment may be restricted to be either a single function call or a single operator. In this case, the last line would be translated to:
int tmp1 = szl + szr; int tmp2 = tmp1 + 1; return tmp2;
Note: You don't need to remember SSA for exams. It's informational.