0
0
DSA Goprogramming~30 mins

BST Delete Operation in DSA Go - Build from Scratch

Choose your learning style9 modes available
BST Delete Operation
📖 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.