Python 无法删除二进制搜索树中的根节点

Python 无法删除二进制搜索树中的根节点,python,python-3.x,data-structures,binary-search-tree,Python,Python 3.x,Data Structures,Binary Search Tree,我最近开始学习和实现Python中的一些数据结构,尝试二进制搜索树并完成了代码。 除了删除根节点外,所有操作都正常运行。 我将代码分为3个模块 这是我的密码: Node.py Output.py 删除命令的错误是: Traceback (most recent call last): File "G:\Docs\Liclipse Projects\DS\BinarySearchTrees\Output.py", line 16, in <module> bst.r

我最近开始学习和实现Python中的一些数据结构,尝试二进制搜索树并完成了代码。 除了删除根节点外,所有操作都正常运行。 我将代码分为3个模块

这是我的密码:

Node.py

Output.py

删除命令的错误是:

    Traceback (most recent call last):
  File "G:\Docs\Liclipse Projects\DS\BinarySearchTrees\Output.py", line 16, in <module>
    bst.removal(5)
  File "G:\Docs\Liclipse Projects\DS\BinarySearchTrees\BinarySearchTree.py", line 24, in removal
    self.rootNode.remove(dataToRemove, tempNode)
  File "G:\Docs\Liclipse Projects\DS\BinarySearchTrees\Node.py", line 34, in remove
    self.rightChild.remove(self.data, self)
  File "G:\Docs\Liclipse Projects\DS\BinarySearchTrees\Node.py", line 23, in remove
    if data < self.data:
TypeError: '<' not supported between instances of 'NoneType' and 'int'
这就像在节点的remove函数中传递的数据参数返回一个None值,尽管在BinarySearchTree移除函数中给出了一个值

如果有人能在我的代码中找到错误,请告诉我解决方法。这将是一个很大的帮助。

if data
当数据不是无时,这将缩短检查时间

对于您的特定问题,我没有一个完整的答案,但有一个一般性建议可以让事情变得更简单:与其将父节点传递到node.remove,不如让它返回一个节点。返回值可以是self、子节点之一,如果正在删除叶节点,则返回值可以是None。这使得节点不必关心它们是其父节点的哪个子节点。因为调用的是父对象,所以它已经知道在哪个子对象上调用该方法。对递归使用self.leftChild=self.leftChild.removesome_值。
from BinarySearchTrees.Node import Node


class BST:

def __init__(self):
    self.rootNode = None;


def insert(self, data):
    if self.rootNode is None:
        self.rootNode = Node(data)

    else:
        self.rootNode.insert(data)


def removal(self, dataToRemove):
    if self.rootNode:
        if self.rootNode.data == dataToRemove:
            tempNode = Node(None)
            tempNode.leftChild = self.rootNode
            print("The value of dataToRemove : " + str(dataToRemove))
            self.rootNode.remove(dataToRemove, tempNode)
        else:
            self.rootNode.remove(dataToRemove, None)


def getMin(self):
    if self.rootNode:
        return self.rootNode.getMin()

def getMax(self):
    if self.rootNode:
        return self.rootNode.getMax()

def traverseInOrder(self):
    if self.rootNode:
        self.rootNode.traverseInOrder()
from BinarySearchTrees.BinarySearchTree import BST

bst = BST()

bst.insert(5)
bst.insert(8)
bst.insert(3)
bst.insert(7)

bst.insert(9)
bst.insert(4)
bst.insert(2)



bst.traverseInOrder()
bst.removal(5)
bst.traverseInOrder()
    Traceback (most recent call last):
  File "G:\Docs\Liclipse Projects\DS\BinarySearchTrees\Output.py", line 16, in <module>
    bst.removal(5)
  File "G:\Docs\Liclipse Projects\DS\BinarySearchTrees\BinarySearchTree.py", line 24, in removal
    self.rootNode.remove(dataToRemove, tempNode)
  File "G:\Docs\Liclipse Projects\DS\BinarySearchTrees\Node.py", line 34, in remove
    self.rightChild.remove(self.data, self)
  File "G:\Docs\Liclipse Projects\DS\BinarySearchTrees\Node.py", line 23, in remove
    if data < self.data:
TypeError: '<' not supported between instances of 'NoneType' and 'int'