Skip to main content

Data Structures · Search trees and balanced trees

Consider BST deletion of a node z

Problem

Consider BST deletion of a node \(z\). Case 0: \(z\) has no children. Case 1: \(z\) has exactly one child. Case 2: \(z\) has two children. Write complete pseudocode \(\mathrm{TREE}\text{-}\mathrm{DELETE}(T, z)\) that handles all three cases, using parent pointers, and that in Case 2 replaces \(z\)'s key by its successor \(y\) and then splices \(y\) out of the tree (\(y\) has no left child). Prove that after \(\mathrm{TREE}\text{-}\mathrm{DELETE}\), the BST property holds on the remaining keys. Your proof must treat Case 2 separately and must argue that moving \(y\)'s key into \(z\)'s node cannot violate the BST property at \(z\) or at any ancestor of \(z\).

Hint

Zero- and one-child deletions are splices: the parent of \(z\) is rewired to \(z\)’s unique child (or to \(\mathrm{NIL}\)). Two-child deletion cannot splice \(z\) without losing a subtree.

Check your work

Work the problem yourself first. Then open it in Training to check your answer and read the full worked solution.

The answer check and full solution for this problem come with ProofAnvil Practice membership ($19 USD monthly). See membership. Or start with the free Data Structures sample problem: Try the free sample problem.

More Data Structures practice problems

Back to Data Structures

An original ProofAnvil practice problem, written for this course. ProofAnvil is a practice course, not a homework-answer service.