From: Ramakrishnan Muthukrishnan Date: Sun, 12 Sep 2010 06:15:31 +0000 (+0530) Subject: solution to 2.63. X-Git-Url: https://git.rkrishnan.org/pf/content/en/components/com_hotproperty/css?a=commitdiff_plain;h=e29af8c80ecb79c7e739788bd1b6dc54c26d7d28;p=sicp.git solution to 2.63. --- diff --git a/src/sicp/ex2_63.clj b/src/sicp/ex2_63.clj new file mode 100644 index 0000000..0da2210 --- /dev/null +++ b/src/sicp/ex2_63.clj @@ -0,0 +1,43 @@ +(ns sicp.ex2_63 + (:use [sicp.ch2_3 :only (entry left-branch right-branch)] + [clojure.test])) + +(defn tree->list-1 [tree] + (if (empty? tree) + '() + (concat (tree->list-1 (left-branch tree)) + (cons (entry tree) + (tree->list-1 (right-branch tree)))))) + +(defn tree->list-2 [tree] + (let [copy-to-list (fn f [tree result-list] + (if (empty? tree) + result-list + (f (left-branch tree) + (cons (entry tree) + (f (right-branch tree) result-list)))))] + (copy-to-list tree '()))) + +(deftest test-for-some-trees + (are [x y] [= x y] + (tree->list-1 '(7 (3 (1 ()) (5 ())) (9 () (11 ())))) '(1 3 5 7 9 11) + (tree->list-2 '(7 (3 (1 ()) (5 ())) (9 () (11 ())))) '(1 3 5 7 9 11) + (tree->list-1 '(3 (1 () ()) (7 (5 () ()) (9 () (11 () ()))))) '(1 3 5 7 9 11) + (tree->list-2 '(3 (1 () ()) (7 (5 () ()) (9 () (11 () ()))))) '(1 3 5 7 9 11) + (tree->list-1 '(5 (3 (1 () ()) ()) (9 (7 () ()) (11 () ())))) '(1 3 5 7 9 11) + (tree->list-2 '(5 (3 (1 () ()) ()) (9 (7 () ()) (11 () ())))) '(1 3 5 7 9 11))) + +;;;; part a +(comment + "yes, as the above tests show, they add elements into the list in the same order + and hence produce same lists." +) + +;;;; part b +(comment + "tree->list-1 does a cons and an append of two lists at every step. The depth of the + tree is log(n). append grows as length of the list. The number of steps required to + traverse down to the tree is log(n). So, this is a O(n.log(n)) operation." + + "list->tree-2 does only a cons operation at every step. So this is a O(n) operation." +)