📢 Too many exams? Don’t know which one suits you best? Book Your Free Expert 👉 call Now!


    Question

    A C function insert(Node* root, int data) for a BST.

    #include typedef struct Node {     int data;     struct Node *left, *right; } Node; Node* newNode(int data) {     Node* temp = (Node*)malloc(sizeof(Node));     temp->data = data;     temp->left = temp->right = NULL;     return temp; } Node* insert(Node* root, int data) {     if (root == NULL) {         return newNode(data);     }     if (data < root->data) {         root->left = insert(root->left, data);     } else { // Handles data >= root->data         root->right = insert(root->right, data);     }     // Bug: Missing 'return root;' here } If this insert function is used to build a BST (e.g., root = insert(root, 50); root = insert(root, 30);), what is the most critical issue that will arise due to the missing return root; statement?
    A The tree will become unbalanced. Correct Answer Incorrect Answer
    B Duplicate values will not be handled correctly. Correct Answer Incorrect Answer
    C The function will return an undefined value, potentially corrupting tree links or causing a crash. Correct Answer Incorrect Answer
    D A memory leak will occur because malloc is used without free. Correct Answer Incorrect Answer
    E The else branch will never be executed. Correct Answer Incorrect Answer

    Solution

    • Why Correct Answer (C): The function will return an undefined value, potentially corrupting tree links or causing a crash. o This accurately describes the consequence of undefined behavior when a non-void function fails to return a value. The root pointer in the calling scope will receive an unpredictable value, leading to data corruption or a crash.

    Practice Next
    ask-question