Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Implement 2 functions, int min(TNode*) and int max(TNode*) that take a pointer t

ID: 3778806 • Letter: I

Question

Implement 2 functions, int min(TNode*) and int max(TNode*) that take a pointer to a TNode and return the smallest (min) and largest (max) node in the BST. Implement two versions of each function, one version using a loop, the other using recurssion. To find the largest value in BST with a loop, you need to keep moving the node pointer right until you reach a node with a null right child. The data at that node is the max. Smallest value is found by going left as far as possible. Recursively, to find the largest value, if node t has no child, it contains the answer, otherwise the answer is the largest value in the right subtree. Test the functions on the BST built in the last assignment.

Explanation / Answer

below are the two function for finding Max & Min element using both ways:

struct TNode
{
    int data;
    struct Tnode* left;
    struct Tnode* right;
};

1> Iterative method

int min(struct TNode* node) {
struct TNode* current = node;

while (current->left != NULL) {
    current = current->left;
}
return(current->data);
}

int max(struct TNode* node) {
struct TNode* current = node;

while (current->right != NULL) {
    current = current->right;
}
return(current->data);
}

2> Recursive

int min(struct TNode* root)
{
       if(root == null) {
           return -1;
       }
      
       if(root->left == null) {
           return root->data;
       }
       return min(root.left);
}
  
int max(struct TNode* root)
{
       if(root == null) {
           return -1;
       }

       if(root->right == null) {
           return root->data;
       }
       return max(root->right);
}

feel free to aksk if you have any doubt :)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote