I don't understand where am I making a mistake, the following is the code for creating a tree but it never creates the tree. I think there is some logical mistake in the make_tree function.
class node:
def __init__(self, a):
self.a=a
self.l=None
self.r=None
class tree:
def __init__(self, a,maxd=5):
self.s=node(a)
self.maxd=maxd
def make_tree(self, n,d=0):
if(self.maxd<d):
return n
else:
n=node(d)
n.a=d
self.make_tree( n.l,d+1)
self.make_tree(n.r,d+1)
return n
t=tree(2)
t.s=t.make_tree(t.s)
print 'children ', (t.s.r!=None), (t.s.l!=None)
output:
children False False
want a tree like this:
o
/ \
o o
/ \ / \
o o o o
t.sareNonein this case, which is why you're getting that output. What is the desired output?make_treeon the created node several times. Each time that node is created, its left and right are set toNone, according yourNode's__init__. It might help to have a diagram of what you want the tree to look like