-
Notifications
You must be signed in to change notification settings - Fork 0
/
2-63.scm
53 lines (44 loc) · 1.68 KB
/
2-63.scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#lang scheme
(define (entry tree) (car tree))
(define (left-branch tree) (cadr tree))
(define (right-branch tree) (caddr tree))
(define (make-tree entry left right)
(list entry left right))
(define (tree->list-1 tree)
(if (null? tree)
'()
(append (tree->list-1 (left-branch tree))
(cons (entry tree)
(tree->list-1 (right-branch tree))))))
(define (tree->list-2 tree)
(define (copy-to-list tree result-list)
(if (null? tree)
result-list
(copy-to-list (left-branch tree)
(cons (entry tree)
(copy-to-list
(right-branch tree)
result-list)))))
(copy-to-list tree '()))
(define t1 (make-tree 7
(make-tree 3
(make-tree 1 nil nil)
(make-tree 5 nil nil))
(make-tree 9
nil
(make-tree 11 nil nil))))
(define t2 (make-tree 3
(make-tree 1 nil nil)
(make-tree 7
(make-tree 5 nil nil)
(make-tree 9
nil
(make-tree 11 nil nil)))))
(define t3 (make-tree 5
(make-tree 3 (make-tree 1 nil nil) nil)
(make-tree 9
(make-tree 7 nil nil)
(make-tree 11 nil nil))))
(define trees (list t1 t2 t3))
(map tree->list-1 trees)
(map tree->list-2 trees)