【问题标题】:How to print parent of a given node如何打印给定节点的父节点
【发布时间】:2017-11-10 18:30:29
【问题描述】:

我创建了一棵二叉树,并试图找到我手动传递的任何节点的父节点。它在左子树上运行良好,但仅对右子树上的某些特定节点没有。

# To create a tree from scratch
class tree:
    """To create nodes each time an instance has been
    created"""
    def __init__(self, key):
        self.data = key
        self.left = None
        self.right = None

    def parent_search(self, root, child_node):
        if root :
         if root.left.data== child_node:
            return root.data
         if root.right.data== child_node:
            return root.data
         elif root:
            return self.parent_search(root.left, child_node)
            return self.parent_search(root.right, child_node)

root = tree(10)
root.left = tree(20)
root.left.left = tree(90)
root.left.right = tree(100)
root.left.left.left = tree(80)
root.right = tree(30)
root.right.left = tree(40)
root.right.right = tree(50)
print(root.parent_search(root,80))

如果我给root.parent_search(root,80),我得到 90 作为 80 的父级。但是,如果我在右边搜索假设 40,它会给我NoneType 错误。

return self.parent_search(root.left, child_node)
  File "/home/vaibhav/Desktop/Data_Structures/python_play_area.py", line 14, in parent_search
    if root.right.data== child_node:
AttributeError: 'NoneType' object has no attribute 'data'

观察

我没有看到子树的右孩子有任何问题,因为我可以看到 root.left.right 在我们将其称为父母或孩子时成功传递。

【问题讨论】:

  • 您的递归需要处理 root.left 或 root.right 为 None 的情况。此外,elif root: 之后的语句将不起作用,因为有两个返回。第二个 return 语句永远不会被执行。
  • 我应该在哪里添加 root.left 或 root.right 是 None 以及为什么两个返回不起作用。请解释
  • 一旦一个方法返回,它就不再执行,所以第二个return语句永远不会被执行。我发布了一个答案,因为 cmets 很难格式化。

标签: python data-structures binary-tree recursive-datastructures


【解决方案1】:

我明白你为什么使用两个returns,你需要从函数中返回一些东西,你需要为左右数组调用它。只需为此使用or。因为,something or None 总是返回 something

def parent_search(self, root, child_node):
    if not root: return None
    if root.left and root.left.data==child_node: return root
    if root.right and root.right.data==child_node: return root
    return self.parent_search(root.left, child_node) or self.parent_search(root.right, child_node) 

【讨论】:

  • 这可以通过删除方法签名中的self参数并将最后一行中的self替换为'root'来制成静态方法。
  • 感谢您的指出。但是为了OP,我个人的理念不是给出完美的答案,而是一个变化最少的答案......
  • 我对我的代码进行了更改并将其发布为答案。 Shihab,john 感谢您的贡献
【解决方案2】:

我只是通过添加 if root.left 和条件 if root.left==child 对我的代码进行了一些小改动,并且它起作用了。这是我做的最小的改变,它奏效了。

# To create a tree from scratch
class tree:
    """To create nodes each time an instance has been
    created"""
    def __init__(self, key):
        self.data = key
        self.left = None
        self.right = None

    def parent_search(self, root, child_node):
        if  root :
            if root.left and root.left.data== child_node:
                return root.data
            if root.right and root.right.data== child_node:
                return root.data
            elif root:
                return self.parent_search(root.left, child_node) or self.parent_search(root.right, child_node)


root = tree(10)
root.left = tree(20)
root.left.left = tree(90)
root.left.right = tree(100)
root.left.left.left = tree(80)
root.right = tree(30)
root.right.left = tree(40)
root.right.right = tree(50)
print(root.parent_search(root,50))

【讨论】:

    猜你喜欢
    • 2019-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多