Complete the code to create a new node with the given value.
func newNode(value int) *Node {
return &Node{data: [1], left: nil, right: nil}
}The new node's data field should be set to the given value.
Complete the code to insert a value into the BST recursively.
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
}When the current root is nil, create a new node with the value to insert.
Fix the error in the comparison to decide where to insert the new value.
if [1] < root.data { root.left = insert(root.left, value) } else { root.right = insert(root.right, value) }
The comparison should be between the value to insert and the current node's data.
Fill both blanks to complete the BST insert function that returns the updated root.
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
}Both blanks require the value parameter: to create a new node and to compare with root.data.
Fill all three blanks to complete the BST insert function with correct node creation, comparison, and recursive call.
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
}The first blank is the value to create a new node. The second blank is the value to compare with root.data. The third blank is the value passed recursively to insert.