方案中的二叉树

发布于 2024-08-21 11:42:30 字数 1126 浏览 4 评论 0原文

考虑以下定义数字树的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

初心 2024-08-28 11:42:30

确实,解决别人的作业很有趣。

(define leaf-count
  (match-lambda 
   [(list 'leaf x)     1]
   [(list 'node-1 t)   (leaf-count t)]
   [(list 'node-2 l r) (+ (leaf-count l) (leaf-count r))]))

Solving other people's assignments is fun, indeed.

(define leaf-count
  (match-lambda 
   [(list 'leaf x)     1]
   [(list 'node-1 t)   (leaf-count t)]
   [(list 'node-2 l r) (+ (leaf-count l) (leaf-count r))]))
假装不在乎 2024-08-28 11:42:30

您引用了 leaf,(leaf-count '(leaf 5)) 所以它是一个符号,而不是您之前定义的变量。这是错误的原因,但不是您应该修复的问题。您的三个定义没有多大意义,并且检测叶或节点的过程与 BNF 规范不匹配。

这是您自己的示例中的一棵树:'(node-1 (node-2 (leaf 4) (node-2 (leaf 2) (leaf 3))))。它被引用,因此 node-1node-2leaf 只是符号,不需要定义。现在编写 leaf?node? 函数来检测上述树的各个元素是什么。这是一个测试用例,其中所有函数调用都应返回 true:

(define a-tree ’(node-1 (node-2 (leaf 4) (node-2 (leaf 2) (leaf 3)))))
(node? a-tree)
(node? (car (cdr a-tree)))
(leaf? (car (cdr (car (cdr a-tree)))))
(node? (car (cdr (cdr (car (cdr a-tree))))))
(leaf? (car (cdr (car (cdr (cdr (car (cdr a-tree))))))))

一旦有效,计数应该也没有问题(尽管您当前的方法不起作用,但我建议编写左子树和右子树函数来帮助您与此)。

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 so node-1, node-2 and leaf are just symbols and need not to be defined. Now write leaf? and node? 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:

(define a-tree ’(node-1 (node-2 (leaf 4) (node-2 (leaf 2) (leaf 3)))))
(node? a-tree)
(node? (car (cdr a-tree)))
(leaf? (car (cdr (car (cdr a-tree)))))
(node? (car (cdr (cdr (car (cdr a-tree))))))
(leaf? (car (cdr (car (cdr (cdr (car (cdr a-tree))))))))

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).

沧笙踏歌 2024-08-28 11:42:30

到目前为止,这是我所拥有的:

 ;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? (cadr tree)))
(define (node? tree) (pair? (cadr tree)))

(define (leaf-count tree)
 (cond ((null? tree) 0)
        ((number? tree) 0)
        ((leaf? tree) 1)
        (else (+ (leaf-count (car tree))
                 (leaf-count (cdr tree))))))

我对其进行了细微的调整:更改条件以检查列表的 cadr,因为这就是包含信息的内容。在这种情况下,列表中的汽车只是数据的标签(叶子、节点等)。不完全是。我让它适用于最基本的测试用例,但不适用于更复杂的测试用例,例如

(leaf-count '(node-2 (leaf 25) (leaf 17)))

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? (cadr tree)))
(define (node? tree) (pair? (cadr tree)))

(define (leaf-count tree)
 (cond ((null? tree) 0)
        ((number? tree) 0)
        ((leaf? tree) 1)
        (else (+ (leaf-count (car tree))
                 (leaf-count (cdr tree))))))

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

(leaf-count '(node-2 (leaf 25) (leaf 17)))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文