繁体   English   中英

如何返回其值大于特定数字的节点列表

[英]how to return a list of nodes whose value are greater than a specific number

我正在尝试使用递归查找其数据大于该值的所有节点。 我从其他人那里了解到我应该使用广度优先搜索,但我对这个算法还是新手。 我试图返回满足要求的第一个节点,但真的不知道如何获得剩余的节点。 谁能帮我弄清楚如何通过子分支 go 以便我可以返回完整列表?

一些预期的测试用例可以是:

find_all(n2,200000) #Should return [N5, N7]
find_all(n1,200000) #Should return []
find_all(root,30000) #Should return [root, N1, N3, N4, N8, N2, N5, N6, N7]

以下代码是如何构建树和我的部分代码:

#The tree definition
class BinaryTreeNode:
    def __init__(self, name,data):
        self.name = name #The name of the node
        self.data = data #Data associated with the node
        self.leftChild = None #Left child
        self.rightChild = None #Right child
    
    #adds a left and right child to the node. Note that the left child is added first
    #I.e., there may be a left child without a right child but there cannot be a right child without a left child
    def add_children(self,left,right=None): 
        self.leftChild = left
        self.rightChild = right
        
    #str function for printing the contents of a node
    def __str__(self):
        rval = self.name
        if self.leftChild:
            rval += " Left: " + self.leftChild.name
        if self.rightChild:
            rval += " Right: " + self.rightChild.name
        return rval
    
    #repr function for internal representation
    def __repr__(self):
        return self.name
#Code for building the tree
root = BinaryTreeNode("root",33000)
n1 = BinaryTreeNode("N1",55000)
n2 = BinaryTreeNode("N2",120000)
n3 = BinaryTreeNode("N3",72000)
n4 = BinaryTreeNode("N4",88000)
n5 = BinaryTreeNode("N5",224000)
n6 = BinaryTreeNode("N6",56000)
n7 = BinaryTreeNode("N7",920000)
n8 = BinaryTreeNode("N8",183000)


root.add_children(n1,n2)
n1.add_children(n3,n4)
n4.add_children(n8)
n2.add_children(n5)
n5.add_children(n6,n7)

以下是我获取节点列表的代码,但我只能成功返回第一个(我知道我应该使用递归,但真的不知道在哪里实现这种技术):

def find(node,value):
# I think I should initialize a list at the first, this step should be correct
    result = []
    if node:  
        if node.data > value:
            result.append(node.name)
        else:
#I'm trying to find all nodes that go through the left children
            if find(node.leftChild, value):
                result.append(node.name)
# here the same thing, I'm trying to get all nodes in the right leaf
            if find(node.rightChild, value):
                result.append(node.name)
    return result

我还将图表插图放在下面: 在此处输入图像描述

尝试:

def find_all(node, value):
    if not node: # if node is None
        return []
    result = []
    if node.data > value:
        result.append(node.name)
    result += find_all(node.leftChild, value) # recursion; result will be a list
    result += find_all(node.rightChild, value)
    return result

print(find_all(n2,200000)) # ['N5', 'N7']
print(find_all(n1,200000)) # []
print(find_all(root,30000)) # ['root', 'N1', 'N3', 'N4', 'N8', 'N2', 'N5', 'N6', 'N7']

您只需要连接来自子树的结果,因为这些结果将是子树中所有节点名称(满足标准)的列表。

或使用发电机 function:

def find_all(node, value):
    if not node:
        return
    if node.data > value:
        yield node.name
    yield from find_all(node.leftChild, value)
    yield from find_all(node.rightChild, value)

print(list(find_all(n2,200000))) # ['N5', 'N7']
print(list(find_all(n1,200000))) # []
print(list(find_all(root,30000))) # ['root', 'N1', 'N3', 'N4', 'N8', 'N2', 'N5', 'N6', 'N7']

暂无
暂无

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

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