Haskell构造binary Tree|99题(55)
提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录
- 前言
- 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) ]
- 要注意最后得到的是一个tree list
- list comprehension 去罗列所有可能的tree
- 只有两种情况 : 完全balence ; 不完全balence
- 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循环的情况下
弄清循环的分级很重要
总结
- Haskell里的循环要多考虑list comprehension。
- 循环要注意考虑条件的分级, 比如代码中要对同一棵树取fst snd
- 循环分级时需用concat
- 从最基本的树开始分析,一层一层往上照,总归会找出规律,Haskell递归的本质就是,发现规律并表示出来。
Haskell构造binary Tree|99题(55)相关推荐
- LOJ #6669 Nauuo and Binary Tree (交互题、树链剖分)
题目链接 https://loj.ac/problem/6669 题解 Orz yyf太神了,出这种又有意思又有意义的好题造福人类-- 首先\(n\)次询问求出所有节点的深度. 考虑按深度扩展(BFS ...
- codeforce 1311E. Construct the Binary Tree (构造,就是个模拟)
ACM思维题训练集合 You are given two integers n and d. You need to construct a rooted binary tree consisting ...
- HDU 5573 Binary Tree 构造
Binary Tree 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5573 Description The Old Frog King lives ...
- 2019PAT春季考试第4题 7-4 Structure of a Binary Tree (30 分)
题外话:考试的时候花了一个小时做了27分,由于Siblings这个单词不知道意思,所以剩下的3分就没去纠结了,后来发现单词是兄弟的意思,气哭~~ 这道题的麻烦之处在于如何从一个字符串中去找数字.先首先 ...
- LeetCode刷题记录14——257. Binary Tree Paths(easy)
LeetCode刷题记录14--257. Binary Tree Paths(easy) 目录 前言 题目 语言 思路 源码 后记 前言 数据结构感觉理论简单,实践起来很困难. 题目 给定一个二叉树, ...
- 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 ...
- 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 ...
- LeetCode 106. 从中序与后序遍历序列构造二叉树(Construct Binary Tree from Inorder and Postorder Traversal)...
题目描述 根据一棵树的中序遍历与后序遍历构造二叉树. 注意: 你可以假设树中没有重复的元素. 例如,给出 中序遍历 inorder = [9,3,15,20,7] 后序遍历 postorder = [ ...
- 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 ...
最新文章
- 【leetcode】394. Decode String
- int printf(const char* fmt,...)
- CG-CTF-Web-COOKIE
- mysql存储过程编写
- (译)Windsor入门教程---第三部分 编写第一个Installer
- vlan 间路由+单臂路由(实验思路讲解+配置)
- js定义到执行(转)
- 电脑插上U盘双击打不开应用程序右键可以打开问题
- 制作单机俄罗斯方块游戏总结(一)
- mysql workbench 建表时PK, NN, UQ, BIN, UN, ZF, AI
- 音乐编辑软件Cubase分享:扒歌技巧教程
- 象棋 计算机配置,中国象棋电脑应用规范(五)
- Photoshop插件-奥顿效果(梦幻柔焦)-脚本开发-PS插件
- 【java】java Jvm内存结构
- python 中 函数的使用!!!
- linux usb 从芯片,新人求教,怎么烧录Linux系统到一个小芯片上?
- CSP—— 登机牌条码(多项式的求解以及多项式的除法)
- 快速入门一个简单的情感分类项目
- CentOS的 Oracle 11g R2安装
- 事务处理:概念与技术