我该如何解决这个问题? builtins.AttributeError:'NoneType'对象没有属性'_helper'

时间:2013-11-02 00:57:49

标签: python python-3.x nodes binary-search-tree

我继续犯这个错误,我不知道如何纠正它。 我正在尝试在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

1 个答案:

答案 0 :(得分:0)

您没有包含追溯,但您在此处显示的代码中对_helper的唯一引用是:

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

错误消息告诉您self.rootNone。由于您没有显示您运行的代码以触发此错误,因此没有人能够猜到您使用self.root仍为None进行调用的原因。但由于BST.__init__()self.root绑定到None,所以肯定不会令人感到意外; - )