Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
34
Task/Huffman-coding/Clojure/huffman-coding-1.clj
Normal file
34
Task/Huffman-coding/Clojure/huffman-coding-1.clj
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
(require '[clojure.pprint :refer :all])
|
||||
|
||||
(defn probs [s]
|
||||
(let [freqs (frequencies s) sum (apply + (vals freqs))]
|
||||
(into {} (map (fn [[k v]] [k (/ v sum)]) freqs))))
|
||||
|
||||
(defn init-pq [weighted-items]
|
||||
(let [comp (proxy [java.util.Comparator] []
|
||||
(compare [a b] (compare (:priority a) (:priority b))))
|
||||
pq (java.util.PriorityQueue. (count weighted-items) comp)]
|
||||
(doseq [[item prob] weighted-items] (.add pq { :symbol item, :priority prob }))
|
||||
pq))
|
||||
|
||||
(defn huffman-tree [pq]
|
||||
(while (> (.size pq) 1)
|
||||
(let [a (.poll pq) b (.poll pq)
|
||||
new-node {:priority (+ (:priority a) (:priority b)) :left a :right b}]
|
||||
(.add pq new-node)))
|
||||
(.poll pq))
|
||||
|
||||
(defn symbol-map
|
||||
([t] (symbol-map t ""))
|
||||
([{:keys [symbol priority left right] :as t} code]
|
||||
(if symbol [{:symbol symbol :weight priority :code code}]
|
||||
(concat (symbol-map left (str code \0))
|
||||
(symbol-map right (str code \1))))))
|
||||
|
||||
(defn huffman-encode [items]
|
||||
(-> items probs init-pq huffman-tree symbol-map))
|
||||
|
||||
(defn display-huffman-encode [s]
|
||||
(->> s huffman-encode (sort-by :weight >) print-table))
|
||||
|
||||
(display-huffman-encode "this is an example for huffman encoding")
|
||||
38
Task/Huffman-coding/Clojure/huffman-coding-2.clj
Normal file
38
Task/Huffman-coding/Clojure/huffman-coding-2.clj
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
(require '[clojure.data.priority-map :refer [priority-map-keyfn-by]])
|
||||
(require '[clojure.pprint :refer [print-table]])
|
||||
|
||||
(defn init-pq [s]
|
||||
(let [c (count s)]
|
||||
(->> s frequencies
|
||||
(map (fn [[k v]] [k {:sym k :weight (/ v c)}]))
|
||||
(into (priority-map-keyfn-by :weight <)))))
|
||||
|
||||
(defn huffman-tree [pq]
|
||||
(letfn [(build-step
|
||||
[pq]
|
||||
(let [a (second (peek pq)) b (second (peek (pop pq)))
|
||||
nn {:sym (str (:sym a) (:sym b))
|
||||
:weight (+ (:weight a) (:weight b))
|
||||
:left a :right b}]
|
||||
(assoc (pop (pop pq)) (:sym nn) nn)))]
|
||||
(->> (iterate build-step pq)
|
||||
(drop-while #(> (count %) 1))
|
||||
first vals first)))
|
||||
|
||||
(defn symbol-map [m]
|
||||
(letfn [(sym-step
|
||||
[{:keys [sym weight left right] :as m} code]
|
||||
(cond (and left right) #(vector (trampoline sym-step left (str code \0))
|
||||
(trampoline sym-step right (str code \1)))
|
||||
left #(sym-step left (str code \0))
|
||||
right #(sym-step right (str code \1))
|
||||
:else {:sym sym :weight weight :code code}))]
|
||||
(trampoline sym-step m "")))
|
||||
|
||||
(defn huffman-encode [s]
|
||||
(->> s init-pq huffman-tree symbol-map flatten))
|
||||
|
||||
(defn display-huffman-encode [s]
|
||||
(->> s huffman-encode (sort-by :weight >) print-table))
|
||||
|
||||
(display-huffman-encode "this is an example for huffman encoding")
|
||||
Loading…
Add table
Add a link
Reference in a new issue