📖 Scenario: You are managing a collection of unique numbers using a Binary Search Tree (BST). Sometimes, you need to remove a number from this collection while keeping the BST properties intact.
🎯 Goal: Build a program that creates a BST, sets a target number to delete, performs the delete operation correctly, and prints the BST in sorted order after deletion.
📋 What You'll Learn
Create a BST by inserting these exact numbers in order: 50, 30, 70, 20, 40, 60, 80
Create a variable called target and set it to 50
Implement a function called deleteNode that deletes target from the BST
Print the BST nodes in ascending order after deletion using an inorder traversal
💡 Why This Matters
🌍 Real World
BSTs are used in databases and file systems to keep data sorted and allow fast insertions, deletions, and lookups.
💼 Career
Understanding BST delete operation is important for software engineers working on data storage, search engines, and performance-critical applications.
Progress0 / 4 steps
1
Create the BST with given numbers
Create a class called Node with val, left, and right properties. Then create a class called BST with an empty root. Add an insert method to insert numbers. Insert these numbers in order: 50, 30, 70, 20, 40, 60, 80 into the BST instance called tree.
DSA Javascript
Hint
Define Node and BST classes first. Use a loop inside insert to find the correct place for each number.
2
Set the target number to delete
Create a variable called target and set it to 50.
DSA Javascript
Hint
Just create a constant named target and assign it the value 50.
3
Implement the deleteNode function
Add a method called deleteNode inside the BST class that takes root and key as parameters. It should delete the node with value key from the BST and return the new root. Use this method to delete target from tree.root and update tree.root.
DSA Javascript
Hint
Use recursion to find the node to delete. Handle three cases: no child, one child, two children. For two children, replace with smallest node in right subtree.
4
Print the BST in sorted order after deletion
Add a function called inorder that takes a node and returns an array of values in ascending order by traversing the BST in order. Use this function to print the sorted values of tree.root after deletion.
DSA Javascript
Hint
Use recursion to visit left child, then current node, then right child. Collect values in an array and print them joined by ' -> ' ending with ' -> null'.
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 simply removed without affecting other nodes.
B. The entire tree is rebuilt from scratch.
C. The node's children replace it in the tree.
D. The root node is always changed.
Solution
Step 1: Understand leaf node deletion
A leaf node has no children, so removing it does not affect other nodes.
Step 2: Effect on BST structure
Since no children exist, simply remove the node and update its parent's pointer to null.
Final Answer:
The node is simply removed without affecting other nodes. -> Option A
Quick Check:
Leaf node deletion = simple removal [OK]
Hint: Leaf nodes delete by direct removal only [OK]
Common Mistakes:
Thinking children replace the node when none exist
Assuming root always changes
Believing tree rebuild is needed
2. Which of the following is the correct syntax to delete a node with value key from a BST in JavaScript?
easy
A. bst.deleteNode(key);
B. bst.delete(key);
C. bst.remove(key);
D. bst.erase(key);
Solution
Step 1: Identify common BST delete method
In many JavaScript BST implementations, the method to delete a node is named delete.
Step 2: Check method naming conventions
Methods like remove, erase, or deleteNode are not standard or common in JavaScript BST examples.
Final Answer:
bst.delete(key); -> Option B
Quick Check:
Standard BST delete method = delete() [OK]
Hint: Use bst.delete(key) to remove nodes [OK]
Common Mistakes:
Using remove() or erase() which are not standard
Confusing delete keyword with method name
Using deleteNode() which is uncommon
3. Consider this BST and the delete operation:
10
/ \
5 15
/ \
12 20
What will be the in-order traversal output after deleting node with value 15?
medium
A. [5, 10, 12, 15, 20]
B. [5, 10, 20]
C. [5, 10, 12, 20]
D. [5, 10, 15, 20]
Solution
Step 1: Identify node to delete and its children
Node 15 has two children: 12 (left) and 20 (right).
Step 2: Replace node 15 with its in-order successor
The in-order successor is the smallest node in right subtree, which is 20. Replace 15 with 20 and delete original 20 node.