Emparejamiento de árboles
Los árboles se pueden representar mediante el siguiente tipo de datos
data Arbol a = N a [Arbol a] deriving (Show, Eq)
Por ejemplo, los árboles
1 3 / \ /|\ 6 3 / | \ | 5 4 7 5 | /\ 6 2 1
se representan por
ej1, ej2 :: Arbol Int ej1 = N 1 [N 6 [],N 3 [N 5 []]] ej2 = N 3 [N 5 [N 6 []], N 4 [], N 7 [N 2 [], N 1 []]]
Definir la función
emparejaArboles :: (a -> b -> c) -> Arbol a -> Arbol b -> Arbol c
tal que (emparejaArboles f a1 a2)
es el árbol obtenido aplicando la función f
a los elementos de los árboles a1
y a2
que se encuentran en la misma posición. Por ejemplo,
λ> emparejaArboles (+) (N 1 [N 2 [], N 3[]]) (N 1 [N 6 []]) N 2 [N 8 []] λ> emparejaArboles (+) ej1 ej2 N 4 [N 11 [],N 7 []] λ> emparejaArboles (+) ej1 ej1 N 2 [N 12 [],N 6 [N 10 []]]