CSE 130 Spring 2011 - Style Exercise 2



Name/SID: ______________________________/____________________

Consider the following Ocaml datatype representing red-blue trees.

type ('a, 'b) rbtree = Red  of 'a 
| Blue of 'b
| Node of ('a, 'b) rbt * ('a, 'b) rbt

Problem 1: Write an OCaml function

val redSum : (int, 'a) rbtree -> int 

that returns the sum of the integers occurring as Red leaves of the red-blue tree. Thus,

Problem 2: Write an Ocaml function

val diff : (int, int) rbtree -> int 

such that diff t returns the number of red leaves minus the number of blue leaves. Thus,

Problem 3: Write an Ocaml function

val flipColor : ('a, 'b) rbtree -> ('b, 'a) rbtree

such that flipColor t returns a tree that looks like t with the leaf colors flipped. Thus,