-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add exercise 2.69 Implemented exercise 2.69 which involves generating a Huffman encoding tree. Updated the make-leaf-set function in the book_2_3.clj file for correct functionality. The README has been updated to reflect the progress of completed exercises and the addition of exercise 2.69. * Refactor Huffman Tree related tests and logic Refactored the logic related to Huffman Tree examples and tests. Created constants for symbols, messages, and Huffman Tree in book_2_3.clj and reference these constants in tests to reduce code duplication. This also allows easier changes in future as only the main definition needs to be adjusted. * Remove unused import and update encode call in test Removed unused 'huffman-pairs' import from book_2_3_test.clj. Also, updated the call to 'encode' in ex_2_68_test.clj to use the 'huffman-tree' defined in 'b23', providing cleaner and more maintainable code.
- Loading branch information
Showing
7 changed files
with
106 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
(ns sicp.chapter-2.part-3.ex-2-69 | ||
(:require [sicp.chapter-2.part-3.book-2-3 :as b23])) | ||
|
||
; Exercise 2.69 | ||
; | ||
; The following procedure takes as its argument a list of symbol-frequency pairs | ||
; (where no symbol appears in more than one pair) and generates a Huffman encoding tree according | ||
; to the Huffman algorithm. | ||
; | ||
; (define (generate-huffman-tree pairs) | ||
; (successive-merge | ||
; (make-leaf-set pairs))) | ||
; | ||
; Make-leaf-set is the procedure given above that transforms the list of pairs into an ordered set | ||
; of leaves. Successive-merge is the procedure you must write, using make-code-tree to successively | ||
; merge the smallest-weight elements of the set until there is only one element left, which is the | ||
; desired Huffman tree. | ||
; | ||
; (This procedure is slightly tricky, but not really complicated. If you find yourself designing | ||
; a complex procedure, then you are almost certainly doing something wrong.) | ||
; | ||
; You can take significant advantage of the fact that we are using an ordered set representation.) | ||
|
||
(defn successive-merge [leaf-set] | ||
(if (= (count leaf-set) 1) | ||
(first leaf-set) | ||
(let [first (first leaf-set) | ||
second (second leaf-set) | ||
rest (drop 2 leaf-set)] | ||
(successive-merge (b23/adjoin-set-h | ||
(b23/make-code-tree first second) | ||
rest))))) | ||
|
||
(defn generate-huffman-tree [pairs] | ||
(successive-merge (b23/make-leaf-set pairs))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
(ns sicp.chapter-2.part-3.ex-2-69-test | ||
(:require [clojure.test :refer [deftest is]] | ||
[sicp.chapter-2.part-3.book-2-3 :as b23] | ||
[sicp.chapter-2.part-3.ex-2-68 :refer [encode-symbol]] | ||
[sicp.chapter-2.part-3.ex-2-69 :refer [generate-huffman-tree]])) | ||
|
||
(deftest generate-huffman-tree-test | ||
(is (= b23/huffman-tree (generate-huffman-tree b23/huffman-pairs)))) | ||
|
||
(deftest encode-symbol-test | ||
(is (= b23/huffman-A (encode-symbol :A (generate-huffman-tree b23/huffman-pairs)))) | ||
(is (= b23/huffman-B (encode-symbol :B (generate-huffman-tree b23/huffman-pairs)))) | ||
(is (= b23/huffman-C (encode-symbol :C (generate-huffman-tree b23/huffman-pairs)))) | ||
(is (= b23/huffman-D (encode-symbol :D (generate-huffman-tree b23/huffman-pairs))))) |