data Tree a = Empty | Node (Tree a) a (Tree a)
naive_find :: (Ord a) => (Tree a) -> a -> Bool
naive_find Empty _ = False
naive_find (Node t1 v t2) x
| x == v = True
| x < v = naive_find t1 x
| x > v = naive_find t2 x这是我当前bst代码的一个片段,当然还有其他功能,但我认为没有必要问这个问题。我需要将上面的2d复杂度降低到d+1,但我是不是总是需要上面的比较来通过搜索树呢?谢谢。感谢你的任何帮助
发布于 2021-11-23 14:13:24
使用compare。它是Data.Ord的一部分
naive_find (Node t1 v t2) x = case compare v x of
LT -> naive_find t1 x
EQ -> True
GT -> naive_find t2 x发布于 2021-11-23 17:20:52
您不需要>比较,因为那时您已经知道它会成功,所以您可以只使用otherwise
data Tree a = Empty | Node (Tree a) a (Tree a)
naive_find :: (Ord a) => Tree a -> a -> Bool
naive_find Empty _ = False
naive_find (Node t1 v t2) x
| x == v = True
| x < v = naive_find t1 x
| otherwise = naive_find t2 xhttps://stackoverflow.com/questions/70080736
复制相似问题