classNode(object):def __init__(self, data = -1, lchild = None, rchild =None):
self.data=data
self.lchild=lchild
self.rchild=rchildclassBinaryTree(object):def __init__(self):
self.root=Node()defadd(self, data):
node=Node(data)ifself.isEmpty():
self.root=nodeelse:
tree_node=self.root
queue=[]
queue.append(self.root)whilequeue:
tree_node=queue.pop(0)if tree_node.lchild ==None:
tree_node.lchild=nodereturn
elif tree_node.rchild ==None:
tree_node.rchild=nodereturn
else:
queue.append(tree_node.lchild)
queue.append(tree_node.rchild)defpre_order(self, start):
node=startif node ==None:return
printnode.data,if node.lchild == None and node.rchild ==None:returnself.pre_order(node.lchild)
self.pre_order(node.rchild)defpre_order_loop(self):ifself.isEmpty():returnstack=[]
node=self.rootwhile node orstack:whilenode:printnode.data,
stack.append(node)
node=node.lchildifstack:
node=stack.pop()
node=node.rchilddefin_order(self, start):
node=startif node ==None:returnself.in_order(node.lchild)printnode.data,
self.in_order(node.rchild)defin_order_loop(self):ifself.isEmpty():
returen
stack=[]
node=self.rootwhile node orstack:whilenode:
stack.append(node)
node=node.lchildifstack:
node=stack.pop()printnode.data,
node=node.rchilddefpost_order(self, start):
node=startif node ==None:returnself.post_order(node.lchild)
self.post_order(node.rchild)printnode.data,defpost_order_loop(self):ifself.isEmpty():returnnode=self.root
stack=[]
queue=[]
queue.append(node)whilequeue:
node=queue.pop()ifnode.lchild:
queue.append(node.lchild)ifnode.rchild:
queue.append(node.rchild)
stack.append(node)whilestack:printstack.pop().data,#if lchild and rchild are None or lchild and rchild are printed, print the parent node node and pop out of the stack
#else lchild and rchild push into the stack
defpost_order_loop1(self):ifself.isEmpty():returnstack=[]
top= -1node=self.root
stack.append(node)#we need to recognize the last printed node
top += 1pre=Nonewhilestack:
node= stack[-1]if node.lchild is None and node.rchild isNone:printnode.data,
pre=node
top-= 1
elif not pre and (node.lchild == pre or node.rchild ==pre):printnode.data,
pre=node
top-= 1
else:ifnode.rchild:if top < len(stack)-1:
stack[top]=node.rchildelse:
stack.append(node.rchild)ifnode.lchild:if top < len(stack)-1:
stack[top]=node.lchildelse:
stack.append(node.lchild)deflevel_order(self):
node=self.rootif node ==None:returnqueue=[]
queue.append(node)whilequeue:
node=queue.pop(0)printnode.data,ifnode.rchild:
queue.append(node.rchild)ifnode.lchild:
queue.append(node.lchild)print
defisEmpty(self):return True if self.root.data == -1 elseFalseif __name__ == '__main__':
arr=[]for i in range(10):
arr.append(i)printarr
tree=BinaryTree()for i inarr:
tree.add(i)print 'level_order:'tree.level_order()print 'pre order:'tree.pre_order(tree.root)print '\npre order loop:'tree.pre_order_loop()print '\nin_order:'tree.in_order(tree.root)print '\nin_order loop:'tree.in_order_loop()print '\npost_order:'tree.post_order(tree.root)print '\npost_order_loop:'tree.post_order_loop()print '\npost_order_loop1:'tree.post_order_loop1()