{"id":3538,"date":"2017-12-20T06:00:23","date_gmt":"2017-12-20T04:00:23","guid":{"rendered":"http:\/\/www.glc.us.es\/~jalonso\/exercitium\/?p=3538"},"modified":"2017-12-27T08:30:42","modified_gmt":"2017-12-27T06:30:42","slug":"suma-de-las-hojas-de-minimo-nivel","status":"publish","type":"post","link":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/suma-de-las-hojas-de-minimo-nivel\/","title":{"rendered":"Suma de las hojas de m\u00ednimo nivel"},"content":{"rendered":"<p>Los \u00e1rboles binarios con valores en las hojas y en los nodos se definen por<\/p>\n<pre lang=\"text\">\n   data Arbol a = H a\n                | N a (Arbol a) (Arbol a) \n     deriving (Eq, Show)\n<\/pre>\n<p>Por ejemplo, el \u00e1rbol<\/p>\n<pre lang=\"text\">\n         1 \n       \/   \\\n      2     3\n     \/ \\   \/ \\\n    4   5 6   7\n       \/ \\\n      8   9 \n<\/pre>\n<p>se pueden representar por<\/p>\n<pre lang=\"text\">\n   ejArbol :: Arbol Int\n   ejArbol = N 1 (N 2 (H 4)\n                      (N 5 (H 8) (H 9)))\n                 (N 3 (H 6) (H 7))\n<\/pre>\n<p>En el \u00e1rbol anterior, los valores de las hojas de menor nivel son 4, 6 y 7 cuya suma es 17.<\/p>\n<p>Definir la funci\u00f3n<\/p>\n<pre lang=\"text\">\n   suma :: Num t => Arbol t -> t\n<\/pre>\n<p>tal que (suma a) es la suma de los valores de las hojas de menor nivel del \u00e1rbol a. Por ejemplo,<\/p>\n<pre lang=\"text\">\n   suma ejArbol                                    ==  17\n   suma (N 1 (N 2 (H 4) (H 5)) (N 3 (H 6) (H 7)))  ==  22\n   suma (N 1 (H 2) (N 3 (H 6) (H 7)))              ==  2\n   suma (N 1 (H 2) (H 3))                          ==  5\n   suma (H 2)                                      ==  2\n<\/pre>\n<h4>Soluciones<\/h4>\n<pre lang=\"haskell\">\ndata Arbol a = H a\n             | N a (Arbol a) (Arbol a) \n  deriving (Eq, Show)\n\nejArbol :: Arbol Int\nejArbol = N 1 (N 2 (H 4)\n                   (N 5 (H 8) (H 9)))\n              (N 3 (H 6) (H 7))\n                      \nsuma :: Num t => Arbol t -> t\nsuma a = aux [a]\n  where aux as | null xs   = aux (concat [[i,d] | (N _ i d) <- as])\n               | otherwise = sum xs\n          where xs = [x | (H x) <- as]\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Los \u00e1rboles binarios con valores en las hojas y en los nodos se definen por data Arbol a = H a | N a (Arbol a) (Arbol a) deriving (Eq, Show) Por ejemplo, el \u00e1rbol 1 \/ \\ 2 3 \/ \\ \/ \\ 4 5 6 7 \/ \\ 8 9 se pueden representar&#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":[7],"tags":[269,12,141,6,40],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/3538"}],"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=3538"}],"version-history":[{"count":2,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/3538\/revisions"}],"predecessor-version":[{"id":3572,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/3538\/revisions\/3572"}],"wp:attachment":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/media?parent=3538"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/categories?post=3538"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/tags?post=3538"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}