{"id":3671,"date":"2018-01-26T06:00:34","date_gmt":"2018-01-26T04:00:34","guid":{"rendered":"http:\/\/www.glc.us.es\/~jalonso\/exercitium\/?p=3671"},"modified":"2018-02-02T09:12:01","modified_gmt":"2018-02-02T07:12:01","slug":"relacion-definida-por-un-arbol","status":"publish","type":"post","link":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/relacion-definida-por-un-arbol\/","title":{"rendered":"Relaci\u00f3n definida por 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 (Eq, Show)\n<\/pre>\n<p>Por ejemplo, el \u00e1rbol<\/p>\n<pre lang=\"text\">\n         10\n        \/  \\\n       \/    \\\n      8      2\n     \/ \\    \/ \\\n    3   5  2   0\n<\/pre>\n<p>se pueden representar por<\/p>\n<pre lang=\"text\">\n   ejArbol :: Arbol Int\n   ejArbol = N 10 (N 8 (H 3) (H 5))\n                  (N 2 (H 2) (H 0))\n<\/pre>\n<p>Un \u00e1rbol binario define una relaci\u00f3n binaria donde un elemento x est\u00e1 relacionado con y si x es el padre de y. Por ejemplo, la relaci\u00f3n definida por el \u00e1rbol anterior es [(10,8),(8,3),(8,5),(10,2),(2,2),(2,0)].<\/p>\n<p>Definir la funci\u00f3n<\/p>\n<pre lang=\"text\">\n   relacionDelArbol :: Arbol a -> [(a,a)]\n<\/pre>\n<p>tal que (relacionDelArbol a) es la relaci\u00f3n binaria definida por el \u00e1rbol a. Por ejemplo,<\/p>\n<pre lang=\"text\">\n   \u03bb> relacionDelArbol ejArbol\n   [(10,8),(8,3),(8,5),(10,2),(2,2),(2,0)]\n   \u03bb> relacionDelArbol (N 10 (H 8) (N 2 (H 2) (H 0)))\n   [(10,8),(10,2),(2,2),(2,0)]\n   \u03bb> relacionDelArbol (N 10 (N 8 (H 3) (H 5)) (H 2))\n   [(10,8),(8,3),(8,5),(10,2)]\n   \u03bb> relacionDelArbol (H 10)\n   []\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\nejArbol :: Arbol Int\nejArbol = N 10 (N 8 (H 3) (H 5))\n               (N 2 (H 2) (H 0))\n\nrelacionDelArbol :: Arbol a -> [(a,a)]\nrelacionDelArbol (H _)     = []\nrelacionDelArbol (N x i d) = ((x,raiz i) : relacionDelArbol i) ++ \n                             ((x,raiz d) : relacionDelArbol d)\n                             \nraiz :: Arbol a -> a\nraiz (H x)     = x\nraiz (N x _ _) = x\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 (Eq, Show) Por ejemplo, el \u00e1rbol 10 \/ \\ \/ \\ 8 2 \/ \\ \/ \\ 3 5 2 0 se pueden representar por ejArbol&#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":[5],"tags":[269,6],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/3671"}],"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=3671"}],"version-history":[{"count":3,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/3671\/revisions"}],"predecessor-version":[{"id":3695,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/3671\/revisions\/3695"}],"wp:attachment":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/media?parent=3671"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/categories?post=3671"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/tags?post=3671"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}