欧美一区二区三区,国内熟女精品熟女A片视频小说,日本av网,小鲜肉男男GAY做受XXX网站

Python中如何表示樹?

夏志豪2年前15瀏覽0評論

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)