{"id":7740,"date":"2022-12-23T06:00:41","date_gmt":"2022-12-23T04:00:41","guid":{"rendered":"http:\/\/www.glc.us.es\/~jalonso\/exercitium\/?p=7740"},"modified":"2022-12-25T11:04:34","modified_gmt":"2022-12-25T09:04:34","slug":"23-dic-22","status":"publish","type":"post","link":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/23-dic-22\/","title":{"rendered":"Suma de un \u00e1rbol"},"content":{"rendered":"<p>Los \u00e1rboles binarios con valores en los nodos se pueden definir por<\/p>\n<pre lang=\"text\">\n   data Arbol a = H\n                | N a (Arbol a) (Arbol a)\n     deriving (Show, Eq)\n<\/pre>\n<p>Por ejemplo, el \u00e1rbol<\/p>\n<pre lang=\"text\">\n        9\n       \/ \\\n      \/   \\\n     8     6\n    \/ \\   \/ \\\n   3   2 4   5\n<\/pre>\n<p>se puede representar por<\/p>\n<pre lang=\"text\">\n   N 9 (N 8 (N 3 H H) (N 2 H H)) (N 6 (N 4 H H) (N 5 H H))\n<\/pre>\n<p>Definir por recursi\u00f3n la funci\u00f3n<\/p>\n<pre lang=\"text\">\n   sumaArbol :: Num a => Arbol1 a -> a\n<\/pre>\n<p>tal <code>sumaArbol x<\/code> es la suma de los valores que hay en el \u00e1rbol <code>x<\/code>. Por ejemplo,<\/p>\n<pre lang=\"text\">\n   sumaArbol (N 2 (N 5 (N 3 H H) (N 7 H H)) (N 4 H H)) == 21\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 Arbol1 a = H\n              | N a (Arbol1 a) (Arbol1 a)\n  deriving (Show, Eq)\n\nsumaArbol :: Num a => Arbol1 a -> a\nsumaArbol H         = 0\nsumaArbol (N x i d) = x + sumaArbol i + sumaArbol d\n<\/pre>\n<p><a name=\"python\"><\/a><br \/>\n<b>Soluciones en Python<\/b><\/p>\n<pre lang=\"python\">\nfrom dataclasses import dataclass\n\n@dataclass\nclass Arbol:\n    pass\n\n@dataclass\nclass H(Arbol):\n    pass\n\n@dataclass\nclass N(Arbol):\n    x: int\n    i: Arbol\n    d: Arbol\n\ndef sumaArbol(a: Arbol) -> int:\n    match a:\n        case H():\n            return 0\n        case N(x, i, d):\n            return x + sumaArbol(i) + sumaArbol(d)\n    assert False\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Los \u00e1rboles binarios con valores en los nodos se pueden definir por data Arbol a = H | N a (Arbol a) (Arbol a) deriving (Show, Eq) Por ejemplo, el \u00e1rbol 9 \/ \\ \/ \\ 8 6 \/ \\ \/ \\ 3 2 4 5 se puede representar por N 9 (N 8 (N&#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\/7740"}],"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=7740"}],"version-history":[{"count":2,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/7740\/revisions"}],"predecessor-version":[{"id":7748,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/7740\/revisions\/7748"}],"wp:attachment":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/media?parent=7740"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/categories?post=7740"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/tags?post=7740"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}