Skip to content

Commit

Permalink
invert binary tree
Browse files Browse the repository at this point in the history
  • Loading branch information
Joseph Williamson committed Jun 7, 2019
1 parent b549fe9 commit 2b1d683
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions invert-binary-tree.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#lang sicp
(define (make-tree entry left right) (cons entry (cons left right)))
(define (left-branch tree) (cadr tree))
(define (right-branch tree) (cddr tree))
(define (entry-tree tree) (car tree))
(define lefts-left (make-tree 'c '() '()))
(define rights-left (make-tree 'q '() '()))
(define l1 (make-tree 'f lefts-left '()))
(define r1 (make-tree 'z rights-left '()))
(define t1 (make-tree 'm l1 r1))

(left-branch t1)
(right-branch t1)

t1

(define (invert-tree tree)
(cond ((null? tree) '())
((cons (entry-tree tree)
(cons (invert-tree (right-branch tree))
(invert-tree (left-branch tree)))))))

(invert-tree t1)

0 comments on commit 2b1d683

Please sign in to comment.