简体   繁体   English

在二叉树方案2中查找列表

[英]Find a list in a binary tree scheme 2

Write a function in Scheme that receives as input argument list cedulas (identification) of students, a binary search tree with instances of the student structure and returns a list of instances of the student structure, where the identification of the list of students is in the binary search tree.在 Scheme 中编写一个函数,该函数接收学生的输入参数列表 cedulas(标识),带有学生结构实例的二叉搜索树,并返回学生结构的实例列表,其中学生列表的标识在二叉搜索树。

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 identification清单标识

(list "94252688" "94888888") 

It should return the following list:它应该返回以下列表:

(list (make-estudiante 1 "94252688" "Estela" (make-fecha 20 5 1993) "calle 4 con cra 2" "3333333") ) 

and I did this but I can not make me return 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)) 

these are the functions that searches the list in the tree这些是在树中搜索列表的函数

(define(buscarevd n E)
(cond
[(or(empty? E) (empty? n))false]
[(equal? (first n)(estudiante-id  E)) true]
 [else (buscarevd (rest n) E)]))

(define(buscare n E)
(cond 
 [(empty? E)false]
[(< (car n)(estudiante-id  E)) true]
 [else false]))


(define (member-bt x bt)
 (cond 
 [(empty? bt) false]
[(or(buscarevd x (arbol-bin-estudiante bt))
[else
(member-bt x (arbol-bin-nod-der bt))]))

and this和这个

(member-bt (list "94252688" "94888888") tree)

return返回

true

As I can do to make me return the list.尽我所能让我返回列表。

Not sure if there is some kind of relationship between nodes which would permit you to skip sub-trees here, so I'd use this kind of approach:不确定节点之间是否存在某种关系可以让您在这里跳过子树,所以我会使用这种方法:

(define (member-bt lst tree)
   (let loop ((tree tree) (res '()))
     (if (null? tree)
         res
         (let* ((node (árbol-bin-estudiante tree))
                (res1 (loop (árbol-bin-nod-der tree) res))
                (res2 (loop (árbol-bin-nod-izq tree) res1)))
           (if (member (estudiante-id node) lst string=?)
               (cons node res2)
               res2)))))

then然后

(let ((lst '("94888888" "34987678" "94252688")))
  (let ((res (member-bt lst tree)))
    (values res (map estudiante-nombre res))))
=>
'(#<estudiante> #<estudiante>)
'("Estela" "Juan")

Note that this traverses the tree only once, but the order of the students in the result list is not the same as in the initial list.请注意,这只会遍历树一次,但结果列表中学生的顺序与初始列表中的顺序不同。 If order needs to be preserved you need to take a different approach, for example:如果需要保留顺序,则需要采用不同的方法,例如:

(define (member-bt-ordered lst tree)
  (define (sub e tree)
    (if (null? tree)
        #f
        (let ((node (árbol-bin-estudiante tree)))
          (if (string=? e (estudiante-id node))
              node
              (or (sub e (árbol-bin-nod-der tree))
                  (sub e (árbol-bin-nod-izq tree)))))))
  (filter values (map (lambda (e) (sub e tree)) lst)))

then然后

(let ((lst '( "94252688" "94888888" "35889188" "34987678" "6457234")))
  (let ((res (member-bt-ordered lst tree)))
    (values res (map estudiante-nombre res))))
=> 
'(#<estudiante> #<estudiante> #<estudiante> #<estudiante>)
'("Estela" "Victor" "Juan" "Manuel")

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

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