liuxinyu95 / AlgoXY

Book of Elementary Functional Algorithms and Data structures
6.09k stars 737 forks source link

Haskell 里面命名不统一 #48

Closed yjhmelody closed 6 years ago

yjhmelody commented 6 years ago
-- Insert an element into a tree
insert::(Ord a) => Tree a -> a -> Tree a
insert Empty k = Node Empty k Empty
insert (Node l x r) k | k < x = Node (insert l k) x r
                      | otherwise = Node l x (insert r k)

delete::(Ord a)=> Tree a -> a -> Tree a
delete Empty _ = Empty
delete (Node l k r) x | x < k = (Node (delete l x) k r)
                      | x > k = (Node l k (delete r x))
                      -- x == k
                      | isEmpty l = r
                      | isEmpty r = l
                      | otherwise = (Node l k' (delete r k')) where k' = min r

-- in-order tree traverse
mapT::(a->b) -> Tree a -> Tree b
mapT _ Empty = Empty
mapT f (Node l x r)= Node (mapT f l) (f x) (mapT f r)

-- Traverse a part of tree inside a range [a, b]
mapR :: (Ord a)=>(a->b) -> a -> a -> Tree a -> Tree b
mapR f a b t = map' t where
    map' Empty = Empty
    map' (Node l k r) | k < a = map' r
                      | a <= k && k <= b = Node (map' l) (f k) (map' r)
                      | k > b = map' l

这里节点值和参数 k 和 x 没有统一,时不时互换含义使用。除此之外还有好几个函数也是这样颠倒了。

liuxinyu95 commented 6 years ago

多谢指出这个问题。我刚才更新了一下master分支下BSTree.hs的变量命名: https://github.com/liuxinyu95/AlgoXY/blob/eaee9e04ddd815432750ca776f254bc62d6a1268/datastruct/tree/binary-search-tree/src/BSTree.hs

稍后会更新其它分支,并检查AVL树、红黑树、以及Heap等代码中的变量命名。