43 result.
m_size = points.size();
44 result.
m_root = Construct<0>(points);
54 std::queue<kd::BaseNode<P>*> todo;
57 while (!todo.empty()) {
78 std::queue<kd::BaseNode<P>*> q;
83 if (current ==
nullptr)
86 if (current->
GetPoint().InBox(box)) {
103 Apply([&result, &point](
const P& pt) ->
bool {
113 template <
typename P>
119 template <
typename P>
123 std::queue<std::pair<int, kd::BaseNode<P>*>> q;
124 q.emplace(1,
m_root.get());
126 auto tmp = q.front();
135 return static_cast<size_t>(h);
138 template <
typename P>
143 m_root = std::make_unique<kd::Node<P, 0>>(point);
157 template <
typename P>
160 std::vector<P> result;
162 [&result](
const P& pt) ->
bool {
163 result.push_back(pt);
170 template <
typename P>
176 template <
typename P>
177 template <std::
size_t D>
183 std::size_t med = kd::Median<P, D>(points);
184 auto median_val = points[med].template Get<D>();
187 std::vector<P> left, right;
188 for (std::size_t i = 0; i < points.size(); i++) {
189 const auto& p = points[i];
192 }
else if (p.template Get<D>() <= median_val) {
199 auto root = std::make_unique<kd::Node<P, D>>(root_pt);
200 root->m_left = Construct<(D + 1) % P::dim>(left);
201 root->m_right = Construct<(D + 1) % P::dim>(right);
A k-d tree: a k-dimensional generalization of binary search trees This data structure allows for effi...
AxisAlignedBoundingBox (hyperrectangle defined by lower and upper bound for every dimension)...
bool Empty() const
Is the tree empty.
static std::unique_ptr< kd::Node< P, D > > Construct(const std::vector< P > &points)
std::size_t Size() const
Get the size of the tree.
std::size_t m_size
The number of points in the tree.
virtual BaseNode< P > * BorrowRight() const =0
Get a non-owning pointer to the right child (nullptr if no such child).
void Insert(P point)
Insert a new point into the tree, using this often may result in an unbalanced tree.
virtual BaseNode< P > * BorrowSplitChild(const P &point) const =0
Get a non-owning pointer to the child corresponding to the correct split for point.
std::size_t GetHeight() const
Get the height of the tree (mostly for testing purposes).
KdTree()
Constructor: builds an empty tree.
Namespace for the geographic and demograhic classes.
virtual P GetPoint() const =0
Gets the point for this node.
P lower
The lower bound for every dimension.
P upper
The upper bound for every dimension.
virtual void AddChild(P point)=0
Add a new child in the right place, according to split.
std::unique_ptr< kd::Node< P, 0 > > m_root
The root node of the tree.
std::vector< P > Query(const AABBox< P > &box) const
Get all points in the tree that lie within box.
static KdTree Build(const std::vector< P > &points)
Build a balanced tree from the given set of points efficiently.
virtual BaseNode< P > * BorrowLeft() const =0
Get a non-owning pointer to the left child (nullptr if no such child).
bool Contains(const P &point) const
Test wether a point is contained in the tree.
void Apply(std::function< bool(const P &)> f) const
Calls a function with each of the points in the tree.
A base class for all instantiations of a Node with D.