Binary Search Trees: Insertion and Deletion Quiz Quiz

Challenge your understanding of Binary Search Trees by answering essential questions on insertion and deletion operations. This quiz covers BST fundamentals, step-by-step examples, and helps you identify common errors for beginners learning data structures.

  1. BST Insertion Process

    When inserting the value 8 into a Binary Search Tree that already contains 10 as a root and 6 as a left child, where will 8 be inserted?

    1. As the left child of 6
    2. As the left child of 10
    3. As the right child of 6
    4. As the right child of 10

    Explanation: In a BST, values less than the root go to the left and greater to the right. Since 8 is less than 10, we move left to 6, then 8 is greater than 6, so it becomes the right child of 6. Option 'As the left child of 6' is incorrect because 8 is not less than 6. Placing 8 as the left child of 10 would disregard the value at 6. The right child of 10 would be used if 8 were greater than 10. Only the correct option preserves the BST ordering rules.

  2. BST Definition

    Which property must always be maintained after inserting a new node into a Binary Search Tree?

    1. All right children are smaller than their parent
    2. The tree must be balanced
    3. Every left child is smaller than its parent
    4. All leaf nodes are at the same depth

    Explanation: In BSTs, for every node, the left child's value must be less than its parent and the right child's value must be greater. This ensures fast search and insert operations. Stating 'All right children are smaller' is incorrect as right children must be larger. The tree being balanced is not a requirement for standard BSTs, but rather for balanced BSTs. Requiring all leaves to be at the same depth applies to complete or perfect trees, not BSTs generally.

  3. Handling Duplicate Values

    What is the typical behavior when attempting to insert a value that already exists in a Binary Search Tree?

    1. The duplicate is placed as the left child
    2. A new root node is created
    3. The existing node is overwritten
    4. Duplicates are usually ignored

    Explanation: Most standard BST implementations disallow duplicate values and ignore insertions of values already present. Overwriting the existing node loses data, which is not standard for insertion. Creating a new root breaks the BST structure. Placing the duplicate as a left child violates the BST unique-key property. Only ignoring duplicates maintains a proper BST.

  4. Deleting a Leaf Node

    If you remove a leaf node (a node with no children) from a Binary Search Tree, what action is necessary?

    1. Replace it with its right subtree
    2. Replace it with its left subtree
    3. Search for a new root
    4. Remove the node directly from its parent

    Explanation: When deleting a leaf node in a BST, you simply disconnect it from its parent, as there are no children to worry about. Replacing it with its right or left subtree makes no sense since leaves have no subtrees. Searching for a new root is not required unless deleting the actual root node, which is a different case. The correct approach is straightforward and minimal.

  5. Deleting a Node with One Child

    During deletion in a BST, if a node with one child is removed, what happens to its child?

    1. The subtree is rebalanced
    2. The child becomes the new root
    3. The child is also deleted
    4. The child takes the deleted node’s place

    Explanation: For a BST node with one child, after deletion, the child replaces the deleted node in the parent's reference, maintaining tree integrity. Simultaneously deleting the child is incorrect since only the specified node should be removed. The child does not automatically become the root unless the root itself is deleted. Rebalancing does not happen automatically when deleting in a standard BST.

  6. Deleting a Node with Two Children

    To delete a node with two children in a BST, which value typically replaces the deleted node?

    1. The leftmost leaf node
    2. Any random child
    3. The in-order successor or predecessor
    4. The largest value in the entire tree

    Explanation: Replacing the deleted node with its in-order successor (smallest in right subtree) or predecessor (largest in left subtree) keeps BST ordering intact. Randomly choosing any child may break BST properties. The leftmost leaf might not be relevant unless it's the in-order successor. Selecting the largest value across the entire tree could be incorrect unless deleting the root and only two nodes exist. The standard practice is successor or predecessor.

  7. Insertion Order

    If you insert values 5, 2, 7, 4 into an empty BST in this order, which node becomes the right child of node 2?

    1. 4
    2. 2
    3. 7
    4. 5

    Explanation: After inserting 5 (root), then 2 (left of 5), and 7 (right of 5), inserting 4 places it right of 2 since 4 u003E 2 but u003C 5. The number 5 is the root, 7 is the right of 5, and 2 is left of 5. Attaching any node as the right child of 2 except for 4 in this insertion example doesn't follow the BST logic.

  8. BST Traversal after Deletion

    If you delete the root node of a BST and it has two children, which traversal visits the new root first?

    1. Post-order traversal
    2. Pre-order traversal
    3. Level-order traversal
    4. In-order traversal

    Explanation: Pre-order traversal always starts from the root, so after deletion and replacement, the new root is visited first. In-order and post-order might visit the root after traversing subtrees. Level-order could also visit the root first, depending on the tree, but pre-order specifically defines root as the starting point. Thus, pre-order is always guaranteed to visit the current root at the very beginning.

  9. Checking BST Property After Insertion

    After inserting a new node in a BST, which traversal can quickly confirm that all nodes maintain BST ordering?

    1. Post-order traversal
    2. In-order traversal
    3. Pre-order traversal
    4. Diagonal traversal

    Explanation: In-order traversal of a BST produces sorted order if the tree is correct. Pre-order and post-order don’t guarantee sorted output and cannot easily verify the BST property. Diagonal traversal is not standard for BST verification. Thus, in-order traversal is preferred for checking the ordering property after insertion.

  10. BST Child Replacement During Deletion

    In deleting a BST node with two children, and using the in-order successor, which subtree of the deleted node is reattached to the successor?

    1. No subtrees are reattached
    2. The right subtree only
    3. The parent’s subtree
    4. The left subtree

    Explanation: When replacing a node with its in-order successor, the replacement node inherits the left subtree of the deleted node, as it is guaranteed not to violate BST rules. Right subtree stays with the successor, while reattaching only the right subtree may break the left relationships. No subtrees or the parent’s subtree being reattached are invalid in this context. The process always involves the deleted node’s left subtree.