简体   繁体   中英

How to break down 2-3-4 tree in OCAML

I want to break down a 2-3-4 tree to small nodes.

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:

    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:

Hint: If this is a recursive definition, you should add the 'rec' keyword on line 1

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. So the contents of the ab234 type are all defined to be of type ab . This doesn't seem correct.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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