1
votes

So I created this node class which has an array of children nodes. Now I want to iterate through the children to find the least cost/value total of all the possible paths. I'm applying the depth first search strategy. But after one of my children has undergone an interation, I get a TypeError saying that "NodeType cannot be iterated"

class Node:
    def __init__(self, cost):
        self.cost= cost
        self.children = None

    def get(self):
        return self.cost

    def findMinPath(self):
        min_val = 10000
        if self.children is None:
            return self.cost
        for child in self.children:
            temp = child.findMinPath()
            if temp<min_val:
                min_val=temp
        return min_val+self.cost

if __name__ =='__main__':
    newnode = Node(0)
    nodeleft= Node(5)
    nodecenter=Node(3)
    noderight=Node(6)
    newnode.children={nodeleft,nodecenter,noderight}
    nodeleft.children=(Node(4))
    Nodecenterleft =Node(2)
    Nodecenterright = Node(0)
    nodecenter.children={Nodecenterleft,Nodecenterright}
    Nodecenterleftleft=Node(1)
    Nodecenterleft.children ={Nodecenterleftleft}
    Nodecenterleftleftright= Node(1)
    Nodecenterleftleft.children={Nodecenterleftleftright}
    Nodecenterrightleft = Node(10)
    Nodecenterright.children={Nodecenterrightleft}
    Noderightleft=Node(1)
    Noderightright=Node(5)
    noderight.children ={Noderightleft,Noderightright}
    print (newnode.findMinPath())

The stack trace is as follows:

Traceback (most recent call last): File "/Users/yashshah/Desktop/Initializer/tree.py", line 45, in print (newnode.findMinPath()) File "/Users/yashshah/Desktop/Initializer/tree.py", line 17, in findMinPath temp = child.findMinPath() File "/Users/yashshah/Desktop/Initializer/tree.py", line 16, in findMinPath for child in self.children: TypeError: 'Node' object is not iterable [Finished in 0.094s]

1
Can you provide a stacktrace, and since your code doesn't have line numbers, tell us on what line the error is occurring? - CryptoFool
Any reason for tagging this question with Java? - Kartik
Yeah I'm new here @Karthik - Yash Shah
@Steve Yeah Traceback (most recent call last): File "/Users/yashshah/Desktop/Initializer/tree.py", line 45, in <module> print (newnode.findMinPath()) File "/Users/yashshah/Desktop/Initializer/tree.py", line 17, in findMinPath temp = child.findMinPath() File "/Users/yashshah/Desktop/Initializer/tree.py", line 16, in findMinPath for child in self.children: TypeError: 'Node' object is not iterable [Finished in 0.094s] - Yash Shah

1 Answers

1
votes

This line is wrong:

nodeleft.children = (Node(4))

it needs to be:

nodeleft.children = {Node(4)}

Since you can put parens around any expression, Python can't be sure you mean to create a tuple with your version. So your line is the same as:

nodeleft.children = Node(4)

And as I expect you already see, your code then ends up thinking it's iterating over a collection of Node objects, but it's really trying to iterate over a single Node object, which isn't kosher.