简体   繁体   中英

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

I'm trying to find all the nodes whose data is greater than the value using recursion. I learned from someone else that I should use Breadth-First Search but I'm still new to this algorithm. I've tried to return the first node that satisfies the requirement, but really don't know how to get the remaining ones. Can anyone help me figure out how to go through the child branches so that I can return a complete list?

Some intended test cases can be:

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 following code is how to build the tree and my partial code:

#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)

The following is my code to get the list of nodes, but I can only successfully return the first one (I know I should use recursion, but really have no idea about where to implement this technique):

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

I also put the graph illustration in the following: 在此处输入图像描述

Try:

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']

You only need to concatenate the results from the child trees, as those results would be lists of all node names (that satisfy the criterion) in the child trees.

Or using a generator 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']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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