Scheme 采用整数n和包含整数n的BST的方案过程路径

Scheme 采用整数n和包含整数n的BST的方案过程路径,scheme,Scheme,编写一个Scheme过程路径,该路径采用整数n和包含整数n的二进制搜索树bst,并返回一个由1和0组成的字符串。向左移动对应于字符0('0'),向右移动对应于字符1('1') 例如: (path 17 '(14 (7 () (12 () ())) (26 (20 (17 () ())())(31 () ())))) "100". 在上面的例子中,我们找到路径时得到字符串100。我已经尝试过了,但是我的路径不正确。您没有提供您迄今为止编写的代码,因此我将勾勒出答案,以便您可以用自己的解决方案来填

编写一个Scheme过程路径,该路径采用整数
n
和包含整数
n
的二进制搜索树
bst
,并返回一个由1和0组成的字符串。向左移动对应于字符0
('0')
,向右移动对应于字符1
('1')

例如:

(path 17 '(14 (7 () (12 () ()))
(26 (20 (17 () ())())(31 () ()))))
"100".

在上面的例子中,我们找到路径时得到字符串100。我已经尝试过了,但是我的路径不正确。

您没有提供您迄今为止编写的代码,因此我将勾勒出答案,以便您可以用自己的解决方案来填补空白。假设
n
在树中:

(define (path n bst)
  (cond ((< <???> n) ; if the current element is less than n
         (string-append "1" <???>)) ; append 1, advance recursion to the right subtree
        ((> <???> n) ; if the current element is greater than n
         (string-append "0" <???>)) ; append 0, advance recursion to the left subtree
        (else        ; we found n
         <???>)))    ; base case, end recursion with empty string
(定义(路径n bst)
(cond((n);如果当前元素大于n
(字符串追加“0”);追加0,将递归推进到左子树
(否则,我们发现
)))    ; 基本情况,以空字符串结束递归

诀窍是遍历树,同时积累答案;假设输出是一个字符串,我们使用
stringappend

一路构建它,请将您迄今为止编写的代码添加到问题中。否则,听起来像是在要求别人做作业;)