{"id":2825,"date":"2017-01-18T06:00:11","date_gmt":"2017-01-18T04:00:11","guid":{"rendered":"http:\/\/www.glc.us.es\/~jalonso\/exercitium\/?p=2825"},"modified":"2022-03-25T20:37:35","modified_gmt":"2022-03-25T18:37:35","slug":"sustitucion-en-una-posicion","status":"publish","type":"post","link":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/sustitucion-en-una-posicion\/","title":{"rendered":"Sustituci\u00f3n en una posici\u00f3n"},"content":{"rendered":"<p>Los \u00e1rboles binarios se pueden representar con el de dato algebraico<\/p>\n<pre lang=\"text\">\n   data Arbol a = H\n                | N a (Arbol a) (Arbol a)\n                deriving Show\n<\/pre>\n<p>Por ejemplo, los \u00e1rboles<\/p>\n<pre lang=\"text\">\n        9                9                \n       \/ \\              \/ \n      \/   \\            \/  \n     8     6          8  \n    \/ \\   \/ \\        \/ \\ \n   3   2 4   5      3   2 \n<\/pre>\n<p>se pueden representar por<\/p>\n<pre lang=\"text\">\n   ej1, ej2:: Arbol Int\n   ej1 = N 9 (N 8 (N 3 H H) (N 2 H H)) (N 6 (N 4 H H) (N 5 H H))\n   ej2 = N 9 (N 8 (N 3 H H) (N 2 H H)) H\n<\/pre>\n<p>Para indicar las posiciones del \u00e1rbol se define el tipo<\/p>\n<pre lang=\"text\">\n  type Posicion = [Direccion]\n<\/pre>\n<p>donde<\/p>\n<pre lang=\"text\">\n  data Direccion = D | I\n    deriving Eq\n<\/pre>\n<p>representa un movimiento hacia la derecha (D) o a la izquierda. Por ejemplo, las posiciones de los elementos del ej1 son<\/p>\n<pre lang=\"text\">\n  9 [] \n  8 [I]\n  3 [I,I]\n  2 [I,D]\n  6 [D]\n  4 [D,I]\n  5 [D,D]\n<\/pre>\n<p>Definir la funci\u00f3n<\/p>\n<pre lang=\"text\">\n   sustitucion :: Posicion -> a -> Arbol a -> Arbol a\n<\/pre>\n<p>tal que (sustitucion ds z x) es el \u00e1rbol obtenido sustituyendo el elemento de x en la posici\u00f3n ds por z. Por ejemplo,<\/p>\n<pre lang=\"text\">\n  \u03bb> sustitucion [I,D] 7 ej1\n  N 9 (N 8 (N 3 H H) (N 7 H H)) (N 6 (N 4 H H) (N 5 H H))\n  \u03bb> sustitucion [D,D] 7 ej1\n  N 9 (N 8 (N 3 H H) (N 2 H H)) (N 6 (N 4 H H) (N 7 H H))\n  \u03bb> sustitucion [I] 7 ej1\n  N 9 (N 7 (N 3 H H) (N 2 H H)) (N 6 (N 4 H H) (N 5 H H))\n  \u03bb> sustitucion [] 7 ej1\n  N 7 (N 8 (N 3 H H) (N 2 H H)) (N 6 (N 4 H H) (N 5 H H))\n<\/pre>\n<h4>Soluciones<\/h4>\n<pre lang=\"haskell\">\ndata Arbol a = H | N a (Arbol a) (Arbol a)\n  deriving (Eq, Show)\n\nej1, ej2:: Arbol Int\nej1 = N 9 (N 8 (N 3 H H) (N 2 H H)) (N 6 (N 4 H H) (N 5 H H))\nej2 = N 9 (N 8 (N 3 H H) (N 2 H H)) H\n\ndata Direccion = D | I\n  deriving Eq\n\ntype Posicion = [Direccion]\n\nsustitucion :: Posicion  -> a -> Arbol a -> Arbol a\nsustitucion (I:ds) z (N x i d) = N x (sustitucion ds z i) d\nsustitucion (D:ds) z (N x i d) = N x i (sustitucion ds z d)\nsustitucion []     z (N _ i d) = N z i d\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Los \u00e1rboles binarios se pueden representar con el de dato algebraico data Arbol a = H | N a (Arbol a) (Arbol a) deriving Show Por ejemplo, los \u00e1rboles 9 9 \/ \\ \/ \/ \\ \/ 8 6 8 \/ \\ \/ \\ \/ \\ 3 2 4 5 3 2 se pueden representar&#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,133],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/2825"}],"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=2825"}],"version-history":[{"count":4,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/2825\/revisions"}],"predecessor-version":[{"id":2864,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/2825\/revisions\/2864"}],"wp:attachment":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/media?parent=2825"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/categories?post=2825"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/tags?post=2825"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}