简体   繁体   English

使用折叠操作的Haskell树

[英]haskell tree using fold operation

How would I go about writing a contains , balanced function given this definition using folds. 给定使用折叠的定义,我将如何编写一个contains平衡函数。

data Tree a = Tree a [Tree a]

treeFold :: (a -> [b] -> b) -> Tree a -> b
treeContains :: Eq a => a -> Tree a -> Bool
treeBalanced :: Tree a -> Bool

Note that in the above definition, empty trees are not allowed, and that a leaf is a tree with an empty list of subtrees. 请注意,在上面的定义中,不允许有空树,并且叶是带有空子树列表的树。

A contains function determines whether the tree contains a given label. 包含函数确定树是否包含给定标签。

A balanced function determine whether a tree is balanced. 平衡函数确定树是否平衡。

A tree is balanced if the heights of its subtrees different by at most one, and the subtrees are balanced 如果一棵树的子树的高度最大相差一,则该树是平衡的。

The contains function that I have got so far is given below. 到目前为止,我已经包含了contains函数。

treeContains :: Eq a => a -> Tree a -> Bool
treeContains a = treeFold (\x y -> if a == x then True else ...)

So how do you do the else part indicate by ... 那么,您如何做其他部分呢?

Any help would be greatly appreciated. 任何帮助将不胜感激。

As you are learning, I shall answer your question by asking you more questions . 在您学习时, 我将通过问您更多问题来回答您的问题


Re treeContains : Re treeContains

  1. What value do you want to compute in the ... ? 您要在...计算什么值? What should it mean? 什么意思 What is its type? 它是什么类型?

    In the ... , you want to look for the next node in the tree and check if that node is what you are after. ... ,您要查找树中的下一个节点,然后检查该节点是否在您要的位置。

    1. Which node is the next node? 下一个节点是哪个节点? If the current node has four subtrees, why would we want to check only one of them? 如果当前节点有四个子树,为什么我们只想检查其中一个? What if there are no subtrees? 如果没有子树怎么办?

      Check if the node that you are after is in the root. 检查您要寻找的节点是否在根目录中。 If it is return true otherwise check if it is in any of the sub trees. 如果返回true,否则检查它是否在任何子树中。 If it is return true otherwise check if there are any sub sub trees. 如果返回true,否则检查是否有任何子子树。 If there are then check the node in those sub sub tree otherwise return false. 如果存在,则检查那些子子树中的节点,否则返回false。

      1. You are saying you should check the nodes in breadth-first order . 您说的是应该以广度优先的顺序检查节点。 Is that the best order to check them in? 那是检查他们的最佳选择吗? What other orders might you check them? 您还可以检查其他哪些订单? Might they be easier? 他们会更容易吗?
  2. What does y mean? y是什么意思? What is its type? 它是什么类型?

    y is the list containing elements of type b . y是包含类型b元素的列表。

    1. Can you be more specific? 你可以说得更详细点吗? (I am asking in the specific context of treeContains , not the more general context of treeFold .) What is b ? (我是在treeContains的特定上下文中treeContains ,而不是在treeContains的更一般的上下文中treeFold 。) b是什么?

      y is a list containing elements of type Tree a (so b is Tree a ) y是一个包含类型为Tree a元素的列表(因此bTree a

      Are you sure? 你确定吗?

      1. You have been told that treeContains :: Eq a => a -> Tree a -> Bool , so what is the type of treeContains a ? 有人告诉您treeContains :: Eq a => a -> Tree a -> Bool treeContains a treeContains :: Eq a => a -> Tree a -> Bool ,那么treeContains a什么类型?

      2. You have decided that treeContains a = treeFold (\\xy -> if a == x then True else ...) . 您已经确定treeContains a = treeFold (\\xy -> if a == x then True else ...) Given that and your answer to the previous question, what is the type of treeFold (\\xy -> if a == x then True else ...) ? 鉴于此以及您对上一个问题的回答, treeFold (\\xy -> if a == x then True else ...)是什么类型treeFold (\\xy -> if a == x then True else ...)

      3. Given your answer to the previous question, and given that treeFold :: (a -> [b] -> b) -> Tree a -> b , what is the type of (\\xy -> if a == x then True else ...) ? 给定您对上一个问题的答案,并给定treeFold :: (a -> [b] -> b) -> Tree a -> b(\\xy -> if a == x then True else ...)的类型是什么? (\\xy -> if a == x then True else ...)

      4. Given your answer to the previous question, what is the type of y ? 给定您对上一个问题的答案, y的类型是什么?

    2. What does this value mean? 这个值是什么意思? Does it tell you something about other nodes in the tree? 它能告诉您有关树中其他节点的信息吗? Entire subtrees? 整个子树? Which ones? 哪个? What does it tell you about them? 它告诉你关于他们的什么? Their size? 他们的大小? Their height? 他们的身高? Whether they're balanced? 他们是否平衡? Something about their contents? 关于他们的内容?

  3. Could you compute the ... from y ? 您能从y计算...吗? What function(s) would you need? 您需要什么功能? What would it/their type(s) be? 它/它们的类型是什么?


Re treeBalanced : 重新treeBalanced

  1. If you wanted to write a function treeHeight :: Tree a -> Int , how would you do that? 如果要编写函数treeHeight :: Tree a -> Int ,您将如何做? Would you use treeFold ? 您会使用treeFold吗?

  2. If you wanted to write a function treeHeightAndBalanced :: Tree a -> (Int, Bool) , how would you do that? 如果您想编写一个函数treeHeightAndBalanced :: Tree a -> (Int, Bool) ,您将如何做?

  3. If you already had treeHeightAndBalanced :: Tree a -> (Int, Bool) , how could you write treeBalanced :: Tree a -> Bool ? 如果您已经有了treeHeightAndBalanced :: Tree a -> (Int, Bool) ,您怎么能写出treeBalanced :: Tree a -> Bool

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM