Question 2 [5] with sample solutions
Using the same node struct and tree assumptions as specified in question 1, implement an iterative (i.e. not recursive) solution for the findExtremes function described below.
// findExtremes takes three parameters: // a pointer to a tree node and two ints passed by reference. // The function finds the smallest and largest ids in the // subtree and copies them to the parameters void findExtremes(node *t, int &smallestId, int &largestId);
SAMPLE SOLUTION void findExtremes(node *t, int &smallestId, int &largestId) { if (t == NULL) return; node *sm = t; while (sm->left != NULL) sm = sm->left; smallestId = sm->id; node *lg = t; while (lg->right != NULL) lg = lg->right; largestId = lg->id; } |