简体   繁体   English

在Ocaml的树中添加元素

[英]Add elements in 'a tree in Ocaml

So, here is my problem.. I need to write a function that will add elements in a tree and it is a binary tree, so it must be well organized. 因此,这是我的问题。我需要编写一个函数,该函数将在树中添加元素,并且它是二叉树,因此必须组织得很好。 The problem is in how is my tree defined. 问题在于如何定义我的树。 I have this tree: 我有这棵树:

type 'a tree = {
  mutable cont:'a;
  mutable left:'a bin_tree;
  mutable right:'a bin_tree
  }
  and 'a bin_tree = 
  Empty
  |Node of 'a bin_tree;;

So, when I write the function to add elements in this tree it says that the tree is of type 'a tree and I am using function for 'a bin_tree. 因此,当我编写在该树中添加元素的函数时,它说该树的类型为“树”,而我正在将函数用于“ bin_tree”。

I tried a lot of different ways to write that function and I get the same error. 我尝试了许多不同的方式来编写该函数,但遇到了相同的错误。 The function I used last is: 我上次使用的功能是:

let rec dodajVDrevo x = function
Empty -> Node{cont=x; left=Empty; right=Empty}
|Node{cont; left; right} -> if x < cont then Node{cont; left= dodajVDrevo x left; right}
else if x > cont then Node{cont; left; right = dodajVDrevo x right}
else Node{cont; left; right};;

Please help me and give some clues. 请帮助我并提供一些线索。

Thank you! 谢谢!

Node构造函数应采用'a tree而不是'a bin_tree

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

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