Deleting from a BST has three cases. Leaf: remove it. One child: replace the node with its child. Two children: copy the inorder successor (smallest key in the right subtree) into the node, then delete the successor from the right subtree. The ordering is preserved in every case.
Remember
if (!t->left) { r = t->right; free(t); return r; }
if (!t->right) { l = t->left; free(t); return l; }
s = min_node(t->right);
t->key = s->key;
t->right = del(t->right, s->key);
Reference
Cheat Sheet
Keep these facts handy while you work with BST Deletion in C in C.
70 has two children; its successor 80 replaces it and the old 80 node is deleted.
30 now has only a right child (40), which takes its place.
Edge Cases & Pitfalls
Check these before relying on BST Deletion in C in real programs.
root
Not reassigning the root
Deletion can change the root, so assign root = del(root, k).
uaf
Using the freed node
Save the child pointer before free.
Remember
Key Takeaways
Three cases.
Two children: inorder successor.
Reassign the returned pointer.
One line: Deleting from a BST has three cases.
Frequently Asked Questions
Replace its key with the smallest key of the right subtree (its inorder successor), then delete that successor node.
Yes, the largest key in the left subtree works equally well.
Continue with "Balanced Trees and AVL Basics in C" next. Balance factors and rotations that keep a BST near O(log n). The C Trees and Binary Search Trees hub lists every topic in order.