如何修复 Python 二进制搜索树中的 NoneType 错误?

How to fix NoneType error in Python Binary Search Tree?

我正在创建一个基本的二叉搜索树程序,其中的节点必须是字符串,但是,我不断收到此错误: 'builtins.AttributeError: 'NoneType' object has no attribute 'addNode'

我有点困惑,因为我认为您必须将子节点声明为 None。我的代码如下(请原谅混乱和额外的打印语句):

class BinarySearchTree:
  #constructor with insertion value & left/right nodes as None
  def __init__(self, data):
    self.data = data
    self.left = None
    self.right = None
  
  #function to insert node
  def addNode(root, data):
    #when tree doesn't exist
    if root == None:
      return BinarySearchTree(data)
    else:
      #when node is already in tree
      if root.data == data:
        return root
      #smaller values go left
      elif data < root.data:
          root.left.addNode(data)
      #bigger values go right
      else:
          root.right.addNode(data)
    return root

  #function to find smallest node value (to help with deletion)  
  def smallestNode(root):
      node = root
      #loop goes to lowest left leaf
      while(node.left is not None):
          node = node.left
      return node

  #function to delete node
  def removeNode(root, data):
    if root == None:
      return root
    #when node to be deleted in smaller than root, go left
    if data < root.data:
      root.left = root.left.removeNode(data)
    #when node to be deleted in bigger than root, go right
    elif data > root.data:
        root.right = root.right.removeNode(data)
    ##when node to be deleted in the same as root...
    else:
        #when node has only 1 or 0 children
        if root.right == None:
            move = root.left
            root = None
            return move
        elif root.left == None:
            move = root.right
            root = None
            return move
        #when node has 2 children, copy then delete smallest node
        move = root.right.smallestNode()
        root.data = move.data
        root.right = root.right.removeNode(move.data)
    return root

  def findNode(root, data):
    #if current node is equal to data value then return the root
    if root.data == data or root == None:
      return root
    #if current node is greater than the data value then, search to the left
    elif data < root.data:
      return root.left.findNode(data)
    #if current node is less than the data value then, search to the right
    else:
      return root.right.findNode(data)

def createBST(keys):
  root = BinarySearchTree(keys[0])

  for i in range(1,len(keys)):
      root.addNode(keys[i])
  return root

print('Hi! Welcome to the Binary Search Tree Builder')
print('Here are your options below:')
print('1) Build new tree')
print('2) Add a node')
print('3) Find a node')
print('4) Delete a node')
choice = int(input('What would you like to do?: '))
if choice == 1:
  nodes = list(input("Enter the strings you would like to build your tree from (separate by a space): ").split())
  print(nodes)
  tree = createBST(nodes)
  print(tree)

我想知道这个错误到底是从哪里来的,我该如何解决?另外,如果您发现我的代码中出现任何其他问题,请告诉我!

您混淆了“树节点”和“树”的概念。你所拥有的是两者的混合物。请记住,所有成员函数的第一个参数都应该是“self”。将“root”替换为“self”可能会使您的问题更清楚。

因此,您可以创建一个 BinaryTree class,一旦您拥有一个节点,它就有一个名为 self.root 的成员,该成员拥有一个 BinaryTreeNode 对象。节点将保存 leftright 值,每个值都将具有 NoneBinaryTreeNode 对象。节点 class 可能不需要太多代码——只需要 self.leftself.right.

BinaryTree class 将有一个 addNode 成员知道如何遍历树以找到正确的位置,但是当您找到该位置时,您只需设置 self.left = BinaryTreeNode(...)。节点不知道树,因此节点不知道如何添加新节点。这是树本身的功能。

这是否让您的前进道路更加清晰?