Description
Submission:

Submit your code in Canvas as one “hw2.scm” file containing all your functions.

The file must be able to load and be tested in the interpreter.

Consequently, the file must be in a simple text format; do not submit Word, PDF,



Also make sure that any auxiliary information (such as your name or question numbers) is commented out.



(55 pts) Consider an implementation of sets with Scheme lists. A set is an unordered collection of elements, without duplicates.


(11 pts) Write a recursive function (isset? L), which determines whether the list L is a set. The following examples illustrate the use of this function:

> (isset? ‘(1 2 5))
#t


(isset? ‘(1 5 2 5))

#f

(11 pts) Write a recursive function (makeset L), which returns a set built from list L by removing duplicates, if any. Remember that the order of set elements does not matter. The following example illustrates the use of this function:


(makeset ‘(4 3 5 3 4 1))

(5341)

(11 pts) Write a recursive function (subset? A S), which determines whether the set A is a subset of the set S.

(11 pts) Write a recursive function (union A B), which returns the union of sets A and
B.

(11 pts) Write a recursive function (intersection A B), which returns the intersection of sets A and B.

(33 pts) Consider an implementation of binary trees with Scheme lists, as in the following example:

(define T
13
‘(13
5
22
(5
(1 () ())
(8 ()
1
8
17
25
(9 () ())))
(22
9
(17 () ())
(25 () ()))))
Before proceeding, it may be useful to define three auxiliary functions (val T), (left T) and (right T), which return the value in the root of tree T, its left subtree and its right subtree, respectively.

(11 pts) Write a recursive function (treemember? V T), which determines whether V appears as an element in the tree T. The following example illustrates the use of this function:


(treemember? 17 T)

#t

(11 pts) Write a recursive function (preorder T), which returns the list of all elements in the tree T corresponding to a preorder traversal of the tree. The following example illustrates the use of this function:


(preorder T)

(135189221725)

(11 pts) Write a recursive function (inorder T), which returns the list of all elements in the tree T corresponding to an inorder traversal of the tree. The following example illustrates the use of this function:


(inorder T)

(158913172225)

(12 pts) Write a recursive function (deepdelete V L), which takes as arguments a value V and a list L, and returns a list identical to L except that all occurrences of V in L or in any sublist of L have been deleted. The following example illustrates the use of this function:


(deepdelete 3 ‘(1 2 3 (4 3) 5 (6 (3 7)) 8)) (1 2 (4) 5 (6 (7)) 8)


(Extra Credit – 10 pts) A binary search tree is a binary tree for which the value in each node is greater than or equal to all values in its left subtree, and less than all values in its right
subtree. The binary tree given as example in problem 2 also qualifies as a binary search tree. Using the same list representation, write a recursive function (insertbst V T), which returns the binary search tree that results by inserting value V into binary search tree T. For example, by inserting the value 20 into the binary search tree T shown above, the result should be:
13
5 22
^{1} ^{8} 17 25
9 20