2010-10-13 84 views
3

我有任务要做,我无法弄清楚如何做一个问题的所有元素。 这是我必须做的:查找BST满足F使用的成功延续在SML

写会聚集在树T满足属性P,并返回其所有元素的功能。按顺序遍历树。 使用成功延续找到满足f的BST中的所有元素。

我做了以下内容:

datatype 'a tree = 
Empty | Node of (int * 'a) * 'a tree * 'a tree 

fun find_all f Empty cont = cont() 
| find_all f (Node(x, l, r)) cont = if (f x) then find_all (f l (fn x => x::cont())) @ find_all (f r (fn x => x::cont())) 
     else find_all (f l (fn() => cont())) @ find_all (f r (fn() => cont())); 

我不明白为什么它不工作...

回答

2

这里是我做过什么:

fun find_all f Empty cont = cont [] 
| find_all f (Node(x, l, r)) cont = if (f x) then find_all f l (fn e => find_all f r (fn t => cont ([email protected](x::t)))) 
         else find_all f l (fn t => find_all f r (fn e => cont ([email protected]))); 

而且它的工作原理罚款