0
0
DSA Goprogramming~10 mins

BST Delete Operation in DSA Go - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a new BST node with the given key.

DSA Go
func newNode(key int) *Node {
    return &Node{
        key: [1],
        left: nil,
        right: nil,
    }
}
Drag options to blanks, or click blank then click option'
Akey
Bnil
CnewNode
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using nil or zero instead of the key parameter.
Trying to call newNode recursively inside itself.
2fill in blank
medium

Complete the code to find the minimum value node in a BST.

DSA Go
func minValueNode(node *Node) *Node {
    current := node
    for current.left != [1] {
        current = current.left
    }
    return current
}
Drag options to blanks, or click blank then click option'
Anode
BnewNode
Ccurrent
Dnil
Attempts:
3 left
💡 Hint
Common Mistakes
Checking for node instead of nil.
Using current instead of nil in the condition.
3fill in blank
hard

Fix the error in the delete function to correctly delete a node with two children.

DSA Go
func deleteNode(root *Node, key int) *Node {
    if root == nil {
        return root
    }
    if key < root.key {
        root.left = deleteNode(root.left, key)
    } else if key > root.key {
        root.right = deleteNode(root.right, key)
    } else {
        if root.left == nil {
            return root.right
        } else if root.right == nil {
            return root.left
        }
        temp := [1](root.right)
        root.key = temp.key
        root.right = deleteNode(root.right, temp.key)
    }
    return root
}
Drag options to blanks, or click blank then click option'
AminValueNode
BdeleteNode
CnewNode
DmaxValueNode
Attempts:
3 left
💡 Hint
Common Mistakes
Using maxValueNode instead of minValueNode.
Calling deleteNode instead of minValueNode to find replacement.
4fill in blank
hard

Fill both blanks to complete the in-order traversal function that prints BST keys.

DSA Go
func inorder(root *Node) {
    if root != [1] {
        inorder(root.[2])
        fmt.Print(root.key, " -> ")
        inorder(root.right)
    }
}
Drag options to blanks, or click blank then click option'
Anil
Bleft
Cright
Droot
Attempts:
3 left
💡 Hint
Common Mistakes
Checking root against left or right instead of nil.
Traversing right subtree first instead of left.
5fill in blank
hard

Fill all three blanks to complete the delete function that handles all cases correctly.

DSA Go
func deleteNode(root *Node, key int) *Node {
    if root == [1] {
        return root
    }
    if key < root.key {
        root.left = deleteNode(root.left, [2])
    } else if key > root.key {
        root.right = deleteNode(root.right, key)
    } else {
        if root.left == nil {
            return root.right
        } else if root.right == nil {
            return root.left
        }
        temp := minValueNode(root.right)
        root.key = temp.key
        root.right = deleteNode(root.right, [3])
    }
    return root
}
Drag options to blanks, or click blank then click option'
Anil
Bkey
Ctemp.key
Droot.key
Attempts:
3 left
💡 Hint
Common Mistakes
Using root.key instead of key in recursive calls.
Not checking for nil root at the start.