scheme使用BT树并将树的元素作为字符串列表返回

scheme使用BT树并将树的元素作为字符串列表返回,scheme,racket,Scheme,Racket,为您提供了以下二进制尝试的定义 (define-struct leaf ()) ;; interpretation: represents a leaf on a BT, a node with no children (define-struct node (word left right)) ;; interpretation: represents a node on a BT with a word, a left and a right ;; subtree ;; A Binar

为您提供了以下二进制尝试的定义

(define-struct leaf ())
;; interpretation: represents a leaf on a BT, a node with no children

(define-struct node (word left right))
;; interpretation: represents a node on a BT with a word, a left and a right 
;; subtree

;; A BinaryTree (BT) is one of 
;; - (make-leaf)
;; - (make-node String BT BT)
设计程序bt->los,该程序使用bt树并以字符串列表的形式返回树的元素。在每个节点上,您的函数都应该

1.处理左子树

;; bt->los: tree -> los 
;; consumes a BT tree and returns the elements of the tree as a list of strings.
(define (bt->los tree)
(cond 
   [(leaf? tree) ""]
   [(node? tree) 
    (append  (bt->los(list (node-left tree)))
             (list (node-word tree))
             (bt->los (list (node-right tree))))]))
2.在此节点处理单词

3.处理正确的子树

;; bt->los: tree -> los 
;; consumes a BT tree and returns the elements of the tree as a list of strings.
(define (bt->los tree)
(cond 
   [(leaf? tree) ""]
   [(node? tree) 
    (append  (bt->los(list (node-left tree)))
             (list (node-word tree))
             (bt->los (list (node-right tree))))]))
我被困在这里了。应该错过什么。这里不需要递归吗

输出应该是

 (define bt-abc (make-node "b" 
                       (make-node "a" (make-leaf) (make-leaf))
                       (make-node "c" (make-leaf) (make-leaf))))
  (bt->los bt-abc) (cons "a" (cons "b" (cons "c" empty)))

你离这里很近。只是几个错误

(define (bt->los tree)
 (cond 
   [(leaf? tree) empty]
   [(node? tree) 
    (append  (bt->los (node-left tree))
             (list (node-word tree))
             (bt->los (node-right tree)))]))
首先,您正在构造一个字符串列表。因为它是一个列表,所以基本大小写应该是空的。不是
。第二,每个节点,已经代表一个BT,不需要列出它。和
bt->los
返回
名单。通过这些简单的更改,它可以按照您对测试用例的预期工作

你也能看看这个问题吗?谢谢