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 但我不會總是需要上面的那些比較來至少通過搜索樹嗎?謝謝。任何幫助表示贊賞
uj5u.com熱心網友回復:
使用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
uj5u.com熱心網友回復:
你不需要>比較,因為那時你已經知道它會成功,所以你可以使用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 x
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/367047.html
