简体   繁体   English

如何在 OCAML 中分解 2-3-4 树

[英]How to break down 2-3-4 tree in OCAML

I want to break down a 2-3-4 tree to small nodes.我想将 2-3-4 树分解为小节点。

These are the types I am using:这些是我正在使用的类型:

        type ele = int 
        type color = Red|Black
        type ab = Vide | Node of ( ele * color * ab * ab *ab) 
        type ab234 = Vide
        |Node_1 of (ele * ab234* ab234)
        |Node_2 of(ele * ele * ab234 *ab234 *ab234) ``
        |Node_3 of(ele * ele * ele *ab234 * ab234* ab234 *ab234)

I based my mapping on this:我的映射基于此:

2-3-4 树到双色

I need help doing my transformation.我需要帮助来完成我的转型。 I tried with this function but it doesn't seem to work, it does break down the root but it doesn't continue to the branches:我试过这个 function 但它似乎不起作用,它确实分解了根但它不会继续到分支:

    let rec eclat = function
    | Vide -> Vide 
    | Node_3(r,x,y,ag,mg,md,ad) -> eclat ( Node_1(x,(Node_1(r,ag,mg)),Node_1(y,md,ad)))
    | Node_2(r,x,ag,ml,ad) -> eclat(Node_1(r,(Node_1(x,ag,ml)),ad))
    | _ -> failwith("zebi") ;;

If I enter your eclat function as you have it now, I get this:如果我现在输入您的eclat function,我会得到:

Hint: If this is a recursive definition, you should add the 'rec' keyword on line 1提示:如果这是一个递归定义,你应该在第 1 行添加 'rec' 关键字

So that's one problem.所以这是一个问题。

I also note that you changed ab to ab234 , but you didn't change the internal appearances inside the type.我还注意到您将ab更改为ab234 ,但您没有更改类型内部的内部外观。 So the contents of the ab234 type are all defined to be of type ab .所以ab234类型的内容都被定义为ab类型。 This doesn't seem correct.这似乎不正确。

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

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