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
Post a Comment