Question: In Ocaml, the function compress takes a tree and converts it to a list of custom type comp using postorder traversal. Write a function decompress

In Ocaml, the function "compress" takes a tree and converts it to a list of custom type comp using postorder traversal.
Write a function "decompress" that restores the list back to a tree. (Example at bottom.)
type 'a tree =
| Node of 'a * 'a tree * 'a tree
| Leaf
type 'a comp =
| No of 'a
| Le
EXAMPLE
let t =Node (50, Leaf, Node (75,Node (50,Leaf, Leaf), Node (125,Leaf, Leaf)))
let co = compress t;;
(* 'a flat list: [Le; Le; Le; No 50; Le; Le; No 125; No 75; No 50]*)
let deco = decompress co;;
(* 'a tree: Node (50, Leaf, Node (75,Node (50,Leaf, Leaf), Node (125,Leaf, Leaf)))*)

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!