0
我想知道他们是否是一种从右向左插入BST列表项的方式。到目前为止,我可以从左至右插入项目。反向订购BST插入
代码:
-- Type constructor
data Tree a = Empty | Node a (Tree a) (Tree a)
deriving (Eq, Show)
-- inserts into BST
insert :: (Ord a) => a -> Tree a -> Tree a
insert x Empty = Node x Empty Empty
insert x (Node y l r)
| y == x = Node y l r
| y < x = Node y l (insert x r)
| y > x = Node y (insert x l) r
-- Converts list to a BST
listToTree :: (Ord a) => [a] -> Tree a
listToTree xs = foldl (\acc x -> insert x acc) Empty xs
什么我目前正在实现:
Prelude> listToTree [4,2,6]
Node 4 (Node 2 Empty Empty) (Node 6 Empty Empty)
但我希望相反的顺序:
Node 6 (Node 2 Empty (Node 4 Empty Empty)) Empty
我真的不知道怎么样我可以修改foldl
来做到这一点。递归方法对于这类任务会更好吗?任何形式的帮助,将不胜感激。
最简单的方法可能是'listToTree xs = foldl(\ acc x - > insert x acc)Empty $ reverse xs'。 –
@WillemVanOnsem谢谢。我也意识到你可以做'listToTree [] =空 listToTree(x:xs)=插入x(listToTree xs)' – RoadRunner
@WillemVanOnsem你可以把它作为答案,我会很乐意接受它。 – RoadRunner