广度优先二叉树遍历Scheme

发布于 2024-08-13 14:00:36 字数 2105 浏览 3 评论 0原文

我正在尝试实现广度优先(级别)树遍历。我非常接近,但我不知道如何获得重复项。非常感谢任何帮助。提前致谢。 JR

(define (atom? x)
  (not (pair? x)))

;;Functions to manipulate a binary tree
(define (leaf? node) (atom? node))
(define (left node) (cadr node))
(define (right node) (caddr node))
(define (label node) (if (leaf? node) node (car node)))

;; Breadth First using queue
(define (breadth node)
  (q 'enqueue! node)              ;; Enqueue tree
  (output 'enqueue! (label node)) ;; Output root
  (helper node)
  (output 'queue->list)           ;; Output elements in queue
)
(define (helper node)
    (if (not(q 'empty?))          ;; If queue is not empty
    (begin
       (if(not(leaf? node))
       (begin
          (q 'enqueue! (left node))          ;;  left tree to q
          (output 'enqueue! (label(left node)))   ;; Output root of left tree
          (q 'enqueue! (right node))         ;; Enqueue right tree to q
          (output 'enqueue! (label(right node)))  ;; Output root of right tree
       ))
       (helper (q 'dequeue!))                ;; Dequeues 1st element in q
                                             ;; and recursively calls helper  
    )
    )
)

(define (make-queue)
  (let ((front '())
        (back '()))
    (lambda (msg . obj)
      (cond ((eq? msg 'empty?) (null? front))
            ((eq? msg 'enqueue!) 
             (if (null? front)
                 (begin 
                   (set! front obj)
                   (set! back obj))
                 (begin
                   (set-cdr! back obj)
                   (set! back obj))))
            ((eq? msg 'dequeue!) 
             (begin
               (let ((val (car front)))
                 (set! front (cdr front))
                 val)))
            ((eq? msg 'queue->list) front)))))
(define q (make-queue))
(define output (make-queue))

(define tree '(A (B C D)(E (F G H) I)))

---------------------------------------------------------
Welcome to DrScheme, version 4.2.2 [3m].
Language: R5RS; memory limit: 128 megabytes.
> (breadth tree)
(a b e b e c d f i c d f i g h g h)    ;; Should be (a b e c d f i g h)
>

I am trying to implement a breadth first (level) tree traversal. I'm very close, but I can't figure out how I'm getting duplicates. Any help is much appreciated. Thanks in advance.
JR

(define (atom? x)
  (not (pair? x)))

;;Functions to manipulate a binary tree
(define (leaf? node) (atom? node))
(define (left node) (cadr node))
(define (right node) (caddr node))
(define (label node) (if (leaf? node) node (car node)))

;; Breadth First using queue
(define (breadth node)
  (q 'enqueue! node)              ;; Enqueue tree
  (output 'enqueue! (label node)) ;; Output root
  (helper node)
  (output 'queue->list)           ;; Output elements in queue
)
(define (helper node)
    (if (not(q 'empty?))          ;; If queue is not empty
    (begin
       (if(not(leaf? node))
       (begin
          (q 'enqueue! (left node))          ;;  left tree to q
          (output 'enqueue! (label(left node)))   ;; Output root of left tree
          (q 'enqueue! (right node))         ;; Enqueue right tree to q
          (output 'enqueue! (label(right node)))  ;; Output root of right tree
       ))
       (helper (q 'dequeue!))                ;; Dequeues 1st element in q
                                             ;; and recursively calls helper  
    )
    )
)

(define (make-queue)
  (let ((front '())
        (back '()))
    (lambda (msg . obj)
      (cond ((eq? msg 'empty?) (null? front))
            ((eq? msg 'enqueue!) 
             (if (null? front)
                 (begin 
                   (set! front obj)
                   (set! back obj))
                 (begin
                   (set-cdr! back obj)
                   (set! back obj))))
            ((eq? msg 'dequeue!) 
             (begin
               (let ((val (car front)))
                 (set! front (cdr front))
                 val)))
            ((eq? msg 'queue->list) front)))))
(define q (make-queue))
(define output (make-queue))

(define tree '(A (B C D)(E (F G H) I)))

---------------------------------------------------------
Welcome to DrScheme, version 4.2.2 [3m].
Language: R5RS; memory limit: 128 megabytes.
> (breadth tree)
(a b e b e c d f i c d f i g h g h)    ;; Should be (a b e c d f i g h)
>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

我三岁 2024-08-20 14:00:36

由于这是家庭作业,我只会给出一个提示:重写 helper 以不接受任何参数。

Since it's homework, I'll just give a hint: rewrite helper to take no arguments.

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