简体   繁体   English

OCaml:树函数

[英]OCaml: Tree functions

Are there any modules or functions for dealing with trees?有没有处理树的模块或函数? I have a type that looks like this:我有一个看起来像这样的类型:

type t =
  Leaf of string (* todo: replace with 'a *)
| Node of string * t list

I'm struggling to do insertion, removal of subtrees, etc.我正在努力进行插入、删除子树等操作。

I've used Google but can't find anything.我用过谷歌但找不到任何东西。

Read the implementation of module Set in the sources of the OCaml standard library. 在OCaml标准库的源代码中阅读模块Set的实现。 They are implemented with binary trees, only a little more sophisticated than yours. 它们是用二进制树实现的,仅比您的复杂得多。

(I would recommend you start with binary trees instead of having a list of children as you seem to have defined) (我建议您从二叉树开始,而不要像您定义的那样列出子列表)

In the past, I've used ocamlgraph . 过去,我使用ocamlgraph This is not a trivial lib to use, but if you need to insert nodes and change path, that could the trick, I've never used that in a b-tree context though... 这不是一个普通的库,但是如果您需要插入节点并更改路径,那可能会成功,但是我从来没有在b树上下文中使用它。

And extracted from the language documentation: 并从语言文档中提取:

The most common usage of variant types is to describe recursive data structures. 变量类型最常见的用法是描述递归数据结构。 Consider for example the type of binary trees: 考虑一下例如二叉树的类型:

#type 'a btree = Empty | Node of 'a * 'a btree * 'a btree;;
type 'a btree = Empty | Node of 'a * 'a btree * 'a btree

This definition reads as follow: a binary tree containing values of type 'a (an arbitrary type) is either empty, or is a node containing one value of type 'a and two subtrees containing also values of type 'a, that is, two 'a btree. 该定义如下:包含类型为'a(任意类型)的值的二叉树要么为空,要么为节点,包含一个类型为'a的值,以及两个子树也包含类型为'a的值,即两个'一棵树。

Operations on binary trees are naturally expressed as recursive functions following the same structure as the type definition itself. 对二叉树的操作自然地表示为递归函数,遵循与类型定义本身相同的结构。 For instance, here are functions performing lookup and insertion in ordered binary trees (elements increase from left to right): 例如,以下是在有序二叉树中执行查找和插入的函数(元素从左到右增加):

#let rec member x btree =
   match btree with
     Empty -> false
   | Node(y, left, right) ->
       if x = y then true else
       if x < y then member x left else member x right;;
val member : 'a -> 'a btree -> bool = <fun>

#let rec insert x btree =
   match btree with
     Empty -> Node(x, Empty, Empty)
   | Node(y, left, right) ->
       if x <= y then Node(y, insert x left, right)
                 else Node(y, left, insert x right);;
val insert : 'a -> 'a btree -> 'a btree = <fun>

Hope this helps 希望这可以帮助

Actually it depends on the way you want your tree to work, for example if there is order between elements and such. 实际上,这取决于您希望树的工作方式,例如,元素之间的顺序等。

Otherwise you can use the known algorithms on trees, if you have an idea how to it in other languages C or Java for example, I can help translate it to OCAML. 否则,您可以在树上使用已知的算法,例如,如果您有一个如何使用其他语言C或Java的想法,我可以帮助将其翻译为OCAML。

我认为,Matt McDonnell有一个Ptree数据类型可以满足您的需求。

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

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