aboutsummaryrefslogtreecommitdiff
path: root/hw04_bintree.scm
blob: dbff02ce1ee7f374cf9e299f8ff85aa0619c87a1 (plain)
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#lang scheme

;; NAME: hw04_bintree.scm
;; AUTHOR: Ben Burwell
;; DESC: CSI310 - Programming Languages - Homework 4 - Binary Tree
;; HISTORY: Created 2013-02-11

;; ========== PROCEDURE ==========
;; NAME: tree?
;; DESC: takes a parameter and returns true if it is a tree
;;       and false otherwise
(define tree?
  (λ (arg)
    ;; a tree has the following properties:
    (and
     
     ;; it is a list
     (list? arg)
     
     ;; it has three elements
     (equal? (length arg) 3)
     
     ;; its first element is a number
     (number? (car arg))
     
     ;; its second element has one of the following:
     (or
      
      ;; it is an empty list...
      (and (list? (cadr arg)) (equal? (length (cadr arg)) 0))
      
      ;; ...or it is a tree
      (tree? (cadr arg)))
     
     ;; its third element also has one of the above conditions
     (or
      (and (list? (caddr arg)) (equal? (length (caddr arg)) 0))
      (tree? (caddr arg)))
     )))

;; ========== TEST CODE ==========
(newline)
(display "Testing (tree?) ==========================================================================")
(newline)

(display "Expected output: #t  ")
(tree? '(1 () ()))

(display "Expected output: #t  ")
(tree? '(5 (2 () ()) ()))

(display "Expected output: #f  ")
(tree? '())

(display "Expected output: #f  ")
(tree? 1)

(display "Expected output: #t  ")
(tree? '( 14 (7 () (12 () ())) (26 (20 (17 () ()) ()) (31 () ()))))





;; ========== PROCEDURE ==========
;; NAME: make-tree
;; DESC: makes a tree from three parameters
(define make-tree
  (λ (val lc rc)
    (cond
      [ (and (tree? lc) (tree? rc)) (list val lc rc) ]
      [ (tree? lc) (list val lc '()) ]
      [ (tree? rc) (list val '() rc) ]
      [ else (list val '() '()) ]
      )))

;; ========== TEST CODE ==========
(newline)
(display "Testing (make-tree) ======================================================================")
(newline)

(display "Expected output: (1 () ())                              ")
(make-tree 1 '() '())

(display "Expected output: (5 (4 (3 (2 (1 () ()) ()) ()) ()) ())  ")
(make-tree 5 (make-tree 4 (make-tree 3 (make-tree 2 (make-tree 1 '() '()) '()) '()) '()) '())


;; ========== PROCEDURE ==========
;; NAME: get-value
;; DESC: returns the value of the root element of the
;;       tree parameter, not-a-tree if the argument is
;;       not a tree
(define get-value
  (λ (tree)
    (if (tree? tree)
        (car tree)
        'not-a-tree
        )))

;; ========== TEST CODE ==========
(newline)
(display "Testing (get-value) ======================================================================")
(newline)

(display "Expected output: 1  ")
(get-value (make-tree 1 '() '()))

(display "Expected output: 5  ")
(get-value (make-tree 5 (make-tree 4 (make-tree 3 (make-tree 2 (make-tree 1 '() '()) '()) '()) '()) '()))

;; ========== PROCEDURE ==========
;; NAME: get-left
;; DESC: returns the left child of the tree parameter
;;       or not-a-tree if the parameter is not a tree
(define get-left
  (λ (tree)
    (if (tree? tree)
        (cadr tree)
        'not-a-tree
        )))

;; ========== TEST CODE ==========
(newline)
(display "Testing (get-left) =======================================================================")
(newline)

(display "Expected output: (1 () ())  ")
(get-left (make-tree 5 (make-tree 1 '() '()) '()))

;; ========== PROCEDURE ==========
;; NAME: get-right
;; DESC: returns the right child of the tree parameter
;;       or not-a-tree if the parameter is not a tree
(define get-right
  (λ (tree)
    (if (tree? tree)
        (caddr tree)
        'not-a-tree
        )))

;; ========== TEST CODE ==========
(newline)
(display "Testing (get-right) ======================================================================")
(newline)

(display "Expected output: (1 () ())  ")
(get-right (make-tree 5 '() (make-tree 1 '() '())))

;; ========== HELP FUNC ==========
;; takes a number to find in a bst as well as a path to append to
(define path-helper
  (λ (n bst pth)
    (cond
      
      ;; we have found the path
      [ (equal? (get-value bst) n) pth ]
      
      ;; the element is not in the tree, return not-found
      [ (and (not (tree? (get-left bst))) (not (tree? (get-right bst)))) 'not-found ]
      
      ;; n < value, we must go left
      [ (< n (get-value bst)) (cons 'left (path-helper n (get-left bst) pth)) ]
      
      ;; n > value, we must go right
      [ (> n (get-value bst)) (cons 'right (path-helper n (get-right bst) pth)) ]
    )))
    
;; ========== PROCEDURE ==========
;; NAME: path
;; DESC: takes a number and a binary search tree in which
;;       to find it and returns a list containing the
;;       appropriate "left"s and "right"s to navigate from
;;       the root of the tree to the element
;;
;;       returns not-found if the needle is not in the
;;       haystack.
(define path
  (λ (n bst)
    (cond
      [ (not (tree? bst)) 'not-a-tree ]
      [ else (path-helper n bst '()) ]
      )))

;; ========== TEST CODE ==========
(newline)
(display "Testing (path) ===========================================================================")
(newline)

(display "Expected output: (right left left)  ")
(path 17 '( 14 (7 () (12 () ())) (26 (20 (17 () ()) ()) (31 () ()))))