{"id":1782,"date":"2015-12-01T06:00:44","date_gmt":"2015-12-01T04:00:44","guid":{"rendered":"http:\/\/www.glc.us.es\/~jalonso\/exercitium\/?p=1782"},"modified":"2015-12-08T07:31:21","modified_gmt":"2015-12-08T05:31:21","slug":"paridad-de-un-arbol","status":"publish","type":"post","link":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/paridad-de-un-arbol\/","title":{"rendered":"Paridad de un \u00e1rbol"},"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 Show\n<\/pre>\n<p>Por ejemplo, el \u00e1rbol<\/p>\n<pre lang=\"text\">\n        5         \n       \/ \\        \n      \/   \\       \n     9     7      \n    \/ \\   \/ \\     \n   1   4 6   8    \n<\/pre>\n<p>se puede representar por<\/p>\n<pre lang=\"text\">\n   N 5 (N 9 (H 1) (H 4)) (N 7 (H 6) (H 8))\n<\/pre>\n<p>Decimos que un \u00e1rbol binario es par si la mayor\u00eda de sus valores (en nodos u hojas) son pares e impar en caso contrario.<\/p>\n<p>Para representar la paridad se define el tipo Paridad<\/p>\n<pre lang=\"text\">\n   data Paridad = Par | Impar deriving (Eq, Show)  \n<\/pre>\n<p>Definir la funci\u00f3n<\/p>\n<pre lang=\"text\">\n   paridad :: Arbol3 Int -> Paridad\n<\/pre>\n<p>tal que (paridad a) es la paridad del \u00e1rbol a. Por ejemplo,<\/p>\n<pre lang=\"text\">\n   paridad (N 8 (N 6 (H 3) (H 4)) (H 5))  ==  Par\n   paridad (N 8 (N 9 (H 3) (H 4)) (H 5))  ==  Impar\n<\/pre>\n<h4>Soluciones<\/h4>\n<pre lang=\"haskell\">\ndata Arbol a = H a\n             | N a (Arbol a) (Arbol a) \n             deriving Show\n\ndata Paridad = Par | Impar deriving (Eq, Show)  \n \nparidad :: Arbol Int -> Paridad\nparidad a | x > y     = Par \n          | otherwise = Impar\n          where (x,y) = paridades a\n\n-- (paridades a) es un par (x,y) donde x es el n\u00famero de valores pares\n-- en el \u00e1rbol a e i es el n\u00famero de valores impares en el \u00e1rbol a. Por\n-- ejemplo,  \n--    paridades (N (N (H 3) 6 (H 4)) 8 (H 5))  ==  (3,2)\n--    paridades (N (N (H 3) 9 (H 4)) 8 (H 5))  ==  (2,3)\nparidades :: Arbol Int -> (Int,Int)\nparidades (H x) | even x    = (1,0)\n                | otherwise = (0,1)\nparidades (N x i d) | even x    = (1+a1+a2,b1+b2)\n                    | otherwise = (a1+a2,1+b1+b2)\n                    where (a1,b1) = paridades i\n                          (a2,b2) = paridades d          \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 Show Por ejemplo, el \u00e1rbol 5 \/ \\ \/ \\ 9 7 \/ \\ \/ \\ 1 4 6 8 se puede representar por N 5&#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,91,6],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/1782"}],"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=1782"}],"version-history":[{"count":4,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/1782\/revisions"}],"predecessor-version":[{"id":1832,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/1782\/revisions\/1832"}],"wp:attachment":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/media?parent=1782"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/categories?post=1782"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/tags?post=1782"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}