SML - How to create a list out of a postorder scan of a tree -


how implement function in sml gets tree , returns list. list consists of values in tree nodes according postorder scan of tree.

the tree datatype is:

datatype 'a tree = leaf | branch of 'a * 'a tree * 'a tree; 

that can done by:

 fun createlist(leaf) = [] =   | createlist(branch(el, left, right)) = createlist(left) @ createlist(right) @ [el]; 

if have branch first visit it's left subtree (createlist(left)), it's right subtree (createlist(right)) , afterwards append element el, postorder tree traversal does. if want create list leaf (an empty tree) result empty list.


Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -