{"id":3819,"date":"2018-03-01T06:00:00","date_gmt":"2018-03-01T04:00:00","guid":{"rendered":"http:\/\/www.glc.us.es\/~jalonso\/exercitium\/?p=3819"},"modified":"2020-03-30T06:57:39","modified_gmt":"2020-03-30T04:57:39","slug":"nodos-con-maxima-suma-de-hijos","status":"publish","type":"post","link":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/nodos-con-maxima-suma-de-hijos\/","title":{"rendered":"Nodos con m\u00e1xima suma de hijos"},"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               3      \n    \/ \\             \/|\\     \n   2   3           \/ | \\    \n       |          5  4  7   \n       4          |    \/|\\  \n                  6   2 8 6 \n<\/pre>\n<p>se representan por<\/p>\n<pre lang=\"text\">\n   ej1, ej2 :: Arbol Int\n   ej1 = N 1 [N 2 [], N 3 [N 4 []]]\n   ej2 = N 3 [N 5 [N 6 []], \n              N 4 [], \n              N 7 [N 2 [], N 8 [], N 6 []]]\n<\/pre>\n<p>Definir la funci\u00f3n<\/p>\n<pre lang=\"text\">\n   nodosSumaMaxima :: (Num t, Ord t) => Arbol t -> [t]\n<\/pre>\n<p>tal que (nodosSumaMaxima a) es la lista de los nodos del \u00e1rbol a cuyos hijos tienen m\u00e1xima suma. Por ejemplo,<\/p>\n<pre lang=\"text\">\n   nodosSumaMaxima ej1  ==  [1]\n   nodosSumaMaxima ej2  ==  [7,3]\n<\/pre>\n<h4>Soluciones<\/h4>\n<pre lang=\"haskell\">\nimport Data.List     (groupBy, sort)\nimport Data.Function (on)\n\ndata Arbol a = N a [Arbol a]\n  deriving Show\n\nej1, ej2 :: Arbol Int\nej1 = N 1 [N 2 [], N 3 [N 4 []]]\nej2 = N 3 [N 5 [N 6 []], \n           N 4 [], \n           N 7 [N 2 [], N 8 [], N 6 []]]\n\n-- 1\u00aa soluci\u00f3n\n-- ===========\n\nnodosSumaMaxima :: (Num t, Ord t) => Arbol t -> [t]\nnodosSumaMaxima a =\n  [x | (s,x) <- ns, s == m]\n  where ns = reverse (sort (nodosSumas a))\n        m  = fst (head ns)\n\n-- (nodosSumas x) es la lista de los pares (s,n) donde n es un nodo del\n-- \u00e1rbol x y s es la suma de sus hijos. Por ejemplo,  \n--    \u03bb> nodosSumas ej1\n--    [(5,1),(0,2),(4,3),(0,4)]\n--    \u03bb> nodosSumas ej2\n--    [(16,3),(6,5),(0,6),(0,4),(16,7),(0,2),(0,8),(0,6)]\nnodosSumas :: Num t => Arbol t -> [(t,t)]\nnodosSumas (N x []) = [(0,x)]\nnodosSumas (N x as) = (sum (raices as),x) : concatMap nodosSumas as\n\n-- (raices b) es la lista de las ra\u00edces del bosque b. Por ejemplo,\n--    raices [ej1,ej2]  ==  [1,3]\nraices :: [Arbol t] -> [t]\nraices = map raiz\n\n-- (raiz a) es la ra\u00edz del \u00e1rbol a. Por ejemplo,\n--    raiz ej1  ==  1\n--    raiz ej2  ==  3\nraiz :: Arbol t -> t\nraiz (N x _) = x\n\n-- 2\u00aa soluci\u00f3n\n-- ===========\n\nnodosSumaMaxima2 :: (Num t, Ord t) => Arbol t -> [t]\nnodosSumaMaxima2 a =\n  [x | (s,x) <- ns, s == m]\n  where ns = sort (nodosOpSumas a)\n        m  = fst (head ns)\n\n-- (nodosOpSumas x) es la lista de los pares (s,n) donde n es un nodo del\n-- \u00e1rbol x y s es el opuesto de la suma de sus hijos. Por ejemplo,  \n--    \u03bb> nodosOpSumas ej1\n--    [(-5,1),(0,2),(-4,3),(0,4)]\n--    \u03bb> nodosOpSumas ej2\n--    [(-16,3),(-6,5),(0,6),(0,4),(-16,7),(0,2),(0,8),(0,6)]\nnodosOpSumas :: Num t => Arbol t -> [(t,t)]\nnodosOpSumas (N x []) = [(0,x)]\nnodosOpSumas (N x as) = (-sum (raices as),x) : concatMap nodosOpSumas as\n\n\n-- 3\u00aa soluci\u00f3n\n-- ===========\n\nnodosSumaMaxima3 :: (Num t, Ord t) => Arbol t -> [t]\nnodosSumaMaxima3 a =\n  [x | (s,x) <- ns, s == m]\n  where ns = sort (nodosOpSumas a)\n        m  = fst (head ns)\n\n-- 4\u00aa soluci\u00f3n\n-- ===========\n\nnodosSumaMaxima4 :: (Num t, Ord t) => Arbol t -> [t]\nnodosSumaMaxima4 a =\n  map snd (head (groupBy (\\p q -> fst p == fst q)\n                         (sort (nodosOpSumas a))))\n\n-- 5\u00aa soluci\u00f3n\n-- ===========\n\nnodosSumaMaxima5 :: (Num t, Ord t) => Arbol t -> [t]\nnodosSumaMaxima5 a =\n  map snd (head (groupBy ((==) `on` fst)\n                         (sort (nodosOpSumas a))))\n\n-- 6\u00aa soluci\u00f3n\n-- ===========\n\nnodosSumaMaxima6 :: (Num t, Ord t) => Arbol t -> [t]\nnodosSumaMaxima6 =\n  map snd\n  . head\n  . groupBy ((==) `on` fst)\n  . sort\n  . nodosOpSumas\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 3 \/ \\ \/|\\ 2 3 \/ | \\ | 5 4 7 4 | \/|\\ 6 2 8 6 se representan por ej1, ej2 :: Arbol Int ej1&#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,161,8,58,160,80,440,71,10,11,6,32,14,159,40],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/3819"}],"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=3819"}],"version-history":[{"count":2,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/3819\/revisions"}],"predecessor-version":[{"id":3853,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/3819\/revisions\/3853"}],"wp:attachment":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/media?parent=3819"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/categories?post=3819"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/tags?post=3819"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}