提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录

  • 前言
  • 1 基本数据结构
    • 1.1 例子
  • 2 一些题目
    • 2.1 Construct completely balanced binary trees
  • 总结

前言

关于刷haskell题目对于tree的整理。
以下代码来自:WIKI99

1 基本数据结构

data Tree a = Empty | Branch a (Tree a) (Tree a)deriving (Show, Eq)
leaf x = Branch x Empty Empty

1.1 例子

-- A binary tree consisting of a root node only
tree2 = Branch 'a' Empty Empty-- An empty binary tree
tree3 = Empty-- A tree of integers
tree4 = Branch 1 (Branch 2 Empty (Branch 4 Empty Empty))(Branch 2 Empty Empty)

2 一些题目

2.1 Construct completely balanced binary trees

要求构成平衡二叉树 :
平衡:左边和右边的差距最大为1
二叉树:一个Branch只有两个sub tree

λ> cbalTree 4
[
-- permutation 1
--     x
--    / \
--   x   x
--        \
--         x
Branch 'x' (Branch 'x' Empty Empty) (Branch 'x' Empty (Branch 'x' Empty Empty)),-- permutation 2
--     x
--    / \
--   x   x
--      /
--     x
Branch 'x' (Branch 'x' Empty Empty) (Branch 'x' (Branch 'x' Empty Empty) Empty),-- permutation 3
--     x
--    / \
--   x   x
--    \
--     x
Branch 'x' (Branch 'x' Empty (Branch 'x' Empty Empty)) (Branch 'x' Empty Empty),-- permutation 4
--     x
--    / \
--   x   x
--  /
-- x
Branch 'x' (Branch 'x' (Branch 'x' Empty Empty) Empty) (Branch 'x' Empty Empty)
]

Answer 1:

cbalTree 0 = [Empty]
cbalTree 1 = [leaf 'x']
cbalTree n = if n `mod` 2 == 1 then [ Branch 'x' l r | l <- cbalTree ((n - 1) `div` 2), r <- cbalTree ((n - 1) `div` 2) ] else concat [ [Branch 'x' l r, Branch 'x' r l] | l <- cbalTree ((n - 1) `div` 2), r <- cbalTree (n `div` 2) ]
  1. 要注意最后得到的是一个tree list
  2. list comprehension 去罗列所有可能的tree
  3. 只有两种情况 : 完全balence ; 不完全balence
  4. concat 使用注意去掉一层括号

Answer 2:
这个答案很具有参考性,更加具有普遍性
可以用于其他场景: 左树比右树高2;3;4…

main = putStrLn $ concatMap (\t -> show t ++ "\n") balTreeswhere balTrees = filter isBalancedTree (makeTrees 'x' 4)isBalancedTree :: Tree a -> Bool
isBalancedTree Empty = True
isBalancedTree (Branch _ l r) = abs (countBranches l - countBranches r) <= 1&& isBalancedTree l && isBalancedTree r
isBalancedTree _ = FalsecountBranches :: Tree a -> Int
countBranches Empty = 0
countBranches (Branch _ l r) = 1 + countBranches l + countBranches r-- makes all possible trees filled with the given number of nodes
-- and fill them with the given value
makeTrees :: a -> Int -> [Tree a]
makeTrees _ 0 = []
makeTrees c 1 = [leaf c]
makeTrees c n = lonly ++ ronly ++ landrwhere lonly  = [Branch c t Empty | t <- smallerTree]ronly = [Branch c Empty t | t <- smallerTree]landr = concat [[Branch c l r | l <- fst lrtrees, r <- snd lrtrees] | lrtrees <- treeMinusTwo]smallerTree = makeTrees c (n-1)treeMinusTwo = [(makeTrees c num, makeTrees c (n-1-num)) | num <- [0..n-2]]

concat 是在2 个for循环的情况下
弄清循环的分级很重要


总结

  1. Haskell里的循环要多考虑list comprehension。
  2. 循环要注意考虑条件的分级, 比如代码中要对同一棵树取fst snd
  3. 循环分级时需用concat
  4. 从最基本的树开始分析,一层一层往上照,总归会找出规律,Haskell递归的本质就是,发现规律并表示出来。

Haskell构造binary Tree|99题(55)相关推荐

  1. LOJ #6669 Nauuo and Binary Tree (交互题、树链剖分)

    题目链接 https://loj.ac/problem/6669 题解 Orz yyf太神了,出这种又有意思又有意义的好题造福人类-- 首先\(n\)次询问求出所有节点的深度. 考虑按深度扩展(BFS ...

  2. codeforce 1311E. Construct the Binary Tree (构造,就是个模拟)

    ACM思维题训练集合 You are given two integers n and d. You need to construct a rooted binary tree consisting ...

  3. HDU 5573 Binary Tree 构造

    Binary Tree 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5573 Description The Old Frog King lives ...

  4. 2019PAT春季考试第4题 7-4 Structure of a Binary Tree (30 分)

    题外话:考试的时候花了一个小时做了27分,由于Siblings这个单词不知道意思,所以剩下的3分就没去纠结了,后来发现单词是兄弟的意思,气哭~~ 这道题的麻烦之处在于如何从一个字符串中去找数字.先首先 ...

  5. LeetCode刷题记录14——257. Binary Tree Paths(easy)

    LeetCode刷题记录14--257. Binary Tree Paths(easy) 目录 前言 题目 语言 思路 源码 后记 前言 数据结构感觉理论简单,实践起来很困难. 题目 给定一个二叉树, ...

  6. leetcode题解:Construct Binary Tree from Preorder and Inorder Traversal (根据前序和中序遍历构造二叉树)...

    题目: Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume ...

  7. LeetCode Construct Binary Tree from Preorder and Inorder Traversal(构造二叉树)

    Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume that ...

  8. LeetCode 106. 从中序与后序遍历序列构造二叉树(Construct Binary Tree from Inorder and Postorder Traversal)...

    题目描述 根据一棵树的中序遍历与后序遍历构造二叉树. 注意: 你可以假设树中没有重复的元素. 例如,给出 中序遍历 inorder = [9,3,15,20,7] 后序遍历 postorder = [ ...

  9. LeetCode OJ:Construct Binary Tree from Preorder and Inorder Traversal(从前序以及中序遍历结果中构造二叉树)...

    Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume that ...

最新文章

  1. 【leetcode】394. Decode String
  2. int printf(const char* fmt,...)
  3. CG-CTF-Web-COOKIE
  4. mysql存储过程编写
  5. (译)Windsor入门教程---第三部分 编写第一个Installer
  6. vlan 间路由+单臂路由(实验思路讲解+配置)
  7. js定义到执行(转)
  8. 电脑插上U盘双击打不开应用程序右键可以打开问题
  9. 制作单机俄罗斯方块游戏总结(一)
  10. mysql workbench 建表时PK, NN, UQ, BIN, UN, ZF, AI
  11. 音乐编辑软件Cubase分享:扒歌技巧教程
  12. 象棋 计算机配置,中国象棋电脑应用规范(五)
  13. Photoshop插件-奥顿效果(梦幻柔焦)-脚本开发-PS插件
  14. 【java】java Jvm内存结构
  15. python 中 函数的使用!!!
  16. linux usb 从芯片,新人求教,怎么烧录Linux系统到一个小芯片上?
  17. CSP—— 登机牌条码(多项式的求解以及多项式的除法)
  18. 快速入门一个简单的情感分类项目
  19. CentOS的 Oracle 11g R2安装
  20. 事务处理:概念与技术

热门文章

  1. 关乎游戏生死的“内容战争”
  2. 沈阳师范大学 计算机专业分数低,辽宁省录取分数不高的3所师范类高校、就业率不低...
  3. Foxdisk-代码仓库介绍暨完结篇
  4. FTP远程工具:8个值推荐的FTP远程工具
  5. 跨境电商独立站社媒内容营销, Instagram Guide 使用指南!
  6. Linux查看本机状况命令
  7. OBS录制全屏游戏的方法(超好录屏)
  8. 2021-10-25 变压器零序电抗分类 总结
  9. 2022流动式起重机司机考试试题及在线模拟考试
  10. Java 字节数组和字符串的相互转化