📖 Scenario: You are managing a simple phone book stored as a Binary Search Tree (BST). Each node contains a unique phone number. You want to be able to remove a phone number from this phone book.
🎯 Goal: Build a program that creates a BST with given phone numbers, sets a phone number to delete, performs the delete operation on the BST, and prints the BST in sorted order after deletion.
📋 What You'll Learn
Create a BST by inserting given phone numbers in order
Create a variable to hold the phone number to delete
Implement the BST delete operation for the given phone number
Print the BST nodes in ascending 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 operations 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 an int data, and Node* pointers left and right. Then create a function insertNode that takes Node* root and int data and inserts the data into the BST. Finally, create a Node* root and insert these phone numbers in this order: 50, 30, 70, 20, 40, 60, 80.
DSA C++
Hint
Start by defining the Node structure with data and pointers. Then write the insertNode function to add nodes correctly. Finally, insert the given numbers into the BST.
2
Set the phone number to delete
Create an int variable called key and set it to 30. 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 30.
3
Implement BST delete operation
Write a function Node* deleteNode(Node* root, int key) that deletes the node with value key from the BST and returns the new root. Use the standard BST delete logic: if the node has two children, replace it with its inorder successor (smallest node in right subtree). Then, in main, update root by calling deleteNode(root, key).
DSA C++
Hint
Write the deleteNode function using recursion. Handle three cases: no child, one child, and two children. Use a helper function minValueNode to find the smallest node in the right subtree.
4
Print BST in sorted order after deletion
Write a function void inorder(Node* root) that prints the BST nodes in ascending order separated by spaces. Then call inorder(root) in main after deleting the node.
DSA C++
Hint
Use inorder traversal to print nodes in ascending order. Call inorder(root) after deletion and print a space after each node.
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 BST properties.
C. The node is simply removed and its parent's pointer is set to null.
D. The node is replaced by its left child.
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
The parent's pointer to this node is set to null, effectively removing the node.
Final Answer:
The node is simply removed and its parent's pointer is set to null. -> Option C