1 #!/usr/bin/python 2 3 class TreeNode(object): 4 def __init__(self,data = 0,left = None,right = None): 5 self.data = data 6 self.left = left 7 self.right = right 8 9 class Bitree(object): 10 def __init__(self,root=None): 11 self.root = root 12 13 def is_empty(self): 14 if self.root is None: 15 return True 16 else: 17 return False 18 19 def preOrder(self,treenode): 20 if treenode is None: 21 return 22 23 print(treenode.data) 24 self.preOrder(treenode.left) 25 self.preOrder(treenode.right) 26 27 def inOrder(self,treenode): 28 29 if treenode is None: 30 return 31 32 self.inOrder(treenode.left) 33 print(treenode.data) 34 self.inOrder(treenode.right) 35 36 def postOrder(self,treenode): 37 38 if treenode is None: 39 return 40 41 self.postOrder(treenode.left) 42 self.postOrder(treenode.right) 43 print(treenode.data) 44 45 46 47 n1 = TreeNode(1) 48 n2 = TreeNode(2,n1) 49 n3 = TreeNode(3) 50 n4 = TreeNode(4) 51 n5 = TreeNode(5,n3,n4) 52 n6 = TreeNode(6,n2,n5) 53 n7 = TreeNode(7,n6) 54 n8 = TreeNode(8) 55 root = TreeNode('root',n7,n8) 56 57 bt = Bitree(root) 58 print ('preOrder......') 59 print (bt.preOrder(bt.root)) 60 print ('inOrder......') 61 print (bt.inOrder(bt.root)) 62 print ('postOrder.....') 63 print (bt.postOrder(bt.root))