0

Follows,

I need some help with fixing a bug in my program. I can't post the entire code here as its an on going assignment.

I'm building a Tree in Python.

class Tree:
	def __init__(self, f, r):
		self.first = f
		self.rest = r

This is my Tree.

I perform a depth-first search on it.

So I call

traverse(tree.rest)

The traverse function looks like this:

def traverse(vertex):
	if len(vertex) == 0:
		return
...

The following is the error that keeps poping out:

if len(vertex) == 0:
AttributeError: Tree instance has no attribute '__len__'

Can someone help me with this.

drjay

2
Contributors
1
Reply
2
Views
8 Years
Discussion Span
Last Post by woooee
0

traverse(tree.rest)

tree would have to be an instance of the Tree class for this to work.

class Tree:
	def __init__(self, f, r):
		self.first = f
		self.rest = r 
		
tree = Tree(1, [])
vertex = tree.rest
print "tree", 
if len(vertex) > 0:
   print "Greater"
else:
   print "Lesser/Equal"
   
tree_2 = Tree(1, [123])
vertex = tree_2.rest
print "tree_2",
if len(vertex) > 0:
   print "Greater"
else:
   print "Lesser/Equal"
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.