Exercise 2.63. Each of the following two procedures converts a binary tree to a list.
lisp 代码:
(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 '()))
b. Do the two procedures have the same order of growth in the number of steps required to convert a balanced tree with n elements to a list? If not, which one grows more slowly?
第一种方法是T(n) = 2 * T(n/2) + 1
由master theorem得到时间复杂度是O(n)
但第二种方法是T(n, m) = T(n/2, T(n/2, m)) + 1
这个我就不知道怎么算时间复杂度