【发布时间】:2017-01-20 17:48:38
【问题描述】:
我正在使用 Haskell 中的二叉搜索树。
这是我写的代码
data BinaryTree a = Leaf | Node (BinaryTree a) a (BinaryTree a)
deriving (Show, Eq)
insert :: (Ord a, Eq a) => a -> BinaryTree a -> BinaryTree a
insert e (Leaf)= (Node Leaf e Leaf)
insert e (Node n1 a n2)
| e<a=(Node (insert e n1) a n2)
| otherwise = (Node n1 a (insert e n2))
所以基本上这段代码将元素插入到 BST 中,如果第二个参数被锁定在括号内(例如 insert 5 (Node Leaf 2 Leaf)),它可以正常工作,但是为了获得我想要的,我需要我的程序工作在这两种情况下,当第二个参数在括号内时,当它不是时(例如insert 5 Node Leaf 2 Leaf)
您能否就如何重写此代码以获得上述内容提出建议
【问题讨论】:
-
你的代码中有一些额外的括号,这是清理后的样子:pastebin.com/vpNKvDW7
-
我认为您实际上需要在没有括号的情况下调用
insert 5 Node Leaf 2 Leaf。这在我看来是一个 XY 问题——就好像你需要一些完全不同的东西,但只提到了括号。你到底想达到什么目的?
标签: haskell binary-tree binary-search-tree