方案中的二叉树
考虑以下定义数字树的 BNF。 请注意,树可以是叶子、具有一个子树的节点 1 或节点 2 有两个子树。
tree ::= (’leaf number)
| (’node-1 tree)
| (’node-2 tree tree)
一个。为这些树上的递归过程编写一个模板。
b.定义返回以下值的过程 (leaf-count t) number of leaves in t
> (leaf-count ’(leaf 5))
1
> (leaf-count ’(node-2 (leaf 25) (leaf 17)))
2
> (leaf-count ’(node-1
(node-2 (leaf 4)
(node-2 (leaf 2) (leaf 3)))))
3
这是我到目前为止所得到的:
;define what a leaf, node-1, and node-2 is
(define leaf list)
(define node-1 list)
(define node-2 list)
;procedure to decide if a list is a leaf or a node
(define (leaf? tree) (number? (car tree)))
(define (node? tree) (pair? (car tree)))
(define (leaf-count tree)
(cond ((null? tree) 0)
((number? tree) 0)
((leaf? tree) 1)
(else (+ (leaf-count (car tree))
(leaf-count (cdr tree))))))
看起来它应该运行得很好,但是当我尝试使用一个简单的测试用例运行它时,
(leaf-count '(leaf 5))
我收到以下错误消息:
car:需要类型为pair的参数;给定叶子
这个错误消息是什么意思?我将叶子定义为列表。但由于某种原因,它没有看到这一点并给了我该错误消息。
Consider the following BNF defining trees of numbers.
Notice that a tree can either be a leaf, a node-1 with one subtrees, or a node-2
with two subtrees.
tree ::= (’leaf number)
| (’node-1 tree)
| (’node-2 tree tree)
a. Write a template for recursive procedures on these trees.
b. Define the procedure (leaf-count t) that returns the
number of leaves in t
> (leaf-count ’(leaf 5))
1
> (leaf-count ’(node-2 (leaf 25) (leaf 17)))
2
> (leaf-count ’(node-1
(node-2 (leaf 4)
(node-2 (leaf 2) (leaf 3)))))
3
Here's what I have so far:
;define what a leaf, node-1, and node-2 is
(define leaf list)
(define node-1 list)
(define node-2 list)
;procedure to decide if a list is a leaf or a node
(define (leaf? tree) (number? (car tree)))
(define (node? tree) (pair? (car tree)))
(define (leaf-count tree)
(cond ((null? tree) 0)
((number? tree) 0)
((leaf? tree) 1)
(else (+ (leaf-count (car tree))
(leaf-count (cdr tree))))))
It looks like it should run just fine, but when I try to run it using a simple test case like
(leaf-count '(leaf 5))
I get the following error message:
car: expects argument of type pair; given leaf
What does this error message mean? I am defining a leaf as a list. But for some reason, it's not seeing that and gives me that error message.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
确实,解决别人的作业很有趣。
Solving other people's assignments is fun, indeed.
您引用了 leaf,
(leaf-count '(leaf 5))
所以它是一个符号,而不是您之前定义的变量。这是错误的原因,但不是您应该修复的问题。您的三个定义没有多大意义,并且检测叶或节点的过程与 BNF 规范不匹配。这是您自己的示例中的一棵树:
'(node-1 (node-2 (leaf 4) (node-2 (leaf 2) (leaf 3))))
。它被引用,因此node-1
、node-2
和leaf
只是符号,不需要定义。现在编写leaf?
和node?
函数来检测上述树的各个元素是什么。这是一个测试用例,其中所有函数调用都应返回 true:一旦有效,计数应该也没有问题(尽管您当前的方法不起作用,但我建议编写左子树和右子树函数来帮助您与此)。
You've quoted leaf,
(leaf-count '(leaf 5))
so it's a symbol, not a variable you've defined earlier. That's the cause of the error, but not the thing you should fix. Your three defines have no much sense and your procedures to detect leaf or node do not match the BNF specification.Here is a tree from your own example:
’(node-1 (node-2 (leaf 4) (node-2 (leaf 2) (leaf 3))))
. It's quoted sonode-1
,node-2
andleaf
are just symbols and need not to be defined. Now writeleaf?
andnode?
functions that could detect what the various elements of above tree are. Here is a test case for you where all the function calls should return true:Once this works, counting should be no problem either (altough your current method wouldn't work, I propose writing left-subtree and right-subtree functions to help you with that).
到目前为止,这是我所拥有的:
我对其进行了细微的调整:更改条件以检查列表的 cadr,因为这就是包含信息的内容。在这种情况下,列表中的汽车只是数据的标签(叶子、节点等)。不完全是。我让它适用于最基本的测试用例,但不适用于更复杂的测试用例,例如
Here's what I have so far:
I got it to work with a minor adjustment: change the conditionals to check the cadr of the list as that is what contains the info. The car of the list in this case is just the label of what the data is (leaf, node, etc.). Not quite. I got it to work for the most basic of test cases, but not for more complex ones like