0
0
DSA C++programming~10 mins

BST Insert Operation in DSA C++ - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a new node with the given value.

DSA C++
struct Node {
    int data;
    Node* left;
    Node* right;
    Node(int val) : data(val), left(nullptr), right(nullptr) {}
};

Node* insert(Node* root, int val) {
    if (root == nullptr) {
        return new [1](val);
    }
    // rest of insert logic
    return root;
}
Drag options to blanks, or click blank then click option'
ANode*
BTree
CBST
DNode
Attempts:
3 left
💡 Hint
Common Mistakes
Using pointer type 'Node*' instead of the struct name 'Node'.
Using an undefined type like 'Tree' or 'BST'.
2fill in blank
medium

Complete the code to insert the value into the left subtree if it is smaller than the root's data.

DSA C++
if (val < root->data) {
    root->left = [1](root->left, val);
}
Drag options to blanks, or click blank then click option'
Ainsert
BnewNode
CcreateNode
DaddNode
Attempts:
3 left
💡 Hint
Common Mistakes
Using a function name that does not exist like 'newNode' or 'createNode'.
Not calling the function recursively.
3fill in blank
hard

Fix the error in the code to insert the value into the right subtree if it is greater than the root's data.

DSA C++
if (val > root->data) {
    root->right = [1](root->right, val);
}
Drag options to blanks, or click blank then click option'
Ainsert
BinsertNode
CaddRight
DnewNode
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect function names like 'insertNode' or 'addRight'.
Not calling the function recursively.
4fill in blank
hard

Fill both blanks to complete the insert function that returns the root after insertion.

DSA C++
Node* insert(Node* root, int val) {
    if (root == nullptr) {
        return new [1](val);
    }
    if (val < root->data) {
        root->left = [2](root->left, val);
    } else if (val > root->data) {
        root->right = insert(root->right, val);
    }
    return root;
}
Drag options to blanks, or click blank then click option'
ANode
Binsert
CNode*
DaddNode
Attempts:
3 left
💡 Hint
Common Mistakes
Using pointer type 'Node*' instead of 'Node' for new node creation.
Using wrong function names for recursion.
5fill in blank
hard

Fill all three blanks to complete the insert function with handling for equal values (ignore duplicates).

DSA C++
Node* insert(Node* root, int val) {
    if (root == nullptr) {
        return new [1](val);
    }
    if (val < root->data) {
        root->left = [2](root->left, val);
    } else if (val > root->data) {
        root->right = [3](root->right, val);
    } else {
        // Duplicate value, do nothing
        return root;
    }
    return root;
}
Drag options to blanks, or click blank then click option'
ANode
Binsert
DNode*
Attempts:
3 left
💡 Hint
Common Mistakes
Using pointer type 'Node*' instead of 'Node' for new node creation.
Not handling duplicates properly.
Using wrong function names for recursion.