sicp-solutions/chapter-2/ex-2.64.txt

22 lines
865 B
Text

a)
partial-tree takes a list elts, and n, a number of starting elements which it
will process, it arranges these n elements into a tree and returns a list with
2 elements. The first is a tree of the first n elements, the second is a list
of the remaining elements. The arrangement is done by making a tree out of
(n-1)/2 elements recursively, then adding the one element at the start of the
remainder-list as the current entry, then taking the other half of the n
elements, and recursively arranging them as well.
(list->tree '(1 3 5 7 9 11))
==
5
/ \
1 9
\ / \
3 7 11
b)
Since only the top-level list has its length calculated O(n), while all the
lower ones have theirs calculated in constant time. And in general, every
specific call of partial-tree has a number of constant operations, the total
would be O(n)