Skip to content

Instantly share code, notes, and snippets.

@tjennings
Created April 6, 2011 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tjennings/905737 to your computer and use it in GitHub Desktop.
Save tjennings/905737 to your computer and use it in GitHub Desktop.
(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