{"id":7563,"date":"2022-12-09T06:00:24","date_gmt":"2022-12-09T04:00:24","guid":{"rendered":"http:\/\/www.glc.us.es\/~jalonso\/exercitium\/?p=7563"},"modified":"2022-12-14T10:17:04","modified_gmt":"2022-12-14T08:17:04","slug":"09-dic-22","status":"publish","type":"post","link":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/09-dic-22\/","title":{"rendered":"\u00c1rbol con las hojas en la profundidad dada"},"content":{"rendered":"<p>El \u00e1rbol binario<\/p>\n<pre lang=\"text\">\n        \u00b7\n       \/ \\\n      \/   \\\n     \u00b7     \u00b7\n    \/ \\   \/ \\\n   1   4 6   9\n<\/pre>\n<p>se puede representar por<\/p>\n<pre lang=\"text\">\n   ejArbol = Nodo (Nodo (Hoja 1) (Hoja 4))\n                  (Nodo (Hoja 6) (Hoja 9))\n<\/pre>\n<p>El tipo de los \u00e1rboles binarios se puede definir por<\/p>\n<pre lang=\"text\">\n   data Arbol a = Hoja a\n                | Nodo (Arbol a) (Arbol a)\n     deriving (Show, Eq)\n<\/pre>\n<p>Definir la funci\u00f3n<\/p>\n<pre lang=\"text\">\n   creaArbol :: Int -> Arbol ()\n<\/pre>\n<p>tal que <code>creaArbol n<\/code> es el \u00e1rbol cuyas hoyas est\u00e1n en la profundidad <code>n<\/code>. Por ejemplo,<\/p>\n<pre lang=\"text\">\n   \u03bb> creaArbol 2\n   Nodo (Nodo (Hoja ()) (Hoja ())) (Nodo (Hoja ()) (Hoja ()))\n<\/pre>\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\">\ndata Arbol a = Hoja a\n             | Nodo (Arbol a) (Arbol a)\n  deriving (Show, Eq)\n\ncreaArbol :: Int -> Arbol ()\ncreaArbol h\n  | h <= 0    = Hoja ()\n  | otherwise = Nodo x x\n  where x = creaArbol (h - 1)\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 typing import Any, Generic, TypeVar\n\nA = TypeVar(\"A\")\n\n@dataclass\nclass Arbol(Generic[A]):\n    pass\n\n@dataclass\nclass Hoja(Arbol[A]):\n    x: A\n\n@dataclass\nclass Nodo(Arbol[A]):\n    i: Arbol[A]\n    d: Arbol[A]\n\ndef creaArbol(h: int) -> Arbol[Any]:\n    if h <= 0:\n        return Hoja(None)\n    x = creaArbol(h - 1)\n    return Nodo(x, x)\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>El \u00e1rbol binario \u00b7 \/ \\ \/ \\ \u00b7 \u00b7 \/ \\ \/ \\ 1 4 6 9 se puede representar por ejArbol = Nodo (Nodo (Hoja 1) (Hoja 4)) (Nodo (Hoja 6) (Hoja 9)) El tipo de los \u00e1rboles binarios se puede definir por data Arbol a = Hoja a | Nodo (Arbol a)&#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":[269],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/7563"}],"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=7563"}],"version-history":[{"count":4,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/7563\/revisions"}],"predecessor-version":[{"id":7630,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/7563\/revisions\/7630"}],"wp:attachment":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/media?parent=7563"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/categories?post=7563"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/tags?post=7563"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}