代码块
代码块语法遵循标准markdown代码,例如:
class tree:
def __init__(self,item=None,left=None,right=None):
#self.root=node(item)
self.__item=item
self.left=left
self.right=right
def insert_left(self,item):
if self.left==None:
self.left=tree(item)
else:
t=tree(item)
t.left=self.left
self.left=t
def insert_right(self,item):
if self.right == None:
self.right = tree(item)
else:
t = tree(item)
t.right = self.right
self.right = t
def get_item(self):
return self.__item
def breadth_searth(self):
__queeue=[]
result=[]
curNode=self
__queeue.append(curNode)
while __queeue!=[]:
#__queeue.append(curNode)
if curNode.left:
__queeue.append(curNode.left)
if curNode.right:
__queeue.append(curNode.right)
if curNode==self:
curNode=__queeue.pop(0)
result.append(curNode.__item)
curNode=__queeue.pop(0)
result.append(curNode.__item)
return result
def depth_searth(self,tree,order,result):
if order=='preOrder':
if tree==None:
return
result.append(tree.__item)
self.depth_searth(tree.left,order='preOrder',result=result)
self.depth_searth(tree.right,order='preOrder',result=result)
return result
if order == 'inOrder':
# result=[]
if tree == None:
return result
self.depth_searth(tree.left, order='inOrder',result=result)
result.append(tree.__item)
self.depth_searth(tree.right,order='inOrder',result=result)
return result
if order == 'postOrder':
if tree == None:
return result
self.depth_searth(tree.left, order='postOrder',result=result)
self.depth_searth(tree.right,order='postOrder',result=result)
result.append(tree.__item)
return result
def __len__(self):
if __name__=="__main__":
a = tree(1)
a.insert_left(2)
a.insert_right(3)
a.left.insert_left(4)
a.left.insert_right(5)
print(a.breadth_searth())
print(a.depth_searth(a,order='preOrder',result=[]))
print(a.depth_searth(a,order='inOrder',result=[]))
print(a.depth_searth(a,order='postOrder',result=[]))