{"id":7757,"date":"2022-12-30T06:00:00","date_gmt":"2022-12-30T04:00:00","guid":{"rendered":"http:\/\/www.glc.us.es\/~jalonso\/exercitium\/?p=7757"},"modified":"2022-12-27T10:09:54","modified_gmt":"2022-12-27T08:09:54","slug":"30-dic-22","status":"publish","type":"post","link":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/30-dic-22\/","title":{"rendered":"Existencia de elementos del \u00e1rbol que verifican una propiedad"},"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     3     2\n    \/ \\\n   1   4\n<\/pre>\n<p>se representa por<\/p>\n<pre lang=\"text\">\n   N 5 (N 3 (H 1) (H 4)) (H 2)\n<\/pre>\n<p>Definir la funci\u00f3n<\/p>\n<pre lang=\"text\">\n   algunoArbol :: Arbol t -> (t -> Bool) -> Bool\n<\/pre>\n<p>tal que <code>algunoArbol a p<\/code> se verifica si alg\u00fan elemento del \u00e1rbol <code>a<\/code> cumple la propiedad <code>p<\/code>. Por ejemplo,<\/p>\n<pre lang=\"text\">\n   algunoArbol (N 5 (N 3 (H 1) (H 4)) (H 2)) (>4)  ==  True\n   algunoArbol (N 5 (N 3 (H 1) (H 4)) (H 2)) (>7)  ==  False\n<\/pre>\n<p><b>Soluciones<\/b><\/p>\n<p>A continuaci\u00f3n se muestran las <a href=\"#haskell\">soluciones en Haskell<\/a> y las <a href=\"#python\">soluciones en Python<\/a>.<\/p>\n<p><a name=\"haskell\"><\/a><br \/>\n<b>Soluciones en Haskell<\/b><\/p>\n<pre lang=\"haskell\">\ndata Arbol a = H a\n             | N a (Arbol a) (Arbol a)\n  deriving Show\n\nalgunoArbol :: Arbol a -> (a -> Bool) -> Bool\nalgunoArbol (H x) p     = p x\nalgunoArbol (N x i d) p = p x || algunoArbol i p || algunoArbol d p\n<\/pre>\n<p><a name=\"python\"><\/a><br \/>\n<b>Soluciones en Python<\/b><\/p>\n<pre lang=\"python\">\nfrom dataclasses import dataclass\nfrom typing import Callable, Generic, TypeVar\n\nA = TypeVar(\"A\")\n\n@dataclass\nclass Arbol(Generic[A]):\n    pass\n\n@dataclass\nclass H(Arbol[A]):\n    x: A\n\n@dataclass\nclass N(Arbol[A]):\n    x: A\n    i: Arbol[A]\n    d: Arbol[A]\n\ndef algunoArbol(a: Arbol[A], p: Callable[[A], bool]) -> bool:\n    match a:\n        case H(x):\n            return p(x)\n        case N(x, i, d):\n            return p(x) or algunoArbol(i, p) or algunoArbol(d, p)\n    assert False\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 \/ \\ \/ \\ 3 2 \/ \\ 1 4 se representa por N 5 (N 3 (H 1) (H&#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":[581],"tags":[269],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/7757"}],"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=7757"}],"version-history":[{"count":1,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/7757\/revisions"}],"predecessor-version":[{"id":7758,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/7757\/revisions\/7758"}],"wp:attachment":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/media?parent=7757"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/categories?post=7757"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/tags?post=7757"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}