简体   繁体   中英

Find Closest Value In BST

Why I got an error missing 1 required positional argument root = findClosestValueInBst(8,)
TypeError: findClosestValueInBst() missing 1 required positional argument: 'target'

def findClosestValueInBst (tree, target):
  return findClosestValueInBstHelper(tree, target, float('inf'))

def findClosestValueInBstHelper(tree, target, closest):
  if tree is None:
    return closest
  if abs(target-closest) > abs(target - tree.value):
    closest = tree.value
  if target < tree.value:
    return findClosestValueInBstHelper(tree.left, target, closest)
  elif target > tree.value:
    return findClosestValueInBstHelper(tree.right, target, closest)
  else:
    return closest



root = findClosestValueInBst(8,)  
root.left = findClosestValueInBst(5)  
root.right = findClosestValueInBst(14) 
root.left.left = findClosestValueInBst(4)  
root.left.right = findClosestValueInBst(6) 
root.left.right.left = findClosestValueInBst(8)  
root.left.right.right = findClosestValueInBst(7)  
root.right.right = findClosestValueInBst(24) 
root.right.right.left = findClosestValueInBst(22)  
    
result = findClosestValueInBstHelper(root, 3)
print(result)

You define a function that takes two arguments:

def findClosestValueInBst (tree, target)

Later you attempt to call it with only a single argument:

root = findClosestValueInBst(8,)

The error you are seeing:

TypeError: findClosestValueInBst() missing 1 required positional argument: 'target'

Is telling you that it is trying to call the function, but it can't because it doesn't know what to set the parameter named 'target' to, since you only passed in one argument.

I have gone through your code and it seems that you first need to create the BST by adding nodes. This can be done in the following steps -

  1. First create a TreeNode class which has three parameters - value , left node and right node.
  2. Then in your helper function, you need to check in which subtree, closest value is present ie, in the left subtree or right subtree.

This code should give you an idea -

class TreeNode:
  def __init__(self, value):
    self.value = value
    self.left = None
    self.right = None


def findClosestValueInBST(tree, target):
  return findClosestValueInBstHelper(tree, target, tree.value)


def findClosestValueInBstHelper(tree, target, closest):
  # Base condition
  if tree is None:
    return closest
  if abs(target - closest) > abs(target - tree.value):
    closest = tree.value
  # If the target is present in the left subtree
  if target < tree.value:
    return findClosestValueInBstHelper(tree.left, target, closest)
  # If the target is present in the right subtree
  elif target > tree.value:
    return findClosestValueInBstHelper(tree.right, target, closest)
  else:
    return closest


# Here we are creating the BST
root = TreeNode(8)
root.left = TreeNode(5)  
root.right = TreeNode(14) 
root.left.left = TreeNode(4)  
root.left.right = TreeNode(6) 
root.left.right.left = TreeNode(8)  
root.left.right.right = TreeNode(7)  
root.right.right = TreeNode(24) 
root.right.right.left = TreeNode(22)

closestValue = findClosestValueInBST(root, 3)
print("The closest value is: " + str(closestValue))

closestValue = findClosestValueInBST(root, 19)
print("The closest value is: " + str(closestValue))

I hope this helps. Happy coding:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM