如何在ML编程语言中定义多个类型的树

好吧,我被要求做下一件事: 要定义一个二进制树,它可以包含两种不同的类型:('a,'b)abtree,这些是要求: 任何内部顶点(不是叶子)必须是“a”或“b”类型,并且叶子没有值。 对于树中的每个路径,所有'值必须出现在'b值之前:路径示例:
'a->'a->''b (legal)
'a->'b->'b (legal)
'a->'a->'a (legal)
'b->'b->'b (legal)
'a->'b->'a (ILLEGAL)
而且我还需要定义另一棵树,就像上面描述的那样,但是现在我也得到了'c,并且在第二个要求中它表示对于每个路径我'值'出现在'b值和所有'b值之前出现在'c值之前。 首先,我不确定如何定义二进制树,其中包含多个类型。 我的意思是最简单的二叉树是:
datatype 'a tree =
          leaf
         | br of 'a * 'a tree * 'a tree;
以及如何定义树以满足这些要求。 任何帮助将不胜感激。 谢谢。 好,多谢。所以你的意思是这样的:
datatype 'b bTree = 
          leaf
        | bBranch of 'b * 'b bTree * 'b bTree
;
datatype ('a,'b) abTree = 
          leaf
        | aBranch of 'a * ('a, 'b) abTree * ('a,'b) abTree
        | bBranch of 'b * 'b bTree * 'b bTree
;
这就是我为这个案例所做的,就像我上面提到的那样,它是一个3型树:
datatype 'c cTree =  
    leaf
    | cBranch of 'c * 'c cTree * 'c cTree
;


datatype ('b, 'c) bcTree = 
            leaf
    | bBranch of 'b * ('b, 'c) bcTree * ('b,'c) bcTree
    | cBranch of 'c * 'c cTree * 'c cTree

;

datatype ('a, 'b, 'c) abcTree = 
    leaf
            | aBranch of 'a * ('a, 'b, 'c) abcTree * ('a, 'b, 'c) abcTree
            | bBranch of 'b * ('b, 'c) bcTree * ('b, 'c) bcTree
    | cBranch of 'c * 'c cTree * 'c cTree
;
我对吗? 另外,叶子的要求是什么意思?那说叶子应该没有价值的那个?     
已邀请:
  首先,我不确定如何定义二进制树,其中包含多个类型。
datatype ('a, 'b) twotypetree = ...
  以及如何定义树以满足这些要求。 将
twotypetree
定义为包含
'a
值的
abranch
和两个
('a, 'b) twotypetree
s或包含
'b tree
的bbranch。 由于
'b tree
不能包含任何
'a
s,因此
bnode
不能包含任何包含
'a
s的子节点,因此满足要求。     

要回复问题请先登录注册