ITPEC FE Subject B October 2025 Question 13
From the answer group below, select the correct combination of answers to be inserted into A through C in the program.
Binary Search Trees (BST) are a fundamental data structure where each node has at most two children: a left child and a right child. The key property of a BST is that each value of all nodes in its left subtree are less than the value of the node, and each value of all nodes in its right subtree are greater than the value of the node. The table shows the member variables of the class Node. Node-type variable holds a reference to an instance of the class Node. Each node in a BST has a different value.
Table Member variables of class Node
The function remove removes a node with the same key value as the argument key from the BST specified by the argument node and returns the resulting BST. The function searches for a node with the same key value, and if the found node (call it X here) does not have a right subtree, the function returns its left subtree. If X has a right subtree, the function locates the node (call it Y here) with the smallest key value in the right subtree, replaces the key value of X with that of Y, and then removes Y. If there is no node with the same key value in the BST, the function performs no operations. The procedure test demonstrates this behavior by removing 1 node from a BST containing 7 nodes. The figure shows the BST before and after node removal.
Figure BST before and after node removal
Answer group
Explanation
A: node.right, B: node2.left is not undefined, C: node2.key — find Y as the leftmost node of X's right subtree, copy its key into X, then delete Y from that right subtree by its key.
The description fixes the method: Y is the node with the smallest key in X's right subtree. The smallest key of a BST is found by starting at its root and following left until there is no left child. So node2 starts at node. (A) and moves left while node2. is not undefined (B). After node., X holds Y's key, and Y must be removed from the right subtree by that key, remove( (C).
Trace remove( on the figure:
- •At 5: 2 < 5, so recurse into the left subtree.
- •At 2: the keys are equal and 2 has a right child (4), so take the
elsebranch. - •
node2← 4; 4 has a left child 3, sonode2← 3; 3 has no left child, stop. - •Node 2's key becomes 3, then
remove(: 3 < 4, recurse left; node 3 has no right subtree, so it returns its left subtree (4, 3) undefined) and 4 loses its left child.
The result, 5 over 3 and 6, with 1 and 4 under 3 and 7 under 6, is the tree on the right of the figure.
Answer (h)
Why not others:
- •(a), (b), (c) and (d) —
node.starts the search in X's left subtree, whose keys are all smaller than X's, so it can never reach the smallest key of the right subtree that the description asks forleft - •(e) and (f) — every key in X's right subtree is greater than
key, sonode2.is false at once andkey < key node2never leavesnode.. Here X would get key 4 instead of 3, and the old node 3 would stay in the right subtree of the new 4, breaking the BST orderright - •(g) —
remove(searches the right subtree for the original key 2, which is not there, so nothing is removed and the tree ends up with two nodes holding 3node. right, key)
Key rule: To delete a node with two children, copy in the key of its in-order successor (the leftmost node of the right subtree), then delete the successor by that key; finding a minimum is a walk down left links, not a key comparison.
Binary Search Tree · AI-written, may contain errors
This question comes from an official ITPEC past paper. ITPEC Practice is an independent study tool and is not affiliated with ITPEC. See the official FE past-paper collection or Report an issue.