📖 Scenario: Imagine you are managing a phone book stored as a Binary Search Tree (BST). You want to remove a contact by their phone number while keeping the tree organized.
🎯 Goal: You will build a program that creates a BST with given phone numbers, sets a target phone number to delete, performs the delete operation, and then prints the BST in-order to show the updated phone book.
📋 What You'll Learn
Create a BST node structure with integer phone numbers
Insert nodes to build the BST with given phone numbers
Set a variable for the phone number to delete
Implement the BST delete function handling all cases
Print the BST in-order after deletion
💡 Why This Matters
🌍 Real World
BSTs are used in databases and file systems to quickly find, add, or remove records like phone numbers.
💼 Career
Understanding BST delete operation is important for software engineers working with data storage, search algorithms, and system design.
Progress0 / 4 steps
1
Create BST with given phone numbers
Create a struct called Node with int data, Node *left, and Node *right. Then create a function newNode that takes an int data and returns a pointer to a new node with data set and left and right set to NULL. Finally, create a function insert that takes Node *root and int data and inserts the data into the BST correctly. Use insert to build a BST with these exact phone numbers in this order: 50, 30, 70, 20, 40, 60, 80. Store the root in a variable called root.
DSA C
Hint
Start by defining the node structure and helper functions to create and insert nodes. Then insert the given phone numbers one by one.
2
Set the phone number to delete
Create an integer variable called key and set it to 50. This is the phone number you want to delete from the BST.
DSA C
Hint
Just create an integer variable named key and assign it the value 50.
3
Implement BST delete function
Write a function minValueNode that takes Node *node and returns the node with the smallest value in that subtree. Then write a function deleteNode that takes Node *root and int key and deletes the node with key from the BST. Handle all three cases: node with no child, one child, and two children. Use minValueNode to find the inorder successor when needed. Update root by calling deleteNode(root, key) in main.
DSA C
Hint
Use recursion to find the node to delete. Handle no child, one child, and two children cases. Use minValueNode to find the smallest node in the right subtree for replacement.
4
Print BST in-order after deletion
Write a function inorder that takes Node *root and prints the BST nodes in ascending order separated by spaces. Call inorder(root) in main after deleting the node to show the updated BST.
DSA C
Hint
Use recursion to print left subtree, then current node data, then right subtree. Call this after deletion to see the updated BST.
Practice
(1/5)
1. What happens when you delete a leaf node (a node with no children) from a Binary Search Tree (BST)?
easy
A. The node is replaced by its right child.
B. The entire tree is restructured to maintain balance.
C. The node is replaced by its left child.
D. The node is simply removed and its parent's pointer is set to null.
Solution
Step 1: Understand leaf node deletion
A leaf node has no children, so removing it does not affect other nodes.
Step 2: Update parent's pointer
After removal, the parent's pointer to this node is set to null to disconnect it.
Final Answer:
The node is simply removed and its parent's pointer is set to null. -> Option D
Quick Check:
Leaf node deletion = remove node and set parent's pointer null [OK]
Hint: Leaf node deletion just removes node, no child replacement needed [OK]
Common Mistakes:
Replacing leaf node with child when none exists
Restructuring entire tree unnecessarily
Confusing leaf node with internal node deletion
2. Which of the following is the correct syntax to delete a node with value key from a BST in C?
easy
A. root = deleteNode(root, key);
B. deleteNode(root, key);
C. delete(root, key);
D. root.deleteNode(key);
Solution
Step 1: Understand function call and assignment
Deleting a node may change the root, so the returned root must be assigned back.
Step 2: Check syntax correctness
root = deleteNode(root, key); correctly assigns the returned root from deleteNode to root.
Final Answer:
root = deleteNode(root, key); -> Option A
Quick Check:
Assign returned root after deletion [OK]
Hint: Always assign returned root after deletion to handle root changes [OK]
Common Mistakes:
Not assigning returned root causing lost tree updates
Using incorrect function names
Using object-oriented syntax in C
3. Given the BST below and the code snippet to delete node with value 15: