【算法】算法图解笔记_广度优先搜索 -Haskell代码实现

(点击上方公众号,可快速关注)

之前的广度优先遍历没有Haskell代码的实现,这里补上。下面代码使用了unordered-containers包的哈希表,用来实现图;containers包的Seq类型,用来实现队列,主要是因为使用内置的列表类型效率太差。

module Main where

import qualified Data.HashMap.Strict as HM
import           Data.Maybe (fromJust)
import qualified Data.Sequence       as DS

graph :: HM.HashMap String [String]
graph = HM.fromList [("you",["alice", "bob", "claire"]),
                    ("bob", ["anuj", "peggy"]),
                    ("alice", ["peggy"]),
                    ("claire", ["thom", "jonny"]),
                    ("anuj",[]),
                    ("peggy",[]),
                    ("thom",[]),
                    ("jonny",[])
                   ]

personIsSeller :: String -> Bool
personIsSeller name = last name == 'm'

search :: HM.HashMap String [String] -> String -> Bool
search graph name = loop $ DS.fromList (graph HM.! name)
  where loop queue
          | null queue = False
          | personIsSeller h = True
          | otherwise = loop $ (DS.drop 1 queue) DS.>< DS.fromList (graph HM.! h)
          where h = queue `DS.index` 0

main :: IO ()
main = do
  print $ search graph "you"

为了能与Python对照,整体风格上与Python代码保持一致,包括测试用例、图的实现方式等等。
Haskell标准模块并不包含一些常用的数据类型,所以,日常开发中需要大量使用其他三方的包,其中containers和unordered-containers是最常用的容器包。
containers包含常用的图、树、集合等结构,具体包含Data.Graph、Data.Tree、Data.Map、Data.IntMap、Data.Set、Data.IntSet、以及例子中使用到的Data.Sequence等。
正如名字所示,unordered-containers包实现了基于无序的数据结构,包含基于哈希的映射和集合。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
平衡二叉树(Balanced Binary Tree)又称为 AVL 树,它是一种自平衡二叉查找树。它的性质是:任何一个节点的左子树和右子树的高度差都不超过 1。 下面是 Haskell 实现平衡二叉树的代码: ``` data Tree a = Leaf | Node (Tree a) a (Tree a) deriving (Show) -- 计算树的高度 height :: Tree a -> Int height Leaf = 0 height (Node l _ r) = 1 + max (height l) (height r) -- 右旋 rightRotate :: Tree a -> Tree a rightRotate (Node (Node a x b) y c) = Node a x (Node b y c) rightRotate t = t -- 左旋 leftRotate :: Tree a -> Tree a leftRotate (Node a x (Node b y c)) = Node (Node a x b) y c leftRotate t = t -- 插入 insert :: (Ord a) => a -> Tree a -> Tree a insert x Leaf = Node Leaf x Leaf insert x (Node l y r) | x < y = balance (Node (insert x l) y r) | x > y = balance (Node l y (insert x r)) | otherwise = Node l y r -- 平衡因子 balanceFactor :: Tree a -> Int balanceFactor Leaf = 0 balanceFactor (Node l _ r) = height l - height r -- 平衡 balance :: Tree a -> Tree a balance t | bf > 1 && x < y = rightRotate t | bf < -1 && x > y = leftRotate t | bf > 1 && x > y = leftRotate (rightRotate t) | bf < -1 && x < y = rightRotate (leftRotate t) | otherwise = t where bf = balanceFactor t x = value t y = value (leftChild t) -- 左子节点 leftChild :: Tree a -> Tree a leftChild (Node l _ _) = l -- 右子节点 rightChild :: Tree a -> Tree a rightChild (Node _ _ r) = r -- 节点的值 value :: Tree a -> a value (Node _ x _) = x -- 创建平衡二叉树 create :: (Ord a) => [a] -> Tree a create = foldl (flip insert) Leaf ``` 使用方法: ``` main = do let t = create [3, 2, 1, 4, 5] print t ``` 输
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值