基本上我想将BST树变成一个映射,其中节点是键,节点的出现次数是值.所以,如果我输入这个:
toMap(叶子13)
我会的
> [(13,1)]
Run Code Online (Sandbox Code Playgroud)
这是我到目前为止:
data Tree a = Empty | Node a (Tree a) (Tree a) deriving (Show)
leaf x = Node x Empty Empty
toMap' :: Int -> Tree a -> ([(a, Int)], Int)
toMap' a Empty = ([], a)
toMap' a (Node x xl xr) = ((x, a): xl' ++ xr', k)
where (xl', i) = toMap' (a+1) xl
(xr', k) = toMap' (i) xr
toMap :: Tree a -> [(a, Int)]
toMap = fst. toMap' …Run Code Online (Sandbox Code Playgroud)