Complete the code to insert a value into a Binary Search Tree (BST).
func insert(root *Node, val int) *Node {
if root == nil {
return &Node{value: val}
}
if val [1] root.value {
root.left = insert(root.left, val)
} else {
root.right = insert(root.right, val)
}
return root
}In a BST, values less than the current node go to the left subtree.
Complete the code to search for a value in a BST.
func search(root *Node, val int) bool {
if root == nil {
return false
}
if val == root.value {
return true
} else if val [1] root.value {
return search(root.left, val)
} else {
return search(root.right, val)
}
}When searching, if the value is less than current node, go left.
Fix the error in the code to find the minimum value node in a BST.
func findMin(root *Node) *Node {
current := root
for current.[1] != nil {
current = current.left
}
return current
}The minimum value in a BST is found by going as left as possible.
Fill both blanks to create a function that deletes a node from a BST.
func deleteNode(root *Node, val int) *Node {
if root == nil {
return root
}
if val [1] root.value {
root.left = deleteNode(root.left, val)
} else if val [2] root.value {
root.right = deleteNode(root.right, val)
} else {
// Node with only one child or no child
if root.left == nil {
return root.right
} else if root.right == nil {
return root.left
}
// Node with two children
temp := findMin(root.right)
root.value = temp.value
root.right = deleteNode(root.right, temp.value)
}
return root
}To delete, traverse left if value is less, right if greater.
Fill all three blanks to create a function that returns a map of node values to their depths in a BST.
func mapDepths(root *Node, depth int, result map[int]int) map[int]int {
if root == nil {
return result
}
result[[1]] = depth
mapDepths(root.[2], depth+1, result)
mapDepths(root.[3], depth+1, result)
return result
}Store node value as key, traverse left and right children recursively.
