Question: In ocaml, we've a the two given types and a function ncompress of type ' a n _ tree - > ( ' a *

In ocaml, we've a the two given types and a function ncompress of type 'a n_tree ->('a * int) list that converts an ntree into a list of tuples. The first element of the tuple is stored in the node and the second is the number of immediate children. This follows preorder traversal.
Write a function ndecompress of type('a * int) list ->'a n_tree that takes the ('a * int) list converts it back to an ntree. If the input list is empty, raise an Invalid_arg exception using failwith because it can't represent a completely empty tree.
Can use functions found in Stdlib and List, but not any submodules of Stdlib
type 'a flat = Le | No of 'a
type 'a ntree = Node of 'a * 'a ntree list
let t = Node ("A",[ Node ("B",[]); Node ("C",[Node ("D",[]); Node ("F",[Node ("E",[])])]); Node ("G",[Node ("H",[])])])
let com = ncompress t;;
com:(string * int) list =[("A",3); ("B",0); ("C",2); ("D",0); ("F",1); ("E",0); ("G",1); ("H",0)]
let deco = ndecompress com;;
deco = t;;

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!