简体   繁体   English

OCaml-一棵树的镜像

[英]OCaml - Mirror image of a tree

My tree type is 我的树类型是

    type 'a tree = Tree of 'a * 'a tree list;; 

How can I get the mirror image of a tree like this? 如何获得像这样的树的镜像? For me is confusing having a list of children, because I do not know how to get individually to each child and do the recursion whitout losing the parent's track, any idea? 对我来说,列出孩子列表是令人困惑的,因为我不知道该如何单独对待每个孩子,而递归白白会失去父母的踪迹吗?

EDITED: I have kept trying and I think I got the solution: 编辑:我一直在努力,我认为我得到了解决方案:

    let spec arbol =
         match arbol with
         Tree(a,[])-> Tree(a,[])
         | Tree(a,l)-> Tree(a, List.rev (
                  let rec aux = function
                      Tree(a,[])::[]->Tree(a,[])::[]
                      | Tree(a,[])::l-> [Tree(a,[])]@(aux l)
                      | Tree(a,t)::[]-> [Tree(a, (List.rev (aux t)))]
                      | Tree(a,t)::l-> [Tree(a, (List.rev (aux t)))]@(aux l)
                  in aux l));;

I have tried it with this tree: 我已经用这棵树尝试过了:

let p = Tree(1, [
    Tree(2,[]);
    Tree(3, [
        Tree(6,[]);
        Tree(7,[])
        ]);
    Tree(4,[]);
    Tree(5,[])
]);;

And I got as result of # spec p;; 而我得到了# spec p;;结果# spec p;;

-: int tree = Tree (1, [
                Tree (5,[]); 
                Tree (4,[]); 
                Tree (3,[
                   Tree(7,[]); 
                   Tree(6,[])]); 
                Tree (2,[])
              ])

So I guess my function works as expected. 所以我想我的功能按预期工作。 Let me know if it is not correct 让我知道是否不正确

If I understand the function you're trying to compute, there is a very simple answer that takes one line of code. 如果我了解您要计算的函数,那么有一个非常简单的答案,需要一行代码。

Unfortunately your code introduces so many cases that it's hard to check by eye. 不幸的是,您的代码引入了很多情况,因此很难一目了然。

It looks to me like your aux function is intended to calculate the mirror image of a list of trees. 在我看来,您的aux函数旨在计算树木列表的镜像。 However, it doesn't work on an empty list. 但是,它在空白列表上不起作用。 If you rewrite aux to work on an empty list, you might find that you won't require so many different cases. 如果您将aux重写为一个空列表,您可能会发现您不需要太多不同的情况。 In particular, you could remove your outermost match and half the cases in your inner match. 特别是,您可以删除最外面的匹配项,而删除内部匹配项的一半大小写。

In fact, your aux function (if correct) does all the work. 实际上,您的aux函数(如果正确)可以完成所有工作。 If you look at it properly, you could just use aux for everything. 如果您正确看待它,则可以对所有内容使用aux

Since you're using List.rev , I assume you could also use List.map . 由于您正在使用List.rev ,因此我想您也可以使用List.map That would be something to look at also. 这也是要看的东西。

Update 更新

Trees are inherently recursive structures, so when looking for a tree algorithm, it often helps to imagine how you would use your algorithm recursively. 树本质上是递归结构,因此在查找树算法时,通常有助于想象您将如何递归使用算法。 In this case, you could ask yourself how you would put together mirror images of all the subtrees of a tree to make a mirror image of the whole tree. 在这种情况下,您可能会问自己如何将一棵树的所有子树的镜像放在一起以构成整个树的镜像。

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

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