python BST错误,“接受1个位置参数,但给出了2个”

时间:2020-10-29 18:46:46

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

class BST:
    class TreeNode:
        def __init__(self, data=None, left=None, right=None):
            self.data = data
            self.left = left
            self.right = right
    
    # Self constructor for initialization
    def __init__(self, root=None):
        self.root = root
    def countNodes(self):
        nodeCount = 0
        curNode = self.root
        if curNode is not None:
            if curNode.left is not None:
                nodeCount += 1
                self.countNodes(curNode.left)
            if curNode.right is not None:
                nodeCount += 1
                self.countNodes(curNode.right)     
        return nodeCount

对此我感到很困惑,只是我没有看到解决我的问题的方法。 我试图仅计算二进制搜索树中的节点数量,我认为我无法在程序中实现递归函数,该如何处理此问题。 我了解理论上的递归,只是对自我的目的感到困惑

错误消息:

                45
            35
        30
            25
                16
    15
        14
10
            9
        8
            4
    2
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-5-9edfda5068f6> in <module>
    210     #finding maximum value of the tree
    211     print(tree.findMax())
--> 212     print(tree.countNodes())
    213     tree.delete(69)
    214     tree.print()

<ipython-input-5-9edfda5068f6> in countNodes(self)
    138             if curNode.left is not None:
    139                 nodeCount += 1
--> 140                 countNodes(curNode.left)
    141             if curNode.right is not None:
    142                 nodeCount += 1

NameError: name 'countNodes' is not defined

0 个答案:

没有答案