SICP 全笔记

Exercise 2.64. The following procedure list->tree converts an ordered list to a balanced binary tree. The helper procedure partial-tree takes as arguments an integer n and list of at least n elements and constructs a balanced tree containing the first n elements of the list. The result returned by partial-tree is a pair (formed with cons) whose car is the constructed tree and whose cdr is the list of elements not included in the tree.

(define (list->tree elements)
  (car (partial-tree elements (length elements))))

(define (partial-tree elts n)
  (if (= n 0)
      (cons '() elts)
      (let ((left-size (quotient (- n 1) 2)))
        (let ((left-result (partial-tree elts left-size)))
          (let ((left-tree (car left-result))
                (non-left-elts (cdr left-result))
                (right-size (- n (+ left-size 1))))
            (let ((this-entry (car non-left-elts))
                  (right-result (partial-tree (cdr non-left-elts)
                                              right-size)))
              (let ((right-tree (car right-result))
                    (remaining-elts (cdr right-result)))
                (cons (make-tree this-entry left-tree right-tree)
                      remaining-elts))))))))

a. Write a short paragraph explaining as clearly as you can how partial-tree works. Draw the tree produced by list->tree for the list (1 3 5 7 9 11).

b. What is the order of growth in the number of steps required by list->tree to convert a list of n elements?

这段程序对 (1 3 5 7 9 11) 求值之后得到的结果是

(5 (1 () (3 () ())) (9 (7 () ()) (11 () ())))

这段程序对一个 list 不断地以中间的一个数字为界限(所以左边大小是 (/ (- n 1) 2),右边大小的 (- n (+ left-size 1))),不断地把左右两边的列表表示为树。因为这个列表是有序的列表,所以形成的一定是一颗左边子树比右边子树的值小的二叉树;因为每次对这个列表进行等数量的左右划分,所以得到的树总是一颗平衡树。

list->tree 转换一个长度为 n 的列表时,需要 O(log(n)) 的步数增长阶。