CMPT 225 Lecture Notes - Wrapper Function, Local Variable

33 views2 pages

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.

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents