{"id":2771,"date":"2017-01-03T06:00:20","date_gmt":"2017-01-03T04:00:20","guid":{"rendered":"http:\/\/www.glc.us.es\/~jalonso\/exercitium\/?p=2771"},"modified":"2017-01-10T08:45:28","modified_gmt":"2017-01-10T06:45:28","slug":"estratificacion-de-un-arbol","status":"publish","type":"post","link":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/estratificacion-de-un-arbol\/","title":{"rendered":"Estratificaci\u00f3n 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     8   3         8   3  \n       |        \/|\\       \/|\\  |   \n       4       4 5 6     4 5 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 8 [], N 3 [N 4 [], N 5 [], N 6 []]]\n   ej3 = N 1 [N 8 [N 4 [], N 5 [], N 6 []], N 3 [N 7 []]]\n<\/pre>\n<p>Un estrato de un \u00e1rbol es la lista de nodos que se encuentran al mismo nivel de profundidad. Por ejemplo, los estratos del \u00e1rbol ej1 son [1], [8,3] y [4].<\/p>\n<p>Definir la funci\u00f3n<\/p>\n<pre lang=\"text\">\n   estratos :: Arbol a -> [[a]]\n<\/pre>\n<p>tal que (estratos x) es la lista de los estratos del \u00e1rbol x. Por ejemplo,<\/p>\n<pre lang=\"text\">\n   estratos ej1 == [[1],[8,3],[4]]\n   estratos ej2 == [[1],[8,3],[4,5,6]]\n   estratos ej3 == [[1],[8,3],[4,5,6,7]]\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 8 [], N 3 [N 4 [], N 5 [], N 6 []]]\nej3 = N 1 [N 8 [N 4 [], N 5 [], N 6 []], N 3 [N 7 []]]\n\nestratos :: Arbol a -> [[a]]\nestratos x = takeWhile (not . null) [estrato n x | n <- [0..]]\n\n-- (estrato n x) es el estrato de nivel n del \u00e1rbol x. Por ejemplo,\n--    estrato 0 ej1  ==  [1]\n--    estrato 1 ej1  ==  [8,3]\n--    estrato 2 ej1  ==  [4]\n--    estrato 4 ej1  ==  []\nestrato :: Int -> Arbol a ->  [a]\nestrato 0 (N x _)  = [x]\nestrato n (N _ xs) = concatMap (estrato (n-1)) xs\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 8 3 8 3 | \/|\\ \/|\\ | 4 4 5 6 4 5 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":[8,58,181,141,11,6,34],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/2771"}],"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=2771"}],"version-history":[{"count":3,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/2771\/revisions"}],"predecessor-version":[{"id":2809,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/2771\/revisions\/2809"}],"wp:attachment":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/media?parent=2771"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/categories?post=2771"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/tags?post=2771"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}