classTree:
def__init__(self,entry,left=None,right=None):
self.entry=entry
self.left=left
self.right=right
def__repr__(self):
args=repr(self.entry)
ifself.leftorself.right:
args+=',{0},{1}'.format(repr(self.left),repr(self.right))
return'Tree({0})'.format(args)
defsquare_tree(t):
ift==None:
return
else:
t.entry=t.entry**2
square_tree(t.left)
square_tree(t.right)
defheight(t):
ift==None:
return0
else:
return1+max(height(t.left),height(t.right))
defsize(t):
ift==None:
return0
else:
returnsize(t.left)+size(t.right)+1
deffind_path(t,x):
ift==None:
returnNone
elift.entry==x:
return(x,)
left=find_path(t.left,x);right=find_path(t.right,x)
ifleft:
return(t.entry,)+left
elifright:
return(t.entry,)+right
else:
returnNone
t=Tree(2,Tree(7,Tree(2),Tree(6,Tree(5),Tree(11))),Tree(15))
print(t)
a=find_path(t,5)
print(a)