Advertisement
JuanFelipeArango28

Punto 3.2

Sep 15th, 2020
1,860
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scheme 3.02 KB | None | 0 0
  1. (define-struct arbol (valor izq der ))
  2.  
  3. (define arbol3 (make-arbol 5
  4.                            (make-arbol 10
  5.                                        (make-arbol 2
  6.                                                    (make-arbol 4
  7.                                                                empty
  8.                                                                empty)
  9.                                                    (make-arbol 8
  10.                                                                empty
  11.                                                                empty))
  12.                                        (make-arbol 6
  13.                                                    (make-arbol 3
  14.                                                                empty
  15.                                                                empty)
  16.                                                    empty))
  17.                            (make-arbol 30
  18.                                        (make-arbol 15
  19.                                                    empty
  20.                                                    empty)
  21.                                        (make-arbol 25
  22.                                                    (make-arbol 1
  23.                                                                empty
  24.                                                                empty)
  25.                                                    (make-arbol 9
  26.                                                                empty
  27.                                                                empty)))
  28.                                                    
  29.                            )
  30.   )
  31.  
  32. (define lista1 (list 1 2 3 4 5 6))
  33. (define lista2 (list 3 6 9 12))
  34. (define lista3 (list 5 10 2 4 8 6 3 30 15 25 1 9))
  35. ;Autores: Juan Felipe Arango Guzman, 202060066. Diego Fernando Llanos Mondragon, .
  36. ;Juan Sebastian Grajales Samudio; .
  37. ;Fecha: 14/09/2020
  38. ;Contrato: contener: listanumerica, arbol -> booleano
  39. ;Proposito: Determinar si los elementos de una lita estan contenidos dentro de un arbol
  40. ;Ejemplos:
  41. ;(comparar lista1 (preorden arbol3)) #true
  42. ;(comparar lista2 (preorden arbol3)) #false
  43. ;(comparar lista3 (preorden arbol3)) #true
  44. ;Definiciones:
  45. (define (contener lst arb)
  46.   (cond
  47.     [(empty? lst)#T]
  48.     [(conteneraux arb (first lst))
  49.      (contener (rest lst) arb)]
  50.     [(not(conteneraux arb (first lst)))
  51.      #F]
  52.     )
  53.   )
  54. ;Contrato: compararaux: lst, num -> booleano
  55. (define (conteneraux lst num)
  56.   (cond
  57.     [(empty? lst) #F]
  58.     [(equal? (first lst) num) #T]
  59.     [else (conteneraux (rest lst) num)]
  60.     )
  61.   )
  62. ;Contrato: preorden: arbol-> lista
  63. (define (preorden arbol)
  64.   (cond
  65.     [(empty? arbol) empty]
  66.     [else (append
  67.            (cons (arbol-valor arbol) empty)
  68.            (preorden (arbol-izq arbol))
  69.            (preorden (arbol-der arbol))
  70.            )]))
  71. ;Pruebas:
  72. (check-expect (contener lista1 (preorden arbol3)) #true)
  73. (check-expect (contener lista2 (preorden arbol3)) #false)
  74. (check-expect (contener lista3 (preorden arbol3)) #true)
  75.  
  76.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement