0
0
DSA C++programming~10 mins

Floor and Ceil in BST 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 find the floor value in a BST.

DSA C++
int floorInBST(Node* root, int key) {
    int floor = -1;
    while (root != nullptr) {
        if (root->data == key) {
            floor = root->data;
            break;
        } else if (root->data > key) {
            root = root->[1];
        } else {
            floor = root->data;
            root = root->right;
        }
    }
    return floor;
}
Drag options to blanks, or click blank then click option'
Achild
Bright
Cparent
Dleft
Attempts:
3 left
💡 Hint
Common Mistakes
Moving to right child when current node's data is greater than key.
Using parent pointer which may not exist.
2fill in blank
medium

Complete the code to find the ceil value in a BST.

DSA C++
int ceilInBST(Node* root, int key) {
    int ceil = -1;
    while (root != nullptr) {
        if (root->data == key) {
            ceil = root->data;
            break;
        } else if (root->data < key) {
            root = root->[1];
        } else {
            ceil = root->data;
            root = root->left;
        }
    }
    return ceil;
}
Drag options to blanks, or click blank then click option'
Aright
Bleft
Cparent
Dchild
Attempts:
3 left
💡 Hint
Common Mistakes
Moving to left child when current node's data is less than key.
Using parent pointer which may not exist.
3fill in blank
hard

Fix the error in the code to correctly find the floor in BST.

DSA C++
int floorInBST(Node* root, int key) {
    int floor = -1;
    while (root != nullptr) {
        if (root->data == key) {
            floor = root->data;
            break;
        } else if (root->data < key) {
            floor = root->data;
            root = root->[1];
        } else {
            root = root->left;
        }
    }
    return floor;
}
Drag options to blanks, or click blank then click option'
Aleft
Bright
Cparent
Dchild
Attempts:
3 left
💡 Hint
Common Mistakes
Moving to left child when current node's data is less than key.
Using parent pointer which may not exist.
4fill in blank
hard

Fill both blanks to complete the code that finds ceil in BST.

DSA C++
int ceilInBST(Node* root, int key) {
    int ceil = -1;
    while (root != nullptr) {
        if (root->data == key) {
            ceil = root->data;
            break;
        } else if (root->data > key) {
            ceil = root->data;
            root = root->[1];
        } else {
            root = root->[2];
        }
    }
    return ceil;
}
Drag options to blanks, or click blank then click option'
Aleft
Bright
Cparent
Dchild
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping left and right child pointers.
Using parent pointer which may not exist.
5fill in blank
hard

Fill all three blanks to complete the code that finds floor and ceil in BST.

DSA C++
pair<int, int> floorAndCeil(Node* root, int key) {
    int floor = -1, ceil = -1;
    while (root != nullptr) {
        if (root->data == key) {
            floor = root->data;
            ceil = root->data;
            break;
        } else if (root->data < key) {
            floor = root->data;
            root = root->[1];
        } else {
            ceil = root->data;
            root = root->[2];
        }
    }
    return {floor, ceil};
}

int main() {
    Node* root = nullptr;
    // Assume BST is built here
    int key = 15;
    auto result = floorAndCeil(root, key);
    cout << "Floor: " << result.[3] << ", Ceil: " << result.second << endl;
    return 0;
}
Drag options to blanks, or click blank then click option'
Aright
Bleft
Cfirst
Dsecond
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping left and right pointers.
Accessing wrong pair element for floor.