3

我有一项任务要做,但我不知道如何做一个问题。这是我必须做的:

编写一个函数,收集树 T 中满足属性 p 的所有元素并返回它。依次遍历树。使用成功延续找到 BST 中满足 f 的所有元素。

我做了以下事情:

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()));

我不明白为什么它不起作用...

4

1 回答 1

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 (e@(x::t))))
                      else find_all f l (fn t => find_all f r (fn e => cont (e@t)));

它工作正常

于 2010-10-15T15:51:26.047 回答