简体   繁体   English

在二叉树结构方案中查找项目列表

[英]Find a list of items in a binary tree structures scheme

Write a function in Scheme that receives as argument of entry a binary search tree of instances of the student structure , a list of birth years and returns a list of list of codes that indicate which birth dates are provided each year. 在Scheme中编写一个函数,该函数接收学生结构实例的二进制搜索树,出生年份列表作为输入参数,并返回一个代码列表列表,以指示每年提供哪些出生日期。

example: 例:

This is the binary tree 这是二叉树

(make-árbol-bin  
(make-estudiante 5 "35889188" "Victor" (make-fecha 10 6 1991) "calle 67 con cra 20" "4444444")  (make-
árbol-bin  
(make-estudiante 2 "6457234" "Manuel" (make-fecha 12 10 1992) "calle 56 con cra 33" "5555555")   (make-árbol-bin 
   (make-estudiante 1 "94252688" "Estela" (make-fecha 20 5 1993) "calle 4 con cra 2" "3333333")    empty    empty)   empty) 
 (make-árbol-bin 
(make-estudiante 7 "34987678" "Juan" (make-fecha 25 3 1995) "calle 34 con cra 12" "6666666")   empty 
 empty) 
)

List years 列出年份

(list 1992 1991 )  

It should return a list of lists of codes: 它应该返回代码列表的列表:

((list (list 2) (list 5)  ) 

and I did this but I can not make me return the list of the list 我这样做了,但我不能让我返回列表的列表

(define-struct fecha ( dia mes año))

This is the structure of the student: 这是学生的结构:

(define-struct estudiante ( codigo id nombre fechanaci direccion telefono))

This is the structure of the binary search tree: 这是二进制搜索树的结构:

(define-struct arbol-bin( estudiante nod-izq nod-der)) 

I did this 我做了这个

(define (elimina X L)
        (if(null? L) '()
        (if (equal? X (car L))
            (elimina X (cdr L))
            (cons (car L) (elimina X (cdr L))))))

(define (busca d tree)
(if (null? tree)
    #f
    (let ((node (arbol-bin-estudiante tree)))
      (let ((n (estudiante-codigo node))) 
      (let ((fe (estudiante-fechanaci node)))
      (if (or(equal? d (fecha-año fe))
             (busca d (arbol-bin-nod-der tree)))
          n
            (busca d (arbol-bin-nod-izq tree))))))))

(define (pert lst tree)
(elimina false (map (lambda (d) (busca d tree)) lst)))

and this 和这个

(pert (list 1992   1991 )  tree)

return 返回

(list 2 5)

As I can do to make me return this: 我可以做的就是让我返回:

((list (list 2) (list 5)  )

help me please! 请帮帮我!

You cannot make it return ((list (list 2) (list 5))) , because that's not a correct Scheme expression. 您不能使其返回((list (list 2) (list 5))) ,因为这不是正确的Scheme表达式。 You can make it return (list (list 2) (list 5)) though: 您可以通过以下方式使其返回(list (list 2) (list 5))

> (list (list 2) (list 5))
'((2) (5))

To do so, in busca , just replace n with (list n) : 为此,在busca ,只需将n替换为(list n)

(define (busca d tree)
  (if (null? tree)
      #f
      (let ((node (árbol-bin-estudiante tree)))
        (let ((n (estudiante-codigo node))) 
          (let ((fe (estudiante-fechanaci node)))
            (if (or (equal? d (fecha-año fe))
                    (busca d (árbol-bin-nod-der tree)))
                (list n) ; <------
                (busca d (árbol-bin-nod-izq tree))))))))

or 要么

> (list (list (list 2) (list 5)))
'(((2) (5)))

by changing the above line to (list (list n)) . 通过将上面的行更改为(list (list n))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM