So far, we used a very inflexible definition of binary trees for storing integer values. Now, we relax this limitation by defining the binary tree as a polymorphic type, such that arbitrary values may be stored at the nodes:
type 'a tree = Empty | Node of 'a * 'a tree * 'a tree
Complete the following tasks using the above definition:
-
insert
Implement a functioninsert : ('a -> 'a -> int) -> 'a -> 'a tree -> 'a tree
to insert a value (second argument) into the tree (third argument) while preserving the binary search tree invariant (without changing the position or values of any other nodes). If the new value is considered equal to an existing value, it should not be inserted. Note, since we do not know the type stored in the tree, the default comparison operators (<
,<=
, …) cannot be used anymore to decide where to insert a new value. Imagine that we could use the tree to storestudent
records ordered byid
. Instead, a compare function has to be passed toinsert
as the first argument. This function compares two values of type'a
and has to return a negative number if the first value goes before the second, a positive number if the second goes before the first, and$0$ if both values are considered equal. -
string_of_tree
Implement a functionstring_of_tree : ('a -> string) -> 'a tree -> string
that constructs a string representation of the given tree. For example, forNode (7, Node (3, Empty, Empty), Empty)
, the string shall be"Node (7, Node (3, Empty, Empty), Empty)"
. Again, in order to tellstring_of_tree
how to convert type'a
to astring
, a corresponding function has to be passed as the first argument.Hint: The tests compare the string case-sensitively, but space-insensitively.
-
inorder_list
Implement a functioninorder_list : 'a tree -> 'a list
that outputs all values inside the tree to a list in order. The function must be tail recursive. In particular, your function must only use constant stack space.Hint:
xs @ ys
, for example, is not tail-recursive and uses stack space in$\mathcal{O}(\texttt{length\enspace xs})$