#93/111: Concrete Abstractions

Following the other basic books I wanted to strengthen my basic knowledge about computer science and this book is excellent for this task. It uses Scheme as the introductory language (like SICP) and does a great job in explaining basic data structures and algorithms. If you want to seriously learn about basic computer science, this is a nice book which is also a bit easy than SICP. You can actually read Concrete Abstractions for free which is quite nice.

Concrete Abstractions: Chapter 12

Exercise 12.4: Draw a tree, analogous to Figure 4.4 on page 92, showing how the value of C(4, 2) is computed by choose. Your tree should have C(4, 2) at the root and six 1s as its leaves.
Solution:

Exercise 12.6: Using these procedures, write
a. A procedure called table-fill! that takes a table and an element and sets every entry in the table to the given element. For example, (table-fill! table 0) would have a similar effect to that of zero-out-vector! in Section 11.6.
b. A procedure called display-table that nicely displays its table parameter.

Solution:

b.

Exercise 12.31: Imagine the following game: You are given a path that consists of white and black squares. The exact configuration of white and black squares varies with the game. You start on the leftmost square (which we’ll call square 0), and your goal is to move off the right end of the path in the least number of moves. However, the rules stipulate that:
If you are on a white square, you can move either 1 or 2 squares to the right.
If you are on a black square, you can move either 1 or 4 squares to the right. […]
Write a memoized version of fewest-moves.
Solution:

Exercise 12.33: The function h(n) is defined for nonnegative integers n as follows:
h(n) = \begin{cases} 1 & \text{ if } n<2\\ h(n-1) + h(n-2) & \text{ if n } < \text{2 and n is odd}\\ h(n-1) + h(n/2) & \text{ if n } \geq \text{2 and n is even}\\ \end{cases}
a. Write a dynamic programming procedure for efficiently calculating h(n).
b. Is it possible to modify the procedure so that it stores all the values it needs in a vector of fixed size, as walk-count can be modified to store the values it needs in a two-element vector? (A vector of “fixed size” is one with a size that does not depend on the parameter, n.) Justify your answer.
Solution: a & b. I implemented b directly into the code.

Concrete Abstractions: Chapter 8

Exercise 8.1: Write a procedure called minimum that will find the smallest element in a nonempty binary search tree of numbers.

Solution:

Exercise 8.2: Write a procedure called number-of-nodes that will count the number of elements in a binary search tree.

Solution:

Exercise 8.6: Suppose we want to create a new binary search tree by adding another element to an already existing binary search tree. Where is the easiest place to add such an element? Write a procedure called insert that takes a number and a binary search tree of numbers and returns a new binary search tree whose elements consist of the given number together with all of the elements of the binary search tree. You may assume that the given number isn’t already in the tree.

Solution:

Exercise 8.7: Using the procedure insert, write a procedure called list->bstree that takes a list of numbers and returns a binary tree whose elements are those numbers. Try this on several different lists and draw the corresponding tree diagrams. What kind of list gives you a short bushy tree? What kind of list gives a tall skinny tree?

Solution:

Exercise 8.11: In many applications, binary trees aren’t sufficient because we need more than two subtrees. An m-ary tree is a tree that is either empty or has a root and m subtrees, each of which is an m-ary tree. Generalize the previous results to m-ary trees.

Solution:
Ok. So let’s see if we can find a formular for 3-ary trees first.

So we can see that a tree with an height of two has 3^2 + 3^1 + 3^0 nodes. Or more general: A 3-ary tree with an height of n has sum_{i=0}^{n} 3^i nodes. This can be simplified as frac{1 - 3^{n+1}}{1-3}. So is this true? Let’s see. The height should be n. What’s happening if we increase the height by one? Each leave will get three children. We assumed that there are 3^n leaves. Therefore there will be 3^n * 3 = 3^{n+1} children. That is, there will be at most text{nodes}(n+1) = text{nodes}(n) + 3^{n+1} nodes which is equivalent to sum_{i=0}^{n} 3^i + 3^{n+1} = sum_{i=0}^{n+1} 3^i.
Therefore there are sum_{i=0}^{n} m^i nodes in a m-ary of height n.

Exercise 8.12: In Exercise 8.7, you wrote a procedure list->bstree that created a binary search tree from a list by successively inserting the elements into the tree. This procedure can lead to trees that are far from minimum height—surprisingly, the worst case occurs if the list is in sorted order. However, if you know the list is already in sorted order, you can do much better: Write a procedure sorted-list->min-height-bstree that creates a minimum height binary search tree from a sorted list of numbers. Hint: If the list has more than one element, split it into three parts: the middle element, the elements before the middle element, and the elements after. Construct the whole tree by making the appropriate recursive calls on these sublists and combining the results.

Solution:

Exercise 8.31: Write a procedure that takes as arguments a binary search tree of numbers, a lower bound, and an upper bound and counts how many elements of the tree are greater than or equal to the lower bound and less than or equal to the upper bound. Assume that the tree may contain duplicate elements. Make sure your procedure doesn’t examine more of the tree than is necessary.

Solution:

Concrete Abstractions: Chapter 7

Exercise 7.5 Generalize sum to a higher-order procedure that can accumulate together the elements of a list in an arbitrary fashion by using a combining procedure (such as +) specified by a procedural parameter. When the list is empty, sum returned 0, but this result isn’t appropriate for other combining procedures. For example, if the com- bining procedure is *, 1 would be the appropriate value for an empty list. (Why?) Following are two possible approaches to this problem:
a. Write the higher-order procedure so that it only works for nonempty lists. That way, the base case can be for one-element lists, in which case the one element can be returned.
b. Write the higher-order procedure so that it takes an additional argument, beyond the list and the combining procedure, that specifies the value to return for an empty list.

Solution:
a.

; b.

Exercise 7.6 a. Write a procedure that will count the number of times a particular element occurs in a given list.
b. Generalize this procedure to one that will count the number of elements in a given list that satisfy a given predicate.

Solution:

Exercise 7.13: The procedure map is extraordinarily handy for creating lists of all sorts. Each of the following problems can be solved by using map.
a. Write a procedure that, when given a positive integer n, returns a list of the first n perfect squares.
b. Write a procedure that, when given a positive integer n, returns a list of the first n even integers.
c. Write a procedure called sevens that, when given a positive integer n, returns a list of n sevens. For example:

d. Write a procedure that, when given a list of positive integers, returns a list of lists of integers. Each of these lists should be the positive integers from 1 to whatever was in the original list. For example,

Solution:
a.

b.

c.

d.

Ex 7.14

Exercise 7.42: Write a procedure called apply-all that, when given a list of functions and a number, will produce the list of the values of the functions when applied to the number.

Solution:

Exercise 7.44: Consider the following two procedures. The procedure last selects the last element from a list, which must be nonempty. It uses length to find the length of the list.

a. How many cdrs does (length lst) do when lst has n elements?
b. How many calls to length does (last lst) make when lst has n elements?
c. Express in Theta notation the total number of cdrs done by (last lst), including cdrs done by length, again assuming that lst has n elements.
d. Give an exact formula for the total number of cdrs done by (last lst), including cdrs done by length, again assuming that lst has n elements.

Solution:
a. n, which can be easily seen. An empty list needs 0 cdrs, a list with length 1 needs one cdrs. If we use induction, we can assume that this holds. For a list of length (n+1), we can see that length uses one cdr and then generates (length ). Therefore there are n uses of cdr for a list of length n.
b. Let’s start with the base case. (last ‘(1)) here last calls length one time. We can see that each time last have to call length. Therefore we can see that it needs n calls for a list of length n. The idea of the proof follows the last one.
c. and d. How many cdrs does last need without considering length? If the length is 1, it needs 0. If the length is 2, it needs 1. In general it needs (n-1) times cdr. We already know how often last calls length, so we can calculate the number of all calls. There are (n-1) calls from last directly. For each iteration in last we need 1 call of length. And each call of length needs (k+1) cdrs. If we merge everything together, we get the following table

Now we just have to add cdr from last and cdr from length and get:
(n-1) + sum_{i=1}{n} i = (n-1) + frac{n(n-1)}{2} = frac{n^2 + 3n - 2}{2}
Which is Theta(n^2) in Theta notation.

Exercise 7.47: Write a procedure map-2 that takes a procedure and two lists as arguments and returns the list obtained by mapping the procedure over the two lists, drawing the two arguments from the two lists. Write this procedure map-2. You may assume that the lists have the same length.

Solution: