CS201 mid-term examination(60 minutes)
Answer all four questions.
local fun listall [] acc = acc
| listall (T (a,children) :: trees) acc = listall(children @ trees) (a :: acc) in fun dfs t = listall t [] end |
fun accsum (Lf a, acc) = a + acc
| accsum (Nd(l,v,r), acc) = accsum(l, accsum(r, v + acc)) fun sumtree (Lf a) = a | sumtree (Nd(l,v,r)) = sumtree l + v + sumtree r |
Show, by tree induction, that accsum(t, acc) = sumtree t + acc
Part 2 (90 minutes)
Answer any two of the following four questions.1
signature Q_ELMT =
sig type T val priority : T -> int end signature PQUEUE = sig type T structure Element: Q_ELMT val empty: T val insert: T * Element.T -> T exception Empty val remove: T -> (Element.T * T) end |