Diferencia entre revisiones de «Relación 8»
De Razonamiento automático (2014-15)
(Página creada con '<source lang="isar"> header {* R8: Árboles binarios completos *} theory R8_Arboles_binarios_completos imports Main begin text {* ---------------------------------------...') |
|||
Línea 34: | Línea 34: | ||
*} | *} | ||
+ | -- "javrodviv1" | ||
fun hojas :: "arbol => nat" where | fun hojas :: "arbol => nat" where | ||
− | "hojas | + | "hojas H = 1" |
− | + | | "hojas (N i d) = hojas i + hojas d" | |
+ | |||
value "hojas (N (N H H) (N H H))" -- "= 4" | value "hojas (N (N H H) (N H H))" -- "= 4" | ||
Línea 47: | Línea 49: | ||
--------------------------------------------------------------------- | --------------------------------------------------------------------- | ||
*} | *} | ||
+ | |||
+ | -- "javrodviv1" | ||
+ | fun max :: "nat ⇒ nat ⇒ nat" where | ||
+ | "max a b= (if a≥ b then a else b)" | ||
+ | |||
+ | fun min :: "nat ⇒ nat ⇒ nat" where | ||
+ | "min a b= (if a≤b then a else b)" | ||
+ | |||
+ | value "max 6 3" -- "= 6" | ||
+ | |||
+ | fun profundidad1 :: "arbol => nat" where | ||
+ | "profundidad1 H = 0" | ||
+ | | "profundidad1 (N a b) = 1 + max (profundidad1 a) (profundidad1 b)" | ||
fun profundidad :: "arbol => nat" where | fun profundidad :: "arbol => nat" where | ||
− | "profundidad | + | "profundidad H = 0" |
+ | | "profundidad (N a b) = 1 + min (profundidad a) (profundidad b)" | ||
value "profundidad (N (N H H) (N H H))" -- "= 2" | value "profundidad (N (N H H) (N H H))" -- "= 2" | ||
+ | |||
+ | (* Javrodviv1: Explico estas dos funciones, la que utilizo para los | ||
+ | lemas de abajo es profundidad ya que profundidad1 me encuentra | ||
+ | contraejemplos y para evitar eso busque definir profundidad | ||
+ | definidad de manera distinta. No obstante la que a mi me parece | ||
+ | mas sensata para definir la profundidad de un árbol es la | ||
+ | profundidad1 *) | ||
text {* | text {* | ||
Línea 63: | Línea 86: | ||
*} | *} | ||
+ | -- "javrodviv1" | ||
fun abc :: "nat ⇒ arbol" where | fun abc :: "nat ⇒ arbol" where | ||
− | "abc 0 = | + | "abc 0 = H" |
+ | | "abc (Suc n) = N (abc n) (abc n)" | ||
value "abc 3" -- "= N (N (N H H) (N H H)) (N (N H H) (N H H))" | value "abc 3" -- "= N (N (N H H) (N H H)) (N (N H H) (N H H))" | ||
Línea 82: | Línea 107: | ||
*} | *} | ||
+ | -- "javrodviv1" | ||
fun es_abc :: "(arbol => 'a) => arbol => bool" where | fun es_abc :: "(arbol => 'a) => arbol => bool" where | ||
− | "es_abc f | + | "es_abc f H = True" |
+ | | "es_abc f (N a b) = ((f a = f b) ∧ (es_abc f a ∧ es_abc f b))" | ||
text {* | text {* |
Revisión del 14:10 6 dic 2014
header {* R8: Árboles binarios completos *}
theory R8_Arboles_binarios_completos
imports Main
begin
text {*
---------------------------------------------------------------------
Ejercicio 1. Definir el tipo de datos arbol para representar los
árboles binarios que no tienen información ni en los nodos y ni en las
hojas. Por ejemplo, el árbol
·
/ \
/ \
· ·
/ \ / \
· · · ·
se representa por "N (N H H) (N H H)".
---------------------------------------------------------------------
*}
datatype arbol = H | N arbol arbol
value "N (N H H) (N H H)"
text {*
---------------------------------------------------------------------
Ejercicio 2. Definir la función
hojas :: "arbol => nat"
tal que (hojas a) es el número de hojas del árbol a. Por ejemplo,
hojas (N (N H H) (N H H)) = 4
---------------------------------------------------------------------
*}
-- "javrodviv1"
fun hojas :: "arbol => nat" where
"hojas H = 1"
| "hojas (N i d) = hojas i + hojas d"
value "hojas (N (N H H) (N H H))" -- "= 4"
text {*
---------------------------------------------------------------------
Ejercicio 4. Definir la función
profundidad :: "arbol => nat"
tal que (profundidad a) es la profundidad del árbol a. Por ejemplo,
profundidad (N (N H H) (N H H)) = 2
---------------------------------------------------------------------
*}
-- "javrodviv1"
fun max :: "nat ⇒ nat ⇒ nat" where
"max a b= (if a≥ b then a else b)"
fun min :: "nat ⇒ nat ⇒ nat" where
"min a b= (if a≤b then a else b)"
value "max 6 3" -- "= 6"
fun profundidad1 :: "arbol => nat" where
"profundidad1 H = 0"
| "profundidad1 (N a b) = 1 + max (profundidad1 a) (profundidad1 b)"
fun profundidad :: "arbol => nat" where
"profundidad H = 0"
| "profundidad (N a b) = 1 + min (profundidad a) (profundidad b)"
value "profundidad (N (N H H) (N H H))" -- "= 2"
(* Javrodviv1: Explico estas dos funciones, la que utilizo para los
lemas de abajo es profundidad ya que profundidad1 me encuentra
contraejemplos y para evitar eso busque definir profundidad
definidad de manera distinta. No obstante la que a mi me parece
mas sensata para definir la profundidad de un árbol es la
profundidad1 *)
text {*
---------------------------------------------------------------------
Ejercicio 5. Definir la función
abc :: "nat ⇒ arbol"
tal que (abc n) es el árbol binario completo de profundidad n. Por
ejemplo,
abc 3 = N (N (N H H) (N H H)) (N (N H H) (N H H))
---------------------------------------------------------------------
*}
-- "javrodviv1"
fun abc :: "nat ⇒ arbol" where
"abc 0 = H"
| "abc (Suc n) = N (abc n) (abc n)"
value "abc 3" -- "= N (N (N H H) (N H H)) (N (N H H) (N H H))"
text {*
---------------------------------------------------------------------
Ejercicio 6. Un árbol binario a es completo respecto de la medida f si
a es una hoja o bien a es de la forma (N i d) y se cumple que tanto i
como d son árboles binarios completos respecto de f y, además,
f(i) = f(r).
Definir la función
es_abc :: "(arbol => 'a) => arbol => bool
tal que (es_abc f a) se verifica si a es un árbol binario completo
respecto de f.
---------------------------------------------------------------------
*}
-- "javrodviv1"
fun es_abc :: "(arbol => 'a) => arbol => bool" where
"es_abc f H = True"
| "es_abc f (N a b) = ((f a = f b) ∧ (es_abc f a ∧ es_abc f b))"
text {*
---------------------------------------------------------------------
Nota. (size a) es el número de nodos del árbol a. Por ejemplo,
size (N (N H H) (N H H)) = 3
---------------------------------------------------------------------
*}
value "size (N (N H H) (N H H))"
value "size (N (N (N H H) (N H H)) (N (N H H) (N H H)))"
text {*
---------------------------------------------------------------------
Nota. Tenemos 3 funciones de medida sobre los árboles: número de
hojas, número de nodos y profundidad. A cada una le corresponde un
concepto de completitud. En los siguientes ejercicios demostraremos
que los tres conceptos de completitud son iguales.
---------------------------------------------------------------------
*}
text {*
---------------------------------------------------------------------
Ejercicio 7. Demostrar que un árbol binario a es completo respecto de
la profundidad syss es completo respecto del número de hojas.
---------------------------------------------------------------------
*}
text {*
---------------------------------------------------------------------
Ejercicio 8. Demostrar que un árbol binario a es completo respecto del
número de hojas syss es completo respecto del número de nodos
---------------------------------------------------------------------
*}
text {*
---------------------------------------------------------------------
Ejercicio 9. Demostrar que un árbol binario a es completo respecto de
la profundidad syss es completo respecto del número de nodos
---------------------------------------------------------------------
*}
text {*
---------------------------------------------------------------------
Ejercicio 10. Demostrar que (abc n) es un árbol binario completo.
---------------------------------------------------------------------
*}
text {*
---------------------------------------------------------------------
Ejercicio 11. Demostrar que si a es un árbolo binario completo
respecto de la profundidad, entonces a es (abc (profundidad a)).
---------------------------------------------------------------------
*}
text {*
---------------------------------------------------------------------
Ejercicio 12. Encontrar una medida f tal que (es_abc f) es distinto de
(es_abc size).
---------------------------------------------------------------------
*}
end