📖 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 structure valid.
🎯 Goal: Build a Go program that creates a BST, sets a value to delete, performs the delete operation correctly, and prints the BST in-order after deletion.
📋 What You'll Learn
Create a BST with these exact values inserted in order: 50, 30, 70, 20, 40, 60, 80
Create a variable called keyToDelete and set it to 50
Implement a function called deleteNode that deletes a node with the given key from the BST
Print the BST nodes in ascending order after deletion using in-order 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 operations is important for software engineers working on data storage, search engines, and performance-critical applications.
Progress0 / 4 steps
1
Create BST with given values
Create a struct called Node with Val int, Left *Node, and Right *Node. Then create a function called insert that inserts a value into the BST. Finally, create a BST by inserting these values in order: 50, 30, 70, 20, 40, 60, 80. Store the root in a variable called root.
DSA Go
Hint
Define the Node struct with integer value and pointers to left and right children. Use recursion in insert to place values correctly.
2
Set the key to delete
Create a variable called keyToDelete and set it to 50.
DSA Go
Hint
Use keyToDelete := 50 to create and set the variable.
3
Implement deleteNode function
Implement a function called deleteNode that takes root *Node and key int and returns *Node. It should delete the node with value key from the BST and return the new root. Use the standard BST delete logic: if node has two children, replace with inorder successor.
DSA Go
Hint
Use recursion to find the node. Handle three cases: no child, one child, two children. For two children, find the smallest node in right subtree (inorder successor) and replace.
4
Delete node and print in-order traversal
Call deleteNode with root and keyToDelete and assign the result back to root. Then implement a function called inOrder that prints the BST nodes in ascending order separated by spaces. Finally, call inOrder(root) to print the BST after deletion.
DSA Go
Hint
Call deleteNode with root and keyToDelete. Then print the BST using in-order traversal to show sorted values.
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's children replace it in the tree.
B. The entire tree is rebuilt from scratch.
C. The node is simply removed without affecting other nodes.
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 C
Quick Check:
Leaf node deletion = simple removal [OK]
Hint: Leaf nodes delete by direct removal only [OK]
Common Mistakes:
Thinking children need rearrangement
Assuming root always changes
Believing tree rebuild is needed
2. Which of the following Go function signatures correctly represents a BST delete operation for a node with integer keys?
easy
A. func deleteNode(key int, root *Node) *Node
B. func deleteNode(root *Node, key int) *Node
C. func deleteNode(root Node, key int) Node
D. func deleteNode(root *Node, key string) *Node
Solution
Step 1: Check parameter order and types
The root node pointer should be first, key integer second, returning updated root pointer.
Step 2: Validate pointer usage and key type
Root must be a pointer to allow tree modification; key must be int for integer keys.
Final Answer:
func deleteNode(root *Node, key int) *Node -> Option B
Quick Check:
Correct signature = root pointer first, int key [OK]
3. Given the BST below and the Go code to delete node with key 10, what is the in-order traversal output after deletion?
BST structure:
15
/ \
10 20
/ \
8 12
Go code snippet:
root = deleteNode(root, 10)
medium
A. [12, 15, 20]
B. [8, 15, 20]
C. [8, 10, 12, 15, 20]
D. [8, 12, 15, 20]
Solution
Step 1: Identify node to delete and its children
Node 10 has two children: 8 (left) and 12 (right).
Step 2: Replace node 10 with its in-order successor
In-order successor is 12. Replace 10 with 12, then delete original 12 node.
Step 3: Perform in-order traversal after deletion
Traversal order: 8, 12, 15, 20.
Final Answer:
[8, 12, 15, 20] -> Option D
Quick Check:
In-order after deleting 10 = [8, 12, 15, 20] [OK]
Hint: Replace deleted node with in-order successor [OK]
Common Mistakes:
Deleting successor node incorrectly
Skipping replacement step
Confusing in-order predecessor with successor
4. Consider this Go code snippet for deleting a node from a BST. What is the bug?
A. Missing assignment of root.right after deleting successor node.
B. Incorrect base case for empty tree.
C. Wrong comparison operators for key checks.
D. Returning nil instead of root in all cases.
Solution
Step 1: Analyze deletion of node with two children
After replacing root.key with successor's key, the successor node must be deleted from right subtree.
Step 2: Check recursive call effect on tree structure
The call deleteNode(root.right, temp.key) is made but its result is not assigned back to root.right, so the successor node remains.
Final Answer:
Missing assignment of root.right after deleting successor node. -> Option A
Quick Check:
Assign recursive delete result to root.right [OK]
Hint: Always assign recursive delete results to child pointers [OK]
Common Mistakes:
Ignoring returned subtree after deletion
Not updating pointers after recursive calls
Confusing base case with recursive case
5. You have a BST with nodes: 30, 20, 40, 10, 25, 35, 50. You delete node 30 (root). Which node becomes the new root after deletion using the standard BST delete operation?
hard
A. Node 35 (in-order successor)
B. Node 25 (in-order predecessor)
C. Node 40 (right child)
D. Node 20 (left child)
Solution
Step 1: Identify node to delete and its children
Node 30 is root with two children: left subtree (20) and right subtree (40).
Step 2: Find in-order successor of node 30
In-order successor is the smallest node in right subtree, which is 35.
Step 3: Replace root with in-order successor
Replace 30 with 35, then delete original 35 node from right subtree.
Final Answer:
Node 35 (in-order successor) -> Option A
Quick Check:
Root replaced by in-order successor = 35 [OK]
Hint: Root replaced by smallest node in right subtree [OK]
Common Mistakes:
Using in-order predecessor instead of successor
Choosing direct child without successor check
Not deleting successor node after replacement
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.