好多種類型的定義的樹,我要求做的下一件事:如何與ML編程語言
要定義一個二叉樹它可以包含兩種不同類型:(「A」 B) abtree,這些是要求:
- 任何內部頂點(不是葉)必須是'a或'b類型,並且葉子沒有值。
對於樹中的b值爲所有「的前一個值必須出現」的每一條路徑:路徑例子:
'a->'a->'a-'b (legal) 'a->'b->'b (legal) 'a->'a->'a (legal) 'b->'b->'b (legal) 'a->'b->'a (ILLEGAL)
並且還我需要定義另一個樹,其是等中描述的一個上面,但現在我也得到了'c,並在第二個要求中說,對於每一條路徑,'值出現在'b值之前,所有'b值出現在'c值之前。
首先,我不知道如何定義二叉樹有超過1種類型。 我的意思是最簡單的二叉樹是:
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
;
我說得對不對?
另外,葉子的要求是什麼意思?那個說葉子應該沒有價值的人?
我在「我自己的anwser」上添加了更多信息。 – user550413 2010-12-22 16:09:54