I need to implement a procedure called inverse-tree that receives a tree whose nodes data values are numbers and booleans and returns the equivalent tree whose nodes satisfy the following:
- If the equivalent node of the original tree is a number, then the resulting tree’s node is −1· that node value
- If the equivalent node of the original tree is a boolean, then the resulting tree’s node is the logical not of that node value
Examples:
> (inverse-tree ’())
’()
> (inverse-tree ’(5))
’(-5)
> (inverse-tree ’(0))
’(0)
> (inverse-tree ’(#f))
’(#t)
> (inverse-tree ’(#t))
’(#f)
> (inverse-tree ’(-5 (1 (-2) (3) (#f)) (#t)))
’(5 (-1 (2) (-3) (#t)) (#f))
The representation of a (possibly empty or non-complete) tree in Scheme is as follows: the first element in every nesting level represents the root of the sub-tree. The rest of the elements are the children (each of them is a tree, of course). A leaf is represented by a list with only one element (the leaf value). A completely empty tree is represented by the empty list.
more info at Scheme altering tree values.