RecursionError:超过最大递归深度-二叉树

时间:2020-02-18 06:25:38

标签: python algorithm binary-tree

在为二叉树实现add_nodesearch方法时,我收到RecursionError:超过最大递归深度

代码:

class TreeNode:
    def __init__(self, value):
        self.value = value
        self.left = None
        self.right = None


class BinaryTree:
    def __init__(self, root=None):
        self.root = root

    def add_node(self, node, value):
        node = self.root
        if node is not None:
            if not node.value:
                node.value = value
            elif not node.left:
                node.left = value
            elif not node.right:
                node.right = value
            else:
                node.left = self.add_node(node.left, value)
        else:
            self.root = TreeNode(value)

    def search(self, value):
        node = self.root
        found = False
        while node is not None:
            if node.value == value:
                found = True
            if node.left:
                found = node.left.search(value)
            if node.right:
                found = found or node.left.search(value)
        return found



def main():
    binary_tree = BinaryTree()
    binary_tree.add_node(binary_tree.root, 200)
    binary_tree.add_node(binary_tree.root, 300)
    binary_tree.add_node(binary_tree.root, 100)
    binary_tree.add_node(binary_tree.root, 30)
    binary_tree.traverse_inorder(binary_tree.root)
    print(binary_tree.search(200))


if __name__ == '__main__':
    main()

错误:

Traceback (most recent call last):
  File ".\binary_tree_test20.py", line 51, in <module>
    main()
  File ".\binary_tree_test20.py", line 45, in main
    binary_tree.add_node(binary_tree.root, 30)
  File ".\binary_tree_test20.py", line 22, in add_node
    node.left = self.add_node(node.left, value)
  File ".\binary_tree_test20.py", line 22, in add_node
    node.left = self.add_node(node.left, value)
  File ".\binary_tree_test20.py", line 22, in add_node
    node.left = self.add_node(node.left, value)
  [Previous line repeated 995 more times]
RecursionError: maximum recursion depth exceeded

2 个答案:

答案 0 :(得分:3)

这是我可以给你的补救方法。

class TreeNode:
    def __init__(self, value):
        self.value = value
        self.left = None
        self.right = None

def _add_node(node, value):
    if not node.value:
        node.value = value
    elif not node.left:
        node.left = TreeNode(value)
    elif not node.right:
        node.right = TreeNode(value)
    else:
        _add_node(node.left, value)

class BinaryTree:
    # ...

    def add_node(self, value):
        node = self.root
        if node is not None:
            _add_node(node, value)
        else:
            self.root = TreeNode(value)

    # ...



def main():
    binary_tree = BinaryTree()
    binary_tree.add_node(200)
    binary_tree.add_node(300)
    binary_tree.add_node(100)
    binary_tree.add_node(30)

尽管我建议仅扩展TreeNode的定义,而不定义BinaryTree

答案 1 :(得分:0)

由于没有使用node参数,因此您将获得无限递归,而将其替换为self.root。因此,当您递归时,您每次都会从根开始,并且永远不会结束。

此外,行node.left = self.add_node(node.left, value)期望add_node返回新节点,但是您的方法未返回任何内容。当更新现有节点时,它应该只返回修改后的节点。如果正在创建一个新节点,它将返回该节点。

    def add_node(self, node, value):
        if node is not None:
            if not node.value:
                node.value = value
            elif not node.left:
                node.left = value
            elif not node.right:
                node.right = value
            else:
                node.left = self.add_node(node.left, value)
            return node
        else:
            return TreeNode(value)

您将这样调用此方法:

binary_tree.root = binary_tree.add_node(binary_tree.root, 30)