Question 5 [5] with sample solutions
Based on the bstree class specified in the attached definitions page (at the back of the exam), implement the printTree function described below. For this question you may assume the bstree methods (in particular getTreeSize and getNth) have already been implemented.
// printTree displays all the ids in the tree, // in increasing order, one id per line void printTree(bstree *t);
SAMPLE SOLUTION void printTree(bstree *t) { if (t == NULL) return; int N = t->getTreesize(); for (int id = 1; id <= N; id++) { int idVal; if (t->getNth(idVal, root)) { cout << idVal << endl; } } } NOTES: observe that this is a function, and t is a pointer to an entire bstree, not to a node within the tree, so a recursive solution/inorder traversal is not possible. |