Created
April 6, 2011 14:27
-
-
Save tjennings/905737 to your computer and use it in GitHub Desktop.
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
(ns bktree) | |
(defn root [distance-fn] {:distance-fn distance-fn :children {} :matches []}) | |
(defn new-node [term] {:term term :children {} :matches []}) | |
(defn insert | |
([element node] | |
(insert element node (:distance-fn node))) | |
([element node distance-fn] | |
(if (:term node) | |
(let [dist (distance-fn (:term node) element) | |
children (:children node) | |
child (get children dist)] | |
(if (= 0 dist) | |
(assoc node :matches (cons element (:matches node))) | |
(if child | |
(assoc node :children (assoc children dist (insert element child distance-fn))) | |
(assoc node :children (assoc children dist (new-node element)))))) | |
(assoc node :term element)))) | |
(defn walk [a-fn node] | |
(a-fn node) | |
(doall (map (fn [n] (walk a-fn n)) (vals (:children node))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment