from time import clock, time ## This is to simutate a linked list structure by Python objects ### ####################### tree ########################### # 6 3 1 4 5 2 ( 6 ) # # ( 3 ) # # ( 1 ) ( 4 ) # # (2) (5) # # # # This program shows the tree structure by parentheses # ######################################################## class node: key=0 left=0 right=0 def insert(x) : p=root while p!=0 : q=p if x<=p.key: p=p.left else: p=p.right p=node() p.key=x; if x<=q.key: q.left=p # p inserted to the left of q else: q.right=p # p inserted to the right of q t=input('input t') # Input like 6,3,1,4,5,2 print t n=len(t) tt=clock() x=t[0] root=node(); root.key=x; for i in range(1,n):x=t[i];insert(x) tt=clock()-tt ########### This is to traverse tree in in-order ############### paren=[] def traverse(p): if p!=0: print '(', p1=p.left; traverse(p1) print p.key, p2=p.right; traverse(p2) print ')', traverse(root)