0

我不断收到这个错误,我不知道如何纠正它。我正在尝试在 BST 类中实现方法 count_less。我正在类 _BSTNode 中编写一个辅助方法,并使用间接递归在 count_less 中调用该辅助方法。

谢谢你。

class BST:
    """A Binary Search Tree."""

    def __init__(self: 'BST', container: list =[]) -> None:
        """
        Initialize this BST by inserting the items from container (default [])
        one by one, in the order given.
        """
        # Initialize empty tree.
        self.root = None
        # Insert every item from container.
        for item in container:
            self.insert(item)

    def __str__(self: 'BST') -> str:
        """
        Return a "sideways" representation of the values in this BST, with
        right subtrees above nodes above left subtrees and each value preceded
        by a number of TAB characters equal to its depth.
        """
        return self.root._str("") if self.root else ""

    def insert(self: 'BST', item: object) -> None:
        """
        Insert item into this BST.
        """
        if self.root:
            self.root.insert(item)
        else:
            self.root = _BSTNode(item)

    def count_less(self: 'BST', item: object) -> int:
        """
        Return the number of items in this BST that are strictly less tham
        item.
        """

        return self.root._helper(self.root, item)



class _BSTNode:
    """A node in a BST."""

    def __init__(self: '_BSTNode', item: object, 
                 left: '_BSTNode' =None, right: '_BSTNode' =None) -> None:
        """
        Initialize this node to store item and have children left and right.
        """
        self.item, self.left, self.right = item, left, right

    def _str(self: '_BSTNode', indent: str) -> str:
        """
        Return a "sideways" representation of the values in the BST rooted at
        this node, with right subtrees above nodes above left subtrees and each
        value preceded by a number of TAB characters equal to its depth, plus
        indent.
        """
        return ((self.right._str(indent + '\t') if self.right else '') +
                indent + str(self.item) + '\n' +
                (self.left._str(indent + '\t') if self.left else ''))

    def insert(self: '_BSTNode', item: object) -> None:
        """
        Insert item into the BST rooted at this node.
        """
        if item < self.item:
            if self.left:
                self.left.insert(item)
            else:
                self.left = _BSTNode(item)
        elif item > self.item:
            if self.right:
                self.right.insert(item)
            else:
                self.right = _BSTNode(item)
        # else:  # item == self.item
        #     pass  # nothing to do: item is already in the tree

    def _helper(self, item):
        count = 0
        if not self.item:
            return 0
        if self.item < item:
            count += 1
        count +=  count_less(self.left, item)
        count += count_less(self.right, item)
        return count
4

1 回答 1

0

您没有包含回溯,但_helper您显示的代码中唯一的参考是这里:

return self.root._helper(self.root, item)

错误消息告诉你self.rootNone. 由于您没有显示您为触发此错误而运行的代码,因此没有人能猜到您为什么要使用self.rootstill调用它None。但是由于BST.__init__()绑定self.rootNone,这肯定不足为奇;-)

于 2013-11-02T02:56:59.837 回答