-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathkd_tree.clj
More file actions
23 lines (21 loc) · 862 Bytes
/
kd_tree.clj
File metadata and controls
23 lines (21 loc) · 862 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
(ns lambda-ml.data.kd-tree
(:require [lambda-ml.data.binary-tree :as bt]))
;; K-d tree
(defn make-tree
"Returns a k-d tree, with dims as the number of dimensions, for the given
nodes. Optionally, a function f can be supplied and used to return the
k-dimensional point for a given node. Otherwise, the node itself is assumed to
be the k-dimensional point."
([dims nodes]
(make-tree dims nodes identity))
([dims nodes f]
(make-tree dims nodes f 0))
([dims nodes f depth]
(if (empty? nodes)
nil
(let [dim (fn [node] (nth (f node) (mod depth dims)))
sorted (sort-by dim nodes)
median (quot (count sorted) 2)]
(bt/make-tree (nth sorted median)
(make-tree dims (take median sorted) f (inc depth))
(make-tree dims (drop (+ median 1) sorted) f (inc depth)))))))