-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathArboles_con_igual_estructura.hs
49 lines (44 loc) · 2.02 KB
/
Arboles_con_igual_estructura.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
-- Arboles_con_igual_estructura.hs
-- Árboles con igual estructura.
-- José A. Alonso Jiménez <https://jaalonso.github.io>
-- Sevilla, 29-diciembre-2022
-- ---------------------------------------------------------------------
-- ---------------------------------------------------------------------
-- Usaremos el [tipo de los árboles binarios con valores en los nodos y
-- en las hojas](https://bit.ly/3H53exA).
--
-- Por ejemplo, los árboles
-- 5 8 5 5
-- / \ / \ / \ / \
-- / \ / \ / \ / \
-- 9 7 9 3 9 2 4 7
-- / \ / \ / \ / \ / \ / \
-- 1 4 6 8 1 4 6 2 1 4 6 2
-- se pueden representar por
-- ej3arbol1, ej3arbol2, ej3arbol3, ej3arbol4 :: Arbol Int
-- ej3arbol1 = N 5 (N 9 (H 1) (H 4)) (N 7 (H 6) (H 8))
-- ej3arbol2 = N 8 (N 9 (H 1) (H 4)) (N3 3 (H 6) (H 2))
-- ej3arbol3 = N 5 (N 9 (H 1) (H 4)) (H 2)
-- ej3arbol4 = N 5 (H 4) (N 7 (H 6) (H 2))
--
-- Definir la función
-- igualEstructura :: Arbol -> Arbol -> Bool
-- tal que (igualEstructura a1 a2) se verifica si los árboles a1 y a2
-- tienen la misma estructura. Por ejemplo,
-- igualEstructura ej3arbol1 ej3arbol2 == True
-- igualEstructura ej3arbol1 ej3arbol3 == False
-- igualEstructura ej3arbol1 ej3arbol4 == False
-- ---------------------------------------------------------------------
module Arboles_con_igual_estructura where
import Arboles_binarios (Arbol (H, N))
ej3arbol1, ej3arbol2, ej3arbol3, ej3arbol4 :: Arbol Int
ej3arbol1 = N 5 (N 9 (H 1) (H 4)) (N 7 (H 6) (H 8))
ej3arbol2 = N 8 (N 9 (H 1) (H 4)) (N 3 (H 6) (H 2))
ej3arbol3 = N 5 (N 9 (H 1) (H 4)) (H 2)
ej3arbol4 = N 5 (H 4) (N 7 (H 6) (H 2))
igualEstructura :: Arbol a -> Arbol a -> Bool
igualEstructura (H _) (H _) = True
igualEstructura (N _ i1 d1) (N _ i2 d2) =
igualEstructura i1 i2 &&
igualEstructura d1 d2
igualEstructura _ _ = False