当前位置: 首页 > 知识库问答 >
问题:

遍历二叉树递归中的列表问题

解明辉
2023-03-14

我尝试使用递归遍历二叉树。每个树要么有两个子树,要么没有子树(即,为子树保留的字段==无)

我想将每个分支(即两个子节点==无的每个节点)的最后叶子添加到一个列表中,并返回该列表。我使用“搜索”功能和助手“搜索库”功能来实现这一点。

通过调试器,我看到“search”函数中的列表确实包含我希望它显示的元素。但是,当它在search_base函数中返回时,结果似乎是一个空列表。

我非常困惑,如果有任何帮助,我将不胜感激。非常感谢。

class Node:
    def __init__(self, data, pos = None, neg = None):
        self.data = data
        self.positive_child = pos
        self.negative_child = neg   

class Diagnoser:
    def __init__(self, root):
        self.root = root

    def search_base(self):
        leaf_list=[]
        current = self.root
        return self.search(current, leaf_list)

    def search(self, current, leaf_list):
        if(current.positive_child == None):
            leaf_list.append(current)
            return leaf_list
        else:
            self.search(current.positive_child, leaf_list)
            self.search(current.negative_child, leaf_list)


if __name__ == "__main__":

    # Manually build a simple tree.
    #                cough
    #          Yes /       \ No
    #        fever           healthy
    #   Yes /     \ No
    # influenza   cold

    flu_leaf = Node("influenza", None, None)
    cold_leaf = Node("cold", None, None)
    inner_vertex = Node("fever", flu_leaf, cold_leaf)
    healthy_leaf = Node("healthy", None, None)
    root = Node("cough", inner_vertex, healthy_leaf)

    diagnoser = Diagnoser(root)
    leaf_list = diagnoser.search_base()
    print(leaf_list[0].data)

共有3个答案

满伟彦
2023-03-14

这里有一个完全简单的解决方案,没有副作用:

class Node:
    def __init__(self, data, pos=None, neg=None):
        self.data = data
        self.positive_child = pos
        self.negative_child = neg

    def leaves(self):
        if self.positive_child is self.negative_child is None:
            return [self]
        else:
            return (self.positive_child.leaves() +
                    self.negative_child.leaves())


if __name__ == "__main__":
    # Manually build a simple tree.
    #                cough
    #          Yes /       \ No
    #        fever           healthy
    #   Yes /     \ No
    # influenza   cold

    flu_leaf = Node("influenza", None, None)
    cold_leaf = Node("cold", None, None)
    inner_vertex = Node("fever", flu_leaf, cold_leaf)
    healthy_leaf = Node("healthy", None, None)
    root = Node("cough", inner_vertex, healthy_leaf)
    for leaf in root.leaves():
        print(leaf.data)
薛元忠
2023-03-14

由于search修改列表,因此它不需要返回任何内容,search\u base只需返回修改后的列表即可。

class Diagnoser:
    def __init__(self, root):
        self.root = root

    def search_base(self):
        leaf_list = []
        current = self.root
        self.search(current, leaf_list)
        return leaf_list

    def search(self, current, leaf_list):
        if current.positive_child is None:
            leaf_list.append(current)
        else:
            self.search(current.positive_child, leaf_list)
            self.search(current.negative_child, leaf_list)

此外,您需要检查两个孩子是否都失踪,即。

if current.positive_child is None and current.negative_child is None:
陶原
2023-03-14

问题是在

self.search(current.positive_child, leaf_list)
self.search(current.negative_child, leaf_list)

这些语句的返回值不会被保存或返回,因此这里函数给出None。此外,传递到这两个语句中的leaf_list是相同的,即它们不会被连接起来。在递归函数中,为了保证安全,最好不要有副作用。应该是:

 def search(self, current, leaf_list=[]):
        if(current.positive_child == None):
            return [current]
        else:
            return (self.search(current.positive_child, leaf_list)
                    + self.search(current.negative_child, leaf_list))
 类似资料:
  • 主要内容:递归实现,非递归实现二叉树中序遍历的实现思想是: 访问当前节点的左子树; 访问根节点; 访问当前节点的右子树; 图 1 二叉树   以图  1 为例,采用中序遍历的思想遍历该二叉树的过程为: 访问该二叉树的根节点,找到 1; 遍历节点 1 的左子树,找到节点 2; 遍历节点 2 的左子树,找到节点 4; 由于节点 4 无左孩子,因此找到节点 4,并遍历节点 4 的右子树; 由于节点 4 无右子树,因此节点 2 的左子

  • 我正在研究二叉树。我在网上看到了一个遍历整个二叉树的代码。这是我得到的代码:“” “‘ 我不明白的是这个函数如何打印正确的孩子?根据代码每次调用函数时,左子被打印出来。代码永远不会到达正确的孩子。

  • 用递归方式遍历二叉树 思路说明 遍历二叉树的方法有广度优先和深度优先两类,下面阐述的是深度优先。 以下图的二叉树为例: 先定义三个符号标记: 访问结点本身(N) 遍历该结点的左子树(L) 遍历该结点的右子树(R) 有四种方式: 前序遍历(PreorderTraversal,NLR):先访问根结点,然后遍历其左右子树 中序遍历(InorderTraversal,LNR):先访问左子树,然后访问根节点

  • 主要内容:递归实现,非递归实现二叉树后序遍历的实现思想是:从根节点出发,依次遍历各节点的左右子树,直到当前节点左右子树遍历完成后,才访问该节点元素。 图 1 二叉树   如图 1 中,对此二叉树进行后序遍历的操作过程为: 从根节点 1 开始,遍历该节点的左子树(以节点 2 为根节点); 遍历节点 2 的左子树(以节点 4 为根节点); 由于节点 4 既没有左子树,也没有右子树,此时访问该节点中的元素 4,并回退到节点 2 ,遍

  • 主要内容:递归实现,非递归实现二叉树先序遍历的实现思想是: 访问根节点; 访问当前节点的左子树; 若当前节点无左子树,则访问当前节点的右子树; 图 1 二叉树   以图  1 为例,采用先序遍历的思想遍历该二叉树的过程为: 访问该二叉树的根节点,找到 1; 访问节点 1 的左子树,找到节点 2; 访问节点 2 的左子树,找到节点 4; 由于访问节点 4 左子树失败,且也没有右子树,因此以节点 4 为根节点的子树遍历完成。但节点

  • 我试图理解二叉树遍历(PreOrder)的实现。非递归方法很好,但我在试图理解递归方法时完全迷失了方向。 代码: 二叉树 我的理解是,当到达节点2(8-4-2)时,节点2的左边没有。所以条件将失败。 下面是我的问题。 点头之后。左无,右无。右边是横穿的?(因为如果启动:条件失败) 在节点1之后,逻辑如何移动到节点5哪个根节点。对吧? 我对递归的理解很差,请帮助!