繁体   English   中英

形状同构代码在特定值上失败

[英]Shape isomorphism code fails on specific values

我写了一段代码,检查2棵树是否同构:

n = int(input())
parent1 = [int(item) for item in input().split()]
parent2 = [int(item) for item in input().split()]


#Structure to store information about nodes
class TreeNode:
    def __init__(self, data, left=None, right=None):
        self.data = data
        self.left = left
        self.right = right

    def add_child(self, node):
        if not self.left:
            self.left = node
        elif not self.right:
            self.right = node

    def __repr__(self):
        return 'TreeNode({self.data!r}, {self.left!r}, {self.right!r})'.format(self=self)



 # Function which converts trees from parent array representation into the usual one.
def construct_tree(parents: list):

    # Put Nodes with corresponding values into the list
    constructed = [TreeNode(i) for i in range(len(parents))]

    root = None
    for i, parent in enumerate(parents):

        # If parent's index = -1, it's the root of the tree
        if parent == -1:
            root = constructed[i]
        else:
            # Tie up current node to corresponding parent
            constructed[parent].add_child(constructed[i])

    return root

def are_isomorphic(T1, T2):
    # Both roots are empty, trees are isomorphic by default

    if len(parent1) != len(parent2):
        return False

    if T1 is None and T2 is None:
        return True
    #if T1.data != T2.data Gives the wrong answer

    # If one of the trees is empty, and the other - isn't, do not bother to check further.
    if T1 is None or T2 is None:
        return False

    # There are two possible cases for n1 and n2 to be isomorphic
    # 1: The subtrees rooted at these nodes haven't been swapped
    # 2: The subtrees rooted at these nodes have been swapped
    return (are_isomorphic(T1.left, T2.left) and are_isomorphic(T1.right, T2.right) or
            are_isomorphic(T1.left, T2.right) and are_isomorphic(T1.right, T2.left))

它几乎为每个树对都给出了正确的答案,除了以下几点:

TreeNode(0,TreeNode(1,TreeNode(3,无,无),TreeNode(4,无,无)),TreeNode(2,无,无))

TreeNode(0,TreeNode(1,TreeNode(3,无,无),无),TreeNode(2,TreeNode(4,无,无),无))

它们不是同构的,但是我的代码确定它们是同构的。

我画了这些树,并认为这种情况已包含在递归过程中。 我尝试了这个:

if are_isomorphic(T1.left, T2.left) is False:
    return "No"

if are_isomorphic(T1.left, T2.right) is False:
    return "No"

if are_isomorphic(T1.right, T2.left) is False:
    return "No"

if are_isomorphic(T1.right, T2.right) is False:
    return "No"

else:
    return "Yes"

和这个:

    if (are_isomorphic(T1.left, T2.left) and are_isomorphic(T1.right, T2.right) is False):
        return "No"

    elif (are_isomorphic(T1.left, T2.right and are_isomorphic(T1.right, T2.left) ) is False):
        return "No"

    else:
        return "Yes"

有人可以解释我在想什么吗?

这并不是真正的答案,但是无法在注释中发布代码。 正如我所说,在我看来,您的代码在您给出的示例中产生了正确的结果。

#Structure to store information about nodes
class TreeNode:
    def __init__(self, data, left=None, right=None):
        self.data = data
        self.left = left
        self.right = right

    def add_child(self, node):
        if not self.left:
            self.left = node
        elif not self.right:
            self.right = node

def are_isomorphic(T1, T2):
    # Both roots are empty, trees are isomorphic by default

    #if len(parent1) != len(parent2):
        #return False

    if T1 is None and T2 is None:
        return True
    #if T1.data != T2.data Gives the wrong answer

    # If one of the trees is empty, and the other - isn't, do not bother to check further.
    if T1 is None or T2 is None:
        return False

    # There are two possible cases for n1 and n2 to be isomorphic
    # 1: The subtrees rooted at these nodes haven't been swapped
    # 2: The subtrees rooted at these nodes have been swapped
    return (are_isomorphic(T1.left, T2.left) and are_isomorphic(T1.right, T2.right) or
            are_isomorphic(T1.left, T2.right) and are_isomorphic(T1.right, T2.left))

T1=TreeNode(0, TreeNode(1, TreeNode(3, None, None), TreeNode(4, None, None)), TreeNode(2, None, None))

T2=TreeNode(0, TreeNode(1, TreeNode(3, None, None), None), TreeNode(2, TreeNode(4, None, None), None))

print(are_isomorphic(T1, T2))

上面打印False 如果有问题,那一定是您构造树的方式。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM