Is there a better was to print a tree? I am doing the following. I am building a tree with the following code. What mistake am I really doing? it is giving the following error
63 ret = "\t"*level+repr(self.label)+"\n"
64 for child in self.children:
---> 65 ret += child.__str__(level+1)
66 return ret
67
TypeError: expected 0 arguments, got 1
Code attached below
from collections import Counter
from sets import Set
class treenode:
def __init__(self, values):
self.label = values
self.children = {}
def __str__(self, level=0):
ret = "\t"*level+repr(self.label)+"\n"
for child in self.children:
ret += child.__str__(level+1)
return ret
def __repr__(self):
return '<tree node representation>'
def build_tree(featurerange, featureid, counts):
if len(sorted(counts)) > 1:
featuresLeft = featurerange - Set([featureid])
if not featuresLeft:
rootnode.children[v] = treenode(counts.most_common(1)[0][0])
else:
rootnode.children[v] = build_tree(featuresLeft)
else:
rootnode.children[v] = treenode(counts.most_common(1)[0][0])
return rootnode
featurerange = set([0, 1])
featureid = 1
counts = Counter({'-': 49, '+': 45})
tree = build_tree(featurerange, featureid, counts)
str(tree)
print tree