{"id":7735,"date":"2022-12-21T06:00:35","date_gmt":"2022-12-21T04:00:35","guid":{"rendered":"http:\/\/www.glc.us.es\/~jalonso\/exercitium\/?p=7735"},"modified":"2022-12-20T12:22:34","modified_gmt":"2022-12-20T10:22:34","slug":"21-dic-22","status":"publish","type":"post","link":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/21-dic-22\/","title":{"rendered":"Sub\u00e1rbol de profundidad dada"},"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>La funci\u00f3n take est\u00e1 definida por<\/p>\n<pre lang=\"text\">\n   take :: Int -> [a] -> [a]\n   take 0            = []\n   take (n+1) []     = []\n   take (n+1) (x:xs) = x : take n xs\n<\/pre>\n<p>Definir la funci\u00f3n<\/p>\n<pre lang=\"text\">\n   takeArbol ::  Int -> Arbol a -> Arbol a\n<\/pre>\n<p>tal que <code>takeArbol n t<\/code> es el sub\u00e1rbol de <code>t<\/code> de profundidad <code>n<\/code>. Por ejemplo,<\/p>\n<pre lang=\"text\">\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)\n<\/pre>\n<p>Comprobar con QuickCheck que la profundidad de <code>takeArbol n x<\/code> es menor o igual que <code>n<\/code>, para todo n\u00famero natural <code>n<\/code> y todo \u00e1rbol <code>x<\/code>.<\/p>\n<p><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\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-- (profundidad x) es la profundidad del \u00e1rbol x. Por ejemplo,\n--    profundidad (N 9 (N 3 (H 2) (H 4)) (H 7))              ==  2\n--    profundidad (N 9 (N 3 (H 2) (N 1 (H 4) (H 5))) (H 7))  ==  3\n--    profundidad (N 4 (N 5 (H 4) (H 2)) (N 3 (H 7) (H 4)))  ==  2\nprofundidad :: Arbol a -> Int\nprofundidad (H _)     = 0\nprofundidad (N _ i d) = 1 + max (profundidad i) (profundidad d)\n\n-- Comprobaci\u00f3n de la propiedad\n-- ============================\n\n-- La propiedad es\nprop_takeArbol :: Int -> Arbol Int -> Property\nprop_takeArbol n x =\n  n >= 0 ==> profundidad (takeArbol n x) <= n\n\n-- La comprobaci\u00f3n es\n--    \u03bb> quickCheck prop_takeArbol\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 takeArbol(n: int, a: Arbol[A]) -> Arbol[A]:\n    match (n, a):\n        case (_, H(x)):\n            return H(x)\n        case (0, N(x, _, _)):\n            return H(x)\n        case (n, N(x, i, d)):\n            return N(x, takeArbol(n - 1, i), takeArbol(n - 1, d))\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# profundidad(x) es la profundidad del \u00e1rbol x. Por ejemplo,\n#    profundidad(N(9, N(3, H(2), H(4)), H(7)))              ==  2\n#    profundidad(N(9, N(3, H(2), N(1, H(4), H(5))), H(7)))  ==  3\n#    profundidad(N(4, N(5, H(4), H(2)), N(3, H(7), H(4))))  ==  2\ndef profundidad(a: Arbol[A]) -> int:\n    match a:\n        case H(_):\n            return 0\n        case N(_, i, d):\n            return 1 + max(profundidad(i), profundidad(d))\n    assert False\n\n# Comprobaci\u00f3n de la propiedad\n# ============================\n\n# La propiedad es\n@given(st.integers(min_value=0, max_value=12),\n       st.integers(min_value=1, max_value=10))\ndef test_takeArbol(n: int, m: int) -> None:\n    x = arbolArbitrario(m)\n    assert profundidad(takeArbol(n, x)) <= n\n\n# La comprobaci\u00f3n es\n#    src> poetry run pytest -q subarbol_de_profundidad_dada.py\n#    1 passed in 0.23s\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) La&#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\/7735"}],"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=7735"}],"version-history":[{"count":1,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/7735\/revisions"}],"predecessor-version":[{"id":7736,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/7735\/revisions\/7736"}],"wp:attachment":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/media?parent=7735"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/categories?post=7735"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/tags?post=7735"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}