{"id":2748,"date":"2016-12-28T06:00:52","date_gmt":"2016-12-28T04:00:52","guid":{"rendered":"http:\/\/www.glc.us.es\/~jalonso\/exercitium\/?p=2748"},"modified":"2022-03-25T20:37:50","modified_gmt":"2022-03-25T18:37:50","slug":"minima-suma-de-las-ramas-de-un-arbol","status":"publish","type":"post","link":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/minima-suma-de-las-ramas-de-un-arbol\/","title":{"rendered":"M\u00ednima suma de las ramas de un \u00e1rbol"},"content":{"rendered":"<p>Los \u00e1rboles se pueden representar mediante el siguiente tipo de datos<\/p>\n<pre lang=\"text\">\n   data Arbol a = N a [Arbol a]\n     deriving Show\n<\/pre>\n<p>Por ejemplo, los \u00e1rboles<\/p>\n<pre lang=\"text\">\n     1         1             1          \n    \/ \\       \/ \\           \/ \\   \n   8   3     5   3         5   3  \n       |        \/|\\       \/|\\  |   \n       4       4 7 6     4 7 6 7\n<\/pre>\n<p>se representan por<\/p>\n<pre lang=\"text\">\n   ej1, ej2, ej3 :: Arbol Int\n   ej1 = N 1 [N 8 [],N 3 [N 4 []]]\n   ej2 = N 1 [N 5 [], N 3 [N 4 [], N 7 [], N 6 []]]\n   ej3 = N 1 [N 5 [N 4 [], N 7 [], N 6 []], N 3 [N 7 []]] \n<\/pre>\n<p>Definir la funci\u00f3n<\/p>\n<pre lang=\"text\">\n   minimaSuma :: Arbol Int -> Int\n<\/pre>\n<p>tal que (minimaSuma a) es el m\u00ednimo de las sumas de las ramas del \u00e1rbol a. Por ejemplo,<\/p>\n<pre lang=\"text\">\n   minimaSuma ej1  ==  8\n   minimaSuma ej2  ==  6\n   minimaSuma ej3  ==  10\n<\/pre>\n<h4>Soluciones<\/h4>\n<pre lang=\"haskell\">\ndata Arbol a = N a [Arbol a]\n  deriving Show\n\nej1, ej2, ej3 :: Arbol Int\nej1 = N 1 [N 8 [],N 3 [N 4 []]]\nej2 = N 1 [N 5 [], N 3 [N 4 [], N 7 [], N 6 []]]\nej3 = N 1 [N 5 [N 4 [], N 7 [], N 6 []], N 3 [N 7 []]] \n\n\n-- 1\u00aa definici\u00f3n\n-- =============\nminimaSuma :: (Ord a, Num a) => Arbol a -> a\nminimaSuma a = minimum [sum xs | xs <- ramas a]\n\n-- (ramas a) es la lista de las ramas del \u00e1rbol a. Por ejemplo,\n--    ghci> ramas (N 3 [N 5 [N 6 []], N 4 [], N 7 [N 2 [], N 1 []]])\n--    [[3,5,6],[3,4],[3,7,2],[3,7,1]]\nramas :: Arbol b -> [[b]]\nramas (N x []) = [[x]]\nramas (N x as) = [x : xs | a <- as, xs <- ramas a]\n\n-- 2\u00aa definici\u00f3n\n-- =============\n  \nminimaSuma2 :: Arbol Int -> Int\nminimaSuma2 (N x []) = x\nminimaSuma2 (N x as) = x + minimum (map minimaSuma2 as)\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Los \u00e1rboles se pueden representar mediante el siguiente tipo de datos data Arbol a = N a [Arbol a] deriving Show Por ejemplo, los \u00e1rboles 1 1 1 \/ \\ \/ \\ \/ \\ 8 3 5 3 5 3 | \/|\\ \/|\\ | 4 4 7 6 4 7 6 7 se representan por&#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":[4],"tags":[269,8,10,340,11,6,133],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/2748"}],"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=2748"}],"version-history":[{"count":3,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/2748\/revisions"}],"predecessor-version":[{"id":2783,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/2748\/revisions\/2783"}],"wp:attachment":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/media?parent=2748"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/categories?post=2748"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/tags?post=2748"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}