0
0
DSA Goprogramming~10 mins

BST Insert 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 node with the given value.

DSA Go
func newNode(value int) *Node {
    return &Node{data: [1], left: nil, right: nil}
}
Drag options to blanks, or click blank then click option'
Avalue
Bdata
Cnode
Dval
Attempts:
3 left
💡 Hint
Common Mistakes
Using an undefined variable like 'val' or 'node' instead of 'value'.
Confusing the field name with the parameter name.
2fill in blank
medium

Complete the code to insert a value into the BST recursively.

DSA Go
func insert(root *Node, value int) *Node {
    if root == nil {
        return newNode([1])
    }
    if value < root.data {
        root.left = insert(root.left, value)
    } else {
        root.right = insert(root.right, value)
    }
    return root
}
Drag options to blanks, or click blank then click option'
Aroot.data
Bnil
Cvalue
Droot
Attempts:
3 left
💡 Hint
Common Mistakes
Using root.data instead of value when creating the new node.
Returning nil instead of a new node.
3fill in blank
hard

Fix the error in the comparison to decide where to insert the new value.

DSA Go
if [1] < root.data {
    root.left = insert(root.left, value)
} else {
    root.right = insert(root.right, value)
}
Drag options to blanks, or click blank then click option'
Aroot.data
Broot.right
Croot.left
Dvalue
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing root.data with itself.
Using root.left or root.right in the comparison.
4fill in blank
hard

Fill both blanks to complete the BST insert function that returns the updated root.

DSA Go
func insert(root *Node, value int) *Node {
    if root == nil {
        return newNode([1])
    }
    if [2] < root.data {
        root.left = insert(root.left, value)
    } else {
        root.right = insert(root.right, value)
    }
    return root
}
Drag options to blanks, or click blank then click option'
Avalue
Broot.data
Croot.left
Droot.right
Attempts:
3 left
💡 Hint
Common Mistakes
Using root.data instead of value in the first blank.
Using root.left or root.right in the comparison.
5fill in blank
hard

Fill all three blanks to complete the BST insert function with correct node creation, comparison, and recursive call.

DSA Go
func insert(root *Node, value int) *Node {
    if root == nil {
        return newNode([1])
    }
    if [2] < root.data {
        root.left = insert(root.left, [3])
    } else {
        root.right = insert(root.right, value)
    }
    return root
}
Drag options to blanks, or click blank then click option'
Avalue
Broot.data
Droot.left
Attempts:
3 left
💡 Hint
Common Mistakes
Using root.data in place of value in the first or third blank.
Using root.left instead of value in the recursive call.