PasteRack.org Paste # 61384 2018-03-13 15:38:11 Forked from paste # 83866. Fork as a new paste. Paste viewed 80 times. Tweet Embed:
1. #lang racket
2. (define t '(10 (5(4(2 ()()) (26()())) (21(15()()) (23 ()()))) (11(6()())(13()()))))
3.
4. (define (leaf? t) (null? t))
5. (define (tree-left-branch t) (cadr t))
6. (define (tree-right-branch t) (caddr t))
7.
8. (define (tree-fn t)
9.   (display `(visiting ,t))
10.   (newline)
11.   (if (leaf? t)
12.       #f
13.       (begin (tree-fn (tree-left-branch t))
14.              (tree-fn (tree-right-branch t)))))
15.
16. (tree-fn t)

=>

(visiting (10 (5 (4 (2 () ()) (26 () ())) (21 (15 () ()) (23 () ()))) (11 (6 () ()) (13 () ()))))

(visiting (5 (4 (2 () ()) (26 () ())) (21 (15 () ()) (23 () ()))))

(visiting (4 (2 () ()) (26 () ())))

(visiting (2 () ()))

(visiting ())

(visiting ())

(visiting (26 () ()))

(visiting ())

(visiting ())

(visiting (21 (15 () ()) (23 () ())))

(visiting (15 () ()))

(visiting ())

(visiting ())

(visiting (23 () ()))

(visiting ())

(visiting ())

(visiting (11 (6 () ()) (13 () ())))

(visiting (6 () ()))

(visiting ())

(visiting ())

(visiting (13 () ()))

(visiting ())

(visiting ())

#f

#### visit tree

1. (define t '(10 (5(4(2 ()()) (26()())) (21(15()()) (23 ()()))) (11(6()())(13()()))))
2.
3. (define (leaf? t) (null? t))
4. (define (tree-left-branch t) (cadr t))
5. (define (tree-right-branch t) (caddr t))
6.
7. (define (tree-fn t)
8.   (display `(visiting ,t))
9.   (newline)
10.   (if (leaf? t)
11.       #f
12.       (begin (tree-fn (tree-left-branch t))
13.              (tree-fn (tree-right-branch t)))))
14.
15. (tree-fn t)

=>

 (visiting (10 (5 (4 (2 () ()) (26 () ())) (21 (15 () ()) (23 () ()))) (11 (6 () ()) (13 () ())))) (visiting (5 (4 (2 () ()) (26 () ())) (21 (15 () ()) (23 () ())))) (visiting (4 (2 () ()) (26 () ()))) (visiting (2 () ())) (visiting ()) (visiting ()) (visiting (26 () ())) (visiting ()) (visiting ()) (visiting (21 (15 () ()) (23 () ()))) (visiting (15 () ())) (visiting ()) (visiting ()) (visiting (23 () ())) (visiting ()) (visiting ()) (visiting (11 (6 () ()) (13 () ()))) (visiting (6 () ())) (visiting ()) (visiting ()) (visiting (13 () ())) (visiting ()) (visiting ())

#f