{"id":7737,"date":"2022-12-22T06:00:49","date_gmt":"2022-12-22T04:00:49","guid":{"rendered":"http:\/\/www.glc.us.es\/~jalonso\/exercitium\/?p=7737"},"modified":"2022-12-20T19:18:29","modified_gmt":"2022-12-20T17:18:29","slug":"22-dic-22","status":"publish","type":"post","link":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/22-dic-22\/","title":{"rendered":"\u00c1rbol de profundidad n con nodos iguales"},"content":{"rendered":"<p>El \u00e1rbol binario<\/p>\n<pre lang=\"text\">\n        9\n       \/ \\\n      \/   \\\n     3     7\n    \/ \\\n   2   4\n<\/pre>\n<p>se puede representar por<\/p>\n<pre lang=\"text\">\n   N 9 (N 3 (H 2) (H 4)) (H 7)\n<\/pre>\n<p>El tipo de los \u00e1rboles binarios se puede definir por<\/p>\n<pre lang=\"text\">\n   data Arbol a = H a\n                | N a (Arbol a) (Arbol a)\n     deriving (Show, Eq)\n<\/pre>\n<p>Definir las funciones<\/p>\n<pre lang=\"text\">\n   repeatArbol    :: a -> Arbol a\n   replicateArbol :: Int -> a -> Arbol a\n<\/pre>\n<p>tales que<\/p>\n<ul>\n<li><code>repeatArbol x<\/code> es es \u00e1rbol con infinitos nodos <code>x<\/code>. Por ejemplo,<\/li>\n<\/ul>\n<pre lang=\"text\">\n     takeArbol 0 (repeatArbol 3) == H 3\n     takeArbol 1 (repeatArbol 3) == N 3 (H 3) (H 3)\n     takeArbol 2 (repeatArbol 3) == N 3 (N 3 (H 3) (H 3)) (N 3 (H 3) (H 3))\n<\/pre>\n<ul>\n<li><code>replicate n x<\/code> es el \u00e1rbol de profundidad <code>n<\/code> cuyos nodos son <code>x<\/code>. Por ejemplo,<\/li>\n<\/ul>\n<pre lang=\"text\">\n     replicateArbol 0 5  ==  H 5\n     replicateArbol 1 5  ==  N 5 (H 5) (H 5)\n     replicateArbol 2 5  ==  N 5 (N 5 (H 5) (H 5)) (N 5 (H 5) (H 5))\n<\/pre>\n<p>Comprobar con QuickCheck que el n\u00famero de hojas de <code>replicateArbol n x<\/code> es <code>2^n<\/code>, para todo n\u00famero natural <code>n<\/code>.<br \/>\n<b>Soluciones<\/b><\/p>\n<p>A continuaci\u00f3n se muestran las <a href=\"#haskell\">soluciones en Haskell<\/a> y las <a href=\"#python\">soluciones en Python<\/a>.<\/p>\n<p><a name=\"haskell\"><\/a><br \/>\n<b>Soluciones en Haskell<\/b><\/p>\n<pre lang=\"haskell\">\nimport Test.QuickCheck\n\ndata Arbol a = H a\n             | N a (Arbol a) (Arbol a)\n  deriving (Show, Eq)\n\nrepeatArbol :: a -> Arbol a\nrepeatArbol x = N x t t\n  where t = repeatArbol x\n\nreplicateArbol :: Int -> a -> Arbol a\nreplicateArbol n = takeArbol n . repeatArbol\n\n-- (takeArbol n t) es el sub\u00e1rbol de t de profundidad n. Por ejemplo,\n--    takeArbol 0 (N 9 (N 3 (H 2) (H 4)) (H 7)) == H 9\n--    takeArbol 1 (N 9 (N 3 (H 2) (H 4)) (H 7)) == N 9 (H 3) (H 7)\n--    takeArbol 2 (N 9 (N 3 (H 2) (H 4)) (H 7)) == N 9 (N 3 (H 2) (H 4)) (H 7)\n--    takeArbol 3 (N 9 (N 3 (H 2) (H 4)) (H 7)) == N 9 (N 3 (H 2) (H 4)) (H 7)\ntakeArbol :: Int -> Arbol a -> Arbol a\ntakeArbol _ (H x)     = H x\ntakeArbol 0 (N x _ _) = H x\ntakeArbol n (N x i d) = N x (takeArbol (n-1) i) (takeArbol (n-1) d)\n\n-- Generador para las comprobaciones\n-- =================================\n\n-- (arbolArbitrario n) es un \u00e1rbol aleatorio de altura n. Por ejemplo,\n--    \u03bb> sample (arbolArbitrario 3 :: Gen (Arbol Int))\n--    N 0 (H 0) (H 0)\n--    N 1 (N (-2) (H (-1)) (H 1)) (H 2)\n--    N 3 (H 1) (H 2)\n--    N 6 (N 0 (H 5) (H (-5))) (N (-5) (H (-5)) (H 4))\n--    H 7\n--    N (-8) (H (-8)) (H 9)\n--    H 2\n--    N (-1) (H 7) (N 9 (H (-2)) (H (-8)))\n--    H (-3)\n--    N 0 (N 16 (H (-14)) (H (-18))) (H 7)\n--    N (-16) (H 18) (N (-19) (H (-15)) (H (-18)))\narbolArbitrario :: Arbitrary a => Int -> Gen (Arbol a)\narbolArbitrario 0 = H <$> arbitrary\narbolArbitrario n =\n  oneof [H <$> arbitrary,\n         N <$> arbitrary <*> arbolArbitrario (div n 2) <*> arbolArbitrario (div n 2)]\n\n-- Arbol es subclase de Arbitrary\ninstance Arbitrary a => Arbitrary (Arbol a) where\n  arbitrary = sized arbolArbitrario\n\n-- Funci\u00f3n auxiliar para la comprobaci\u00f3n\n-- =====================================\n\n-- (nHojas x) es el n\u00famero de hojas del \u00e1rbol x. Por ejemplo,\n--    nHojas (N 9 (N 3 (H 2) (H 4)) (H 7))  ==  3\nnHojas :: Arbol a -> Int\nnHojas (H _)     = 1\nnHojas (N _ i d) = nHojas i + nHojas d\n\n-- Comprobaci\u00f3n de la propiedad\n-- ============================\n\n-- La propiedad es\nprop_replicateArbol :: Int -> Int -> Property\nprop_replicateArbol n x =\n  n >= 0 ==> nHojas (replicateArbol n x) == 2^n\n\n-- La comprobaci\u00f3n es\n--    \u03bb> quickCheckWith (stdArgs {maxSize=7}) prop_replicateArbol\n--    +++ OK, passed 100 tests.\n<\/pre>\n<p><a name=\"python\"><\/a><br \/>\n<b>Soluciones en Python<\/b><\/p>\n<pre lang=\"python\">\nfrom dataclasses import dataclass\nfrom random import choice, randint\nfrom typing import Generic, TypeVar\n\nfrom hypothesis import given\nfrom hypothesis import strategies as st\n\nA = TypeVar(\"A\")\n\n@dataclass\nclass Arbol(Generic[A]):\n    pass\n\n@dataclass\nclass H(Arbol[A]):\n    x: A\n\n@dataclass\nclass N(Arbol[A]):\n    x: A\n    i: Arbol[A]\n    d: Arbol[A]\n\ndef replicateArbol(n: int, x: A) -> Arbol[A]:\n    match n:\n        case 0:\n            return H(x)\n        case n:\n            t = replicateArbol(n - 1, x)\n            return N(x, t, t)\n    assert False\n\n# Generador para las comprobaciones\n# =================================\n\n# (arbolArbitrario n) es un \u00e1rbol aleatorio de orden n. Por ejemplo,\n#    >>> arbolArbitrario(4)\n#    N(x=2, i=H(x=1), d=H(x=9))\n#    >>> arbolArbitrario(4)\n#    H(x=10)\n#    >>> arbolArbitrario(4)\n#    N(x=4, i=N(x=7, i=H(x=4), d=H(x=0)), d=H(x=6))\ndef arbolArbitrario(n: int) -> Arbol[int]:\n    if n <= 1:\n        return H(randint(0, 10))\n    m = n \/\/ 2\n    return choice([H(randint(0, 10)),\n                   N(randint(0, 10),\n                     arbolArbitrario(m),\n                     arbolArbitrario(m))])\n\n# Funci\u00f3n auxiliar para la comprobaci\u00f3n\n# =====================================\n\n# nHojas(x) es el n\u00famero de hojas del \u00e1rbol x. Por ejemplo,\n#    nHojas(N(9, N(3, H(2), H(4)), H(7)))  ==  3\ndef nHojas(a: Arbol[A]) -> int:\n    match a:\n        case H(_):\n            return 1\n        case N(_, i, d):\n            return nHojas(i) + nHojas(d)\n    assert False\n\n# Comprobaci\u00f3n de la propiedad\n# ============================\n\n# La propiedad es\n@given(st.integers(min_value=1, max_value=10),\n       st.integers(min_value=1, max_value=10))\ndef test_nHojas(n: int, x: int) -> None:\n    assert nHojas(replicateArbol(n, x)) == 2**n\n\n# La comprobaci\u00f3n es\n#    src> poetry run pytest -q arbol_de_profundidad_n_con_nodos_iguales.py\n#    1 passed in 0.20s\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>El \u00e1rbol binario 9 \/ \\ \/ \\ 3 7 \/ \\ 2 4 se puede representar por N 9 (N 3 (H 2) (H 4)) (H 7) El tipo de los \u00e1rboles binarios se puede definir por data Arbol a = H a | N a (Arbol a) (Arbol a) deriving (Show, Eq) Definir&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"footnotes":"","_jetpack_memberships_contains_paid_content":false},"categories":[581],"tags":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/7737"}],"collection":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/comments?post=7737"}],"version-history":[{"count":1,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/7737\/revisions"}],"predecessor-version":[{"id":7738,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/7737\/revisions\/7738"}],"wp:attachment":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/media?parent=7737"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/categories?post=7737"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/tags?post=7737"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}