CMPT 225 Lecture Notes - Wrapper Function, Local Variable
Document Summary
Traversals (+ recursion) void print(node* nd) { if (nd != null) { print(nd->leftchild); cout << nd. data << endl; print(nd->right); Note: apparently this is broken int size() const { postordersize(root, 0); int postordersize(node* nd, int result) const { if (nd != null) { postordersize(nd->left, result); postordersize(nd->right, result); result++; return result; For a tree with n nodes, this will return 1. result is local to recursive call. What you do further down the tree has no impact on root"s result. Recursive function calls are just like any other function calls! If we try and change it, will end up basically looking just like first example. Or could pass by reference: int size() const { int result = 0; postordersize(root, result); return result; void postordersize(node* nd, int & result) const { if (nd != null) { postordersize(nd->left, result); postordersize(nd->right, result); result++; return result; Reference subtly difference from address, but whatever for right now.