{"id":2107,"date":"2016-02-12T06:00:10","date_gmt":"2016-02-12T04:00:10","guid":{"rendered":"http:\/\/www.glc.us.es\/~jalonso\/exercitium\/?p=2107"},"modified":"2016-02-19T07:46:38","modified_gmt":"2016-02-19T05:46:38","slug":"anotacion-de-la-profundidad-los-nodos","status":"publish","type":"post","link":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/anotacion-de-la-profundidad-los-nodos\/","title":{"rendered":"Anotaci\u00f3n de la profundidad de los nodos"},"content":{"rendered":"<p>Los \u00e1rboles binarios con datos en los nodos y hojas se definen por<\/p>\n<pre lang=\"text\">\n   data Arbol a = H \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          3\n         \/ \\\n        \/   \\\n       4     7\n      \/ \\   \/ \\\n     5   0 0   3\n    \/ \\\n   2   0   \n<\/pre>\n<p>se representa por<\/p>\n<pre lang=\"text\">\n   ejArbol :: Arbol Integer\n   ejArbol = N 3 (N 4 (N 5 (H 2)(H 0)) (H 0)) (N 7 (H 0) (H 3))\n<\/pre>\n<p>Anotando cada elemento del \u00e1rbol anterior con su profundidad, se obtiene el \u00e1rbol siguiente<\/p>\n<pre lang=\"text\">\n          3-0\n          \/ \\\n         \/   \\\n        \/     \\\n      4-1     7-1\n      \/ \\     \/ \\\n    5-2 0-2 0-2 3-0\n    \/ \\\n  2-3 0-3   \n<\/pre>\n<p>Definir la funci\u00f3n<\/p>\n<pre lang=\"text\">\n   anotado :: Arbol a -> Arbol (a,Int)\n<\/pre>\n<p>tal que (anotado x) es el \u00e1rbol obtenido anotando los elementos de x con su profundidad. Por ejemplo,<\/p>\n<pre lang=\"text\">\n   \u03bb> anotado ejArbol\n   N (3,0) \n     (N (4,1) \n        (N (5,2) (H (2,3)) (H (0,3))) \n        (H (0,2))) \n     (N (7,1) (H (0,2)) (H (3,2)))\n<\/pre>\n<h4>Soluciones<\/h4>\n<pre lang=\"haskell\">\ndata Arbol a = H a \n             | N a (Arbol a) (Arbol a) \n             deriving (Eq, Show)\n\nejArbol :: Arbol Integer\nejArbol = N 3 (N 4 (N 5 (H 2)(H 0)) (H 0)) (N 7 (H 0) (H 3))\n\n-- 1\u00aa soluci\u00f3n\n-- ===========\n\nanotado1 :: Arbol a -> Arbol (a,Int)\nanotado1 (H x)     = H (x,0)\nanotado1 (N x i d) = aux (N x i d) 0\n    where aux (H x)     n = H (x,n)\n          aux (N x i d) n = N (x,n) (aux i (n+1)) (aux d (n+1))  \n\n-- 2\u00aa soluci\u00f3n\nanotado2 :: Arbol a -> Arbol (a, Int)\nanotado2 a = aux a [0..]\n    where aux (H a)     (n:_ ) = H (a,n)\n          aux (N a i d) (n:ns) = N (a,n) (aux i ns) (aux d ns)\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Los \u00e1rboles binarios con datos en los nodos y hojas se definen por data Arbol a = H | N a (Arbol a) (Arbol a) deriving (Eq, Show) Por ejemplo, el \u00e1rbol 3 \/ \\ \/ \\ 4 7 \/ \\ \/ \\ 5 0 0 3 \/ \\ 2 0 se representa 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":[4],"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\/2107"}],"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=2107"}],"version-history":[{"count":8,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/2107\/revisions"}],"predecessor-version":[{"id":2148,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/2107\/revisions\/2148"}],"wp:attachment":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/media?parent=2107"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/categories?post=2107"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/tags?post=2107"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}