[back]

Reversing Rose Trees

type 'a rtree = Node of 'a * 'a rtree list

Implement the function

val rev : 'a rtree -> 'a rtree

so that it reverses the order of nodes, i.e., rev_rtree t is the mirror image of t about the y-axis. Equivalently, the order of children is reversed at every level of the tree, recursively.

Examples

let n x l = Node (x, l)
let _ = assert
  (rev_rtree (n 1 [n 2 [n 3 []; n 4 []]; n 5 []; n 6 [n 7 []; n 8 []]])
   = n 1 [n 6 [n 8 []; n 7 []]; n 5 []; n 2 [n 4 []; n 3 []]])
let _ = assert (rev_rtree (n 1 []) = n 1 [])