{"id":4634,"date":"2019-01-25T06:00:47","date_gmt":"2019-01-25T04:00:47","guid":{"rendered":"http:\/\/www.glc.us.es\/~jalonso\/exercitium\/?p=4634"},"modified":"2019-02-01T11:36:51","modified_gmt":"2019-02-01T09:36:51","slug":"recorrido-de-arboles-en-espiral","status":"publish","type":"post","link":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/recorrido-de-arboles-en-espiral\/","title":{"rendered":"Recorrido de \u00e1rboles en espiral"},"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\n      2      3          \/|\\       \/|\\  |\n     \/ \\    \/ \\        4 5 6     4 5 6 7\n    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 2 [N 4 [], N 5 []], N 3 [N 6 [], N 7 []]]\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>Definir la funci\u00f3n<\/p>\n<pre lang=\"text\">\n   espiral :: Arbol a -> [a]\n<\/pre>\n<p>tal que (espiral x) es la lista de los nodos del \u00e1rbol x recorridos en espiral; es decir, la ra\u00edz de x, los nodos del primer nivel de izquierda a derecha, los nodos del segundo nivel de derecha a izquierda y as\u00ed sucesivamente. Por ejemplo,<\/p>\n<pre lang=\"text\">\n   espiral ej1  ==  [1,2,3,7,6,5,4]\n   espiral ej2  ==  [1,8,3,6,5,4]\n   espiral ej3  ==  [1,8,3,7,6,5,4]\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 2 [N 4 [], N 5 []], N 3 [N 6 [], N 7 []]]\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\n-- 1\u00aa soluci\u00f3n\n-- ===========\n\nespiral :: Arbol a -> [a]\nespiral x =\n  concat [f xs | (f,xs) <- zip (cycle [reverse,id]) (niveles x)]\n\n-- (niveles x) es la lista de los niveles del \u00e1rbol x. Por ejemplo, \n--    niveles ej1 == [[1],[8,3],[4]]\n--    niveles ej2 == [[1],[8,3],[4,5,6]]\n--    niveles ej3 == [[1],[8,3],[4,5,6,7]]\nniveles :: Arbol a -> [[a]]\nniveles x = takeWhile (not . null) [nivel n x | n <- [0..]]\n\n-- (nivel n x) es el nivel de nivel n del \u00e1rbol x. Por ejemplo,\n--    nivel 0 ej1  ==  [1]\n--    nivel 1 ej1  ==  [8,3]\n--    nivel 2 ej1  ==  [4]\n--    nivel 4 ej1  ==  []\nnivel :: Int -> Arbol a ->  [a]\nnivel 0 (N x _)  = [x]\nnivel n (N _ xs) = concatMap (nivel (n-1)) xs\n\n-- 2\u00aa soluci\u00f3n\n-- ===========\n\nespiral2 :: Arbol a -> [a]\nespiral2 = \n  concat . zipWith ($) (cycle [reverse,id]) . niveles\n\n-- 3\u00aa soluci\u00f3n\n-- ===========\n\nespiral3 :: Arbol a -> [a]\nespiral3 = concat . zipWith ($) (cycle [reverse,id]) . niveles3\n\nniveles3 :: Arbol a -> [[a]]\nniveles3 t = map (map raiz)\n           . takeWhile (not . null)\n           . iterate (concatMap subBosque) $ [t]\n\nraiz :: Arbol a -> a\nraiz (N x _) = x\n\nsubBosque :: Arbol a -> [Arbol a]\nsubBosque (N _ ts) = ts\n\n-- 4\u00aa soluci\u00f3n\n-- ===========\n\nespiral4 :: Arbol a -> [a]\nespiral4 = concat . zipWith ($) (cycle [reverse,id]) . niveles4\n\nniveles4 :: Arbol a -> [[a]]\nniveles4 = map (map raiz)\n         . takeWhile (not . null)\n         . iterate (concatMap subBosque)\n         . return  \n\n-- 5\u00aa definici\u00f3n\n-- =============\n\nespiral5 :: Arbol a -> [a]\nespiral5 x = concat $ zipWith ($) (cycle [reverse,id]) $ niveles5 [x]\n\nniveles5 :: [Arbol a] -> [[a]]\nniveles5 [] = []\nniveles5 xs = a : niveles5 (concat b)\n  where (a,b) = unzip $ map (\\(N x y) -> (x,y)) xs\n\n-- 6\u00aa definici\u00f3n\n-- =============\n\nespiral6 :: Arbol a -> [a]\nespiral6 = concat . zipWith ($) (cycle [reverse,id]) . niveles5 . return\n<\/pre>\n<h4>Pensamiento<\/h4>\n<blockquote><p>\nDice la monoton\u00eda<br \/>\ndel agua clara al caer:<br \/>\nun d\u00eda es como otro d\u00eda;<br \/>\nhoy es lo mismo que ayer.<\/p>\n<p>Antonio Machado\n<\/p><\/blockquote>\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 2 3 \/|\\ \/|\\ | \/ \\ \/ \\ 4 5 6 4 5 6&#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":[8,12,58,166,63,181,141,11,6,32,34,9],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/4634"}],"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=4634"}],"version-history":[{"count":3,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/4634\/revisions"}],"predecessor-version":[{"id":4675,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/4634\/revisions\/4675"}],"wp:attachment":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/media?parent=4634"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/categories?post=4634"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/tags?post=4634"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}