Trying to write a function to search a binary tree to get the maximum value and
ID: 3633812 • Letter: T
Question
Trying to write a function to search a binary tree to get the maximum value and display?Explanation / Answer
template tnode *max(tnode *t) { // we will return maxValPtr tnode *maxValPtr,*leftMax, *rightMax; if (t == NULL) maxValPtr=NULL; else { // temporarily assume t is pointing to the maximum maxValPtr=t; // leftMax points to the maximum value on the left subtree leftMax=max(t->left); // if leftMax is not NULL and the value to which it points // is larger, change maxValPtr if(leftMax!=NULL&&leftMax->nodeValue>maxValPtr->nodeValue) { maxValPtr=leftMax; } // rightMax points to the maximum value on the right subtree rightMax=max(t->right); // if rightMax is not NULL and the value to which it points // is larger, change maxValPtr if(rightMax!=NULL&&rightMax->nodeValue>maxValPtr->nodeValue) { maxValPtr=rightMax; } } return maxValPtr; // return a maxValPtr points to the node with maximum value for the tree }Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.