#encoding=utf-8
class node(object):
def __init__(self,data=None,left=None,right=None):
self.data=data
self.left=left
self.right=right
#前序遍历
def pre_order(tree):
if tree==None:
return
print tree.data
pre_order(tree.left)
pre_order(tree.right)
#深度
def depth(tree):
if tree==None:
return 0
left,right=depth(tree.left),depth(tree.right)
return max(left,right)+1
#层次遍历
def level_order(tree):
if tree==None:
return
q=[]
q.append(tree)
while q:
current=q.pop(0)
print current.data
if current.left!=None:
q.append(current.left)
if current.right!=None:
q.append(current.right)
tree=node('D',node('B',node('A'),node('C')),node('E',right=node('G',node('F'))))
#tree=node(1,node(2),node(3,node(5)))
#tree=node(1)
print'pre_order:'
pre_order(tree)
print 'depth:',depth(tree)
level_order(tree)