5 Star 52 Fork 0

Gitee Community/码力传递:晒代码赢奖品

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
haskell实现红黑树.hs 1.69 KB
一键复制 编辑 原始数据 按行查看 历史
rewine 提交于 2020-06-12 15:14 . add haskell实现红黑树.hs.
isBlack (Node Red _ _ _) = False
isBlack _ = True
balL color y (left, True) right = (Node color y left right, True)
balL color y (left, False) right = balL' color y left right
balR color y left (right, True) = (Node color y left right, True)
balR color y left (right, False) = balR' color y left right
balL' color1 p n (Node color2 s sl sr)
| color2 == Red = balL Black s (balL' Red p n sl) sr
| isBlack sl && isBlack sr = (Node Black p n (Node Red s sl sr), color1 == Red)
| not (isBlack sr) = (Node color1 s (Node Black p n sl) (blacken sr), True)
| otherwise = let (Node Red x sll slr) = sl in balL' color1 p n (Node Black x sll (Node Red s slr sr))
balR' color1 p (Node color2 s sl sr) n
| color2 == Red = balR Black s sl (balR' Red p sr n)
| isBlack sl && isBlack sr = (Node Black p (Node Red s sl sr) n, color1 == Red)
| not (isBlack sl) = (Node color1 s (blacken sl) (Node Black p sr n), True)
| otherwise = let (Node Red x srl srr) = sr in balR' color1 p (Node Black x (Node Red s sl srl) srr) n
delete x t = fst $ delete' x t
where delete' x Nil = (Nil, True)
delete' x root@(Node color y left right)
| x < y = balL color y (delete' x left) right
| x > y = balR color y left (delete' x right)
| otherwise = deleteRoot root
deleteRoot (Node color _ Nil Nil) = (Nil, color == Red)
deleteRoot (Node _ _ left Nil) = (blacken left, True)
deleteRoot (Node _ _ Nil right) = (blacken right, True)
deleteRoot (Node color _ left right) = let m = findMin right in balR color m left (delete' m right)
findMin (Node _ x Nil _) = x
findMin (Node _ _ left _) = findMin left
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/gitee-community/gitee-7th-event-3.git
[email protected]:gitee-community/gitee-7th-event-3.git
gitee-community
gitee-7th-event-3
码力传递:晒代码赢奖品
master

搜索帮助