datatype 'a Tree = Empty | Node of ('a * 'a Tree * 'a Tree); fun findDepth((Node(a,Empty,Empty))) =1 | findDepth((Node(a,sol,Empty))) = 1+findDepth(sol) | findDepth((Node(a,Empty,sag))) = 1+findDepth(sag) | findDepth((Node(a,sol,sag)))=1+let val k=findDepth(sol); val l=findDepth(sag) in if k>l then k else l end; fun getChars((Node(a,sol,sag)),all,curr) = if all=curr+1 then [a] else if sol=Empty andalso sag=Empty then nil else if sag=Empty then getChars(sol,all,curr+1) else if sol=Empty then getChars(sag,all,curr+1) else getChars(sol,all,curr+1)@getChars(sag,all,curr+1); fun writeLevels(mytree,alldepth,currentdepth) = if currentdepth>alldepth then nil else [getChars(mytree,currentdepth,0)]@writeLevels(mytree,alldepth,currentdepth+1); fun getLevels mytree = writeLevels(mytree,findDepth(mytree),1);