1 |
|
---|
2 | #ifndef _COVERTREE_H
|
---|
3 | #define _COVERTREE_H
|
---|
4 |
|
---|
5 | #include <map>
|
---|
6 | class Observation;
|
---|
7 |
|
---|
8 | class Covertree{
|
---|
9 |
|
---|
10 | /**
|
---|
11 | * Cover tree node. Consists of arbitrarily many points P, as long as
|
---|
12 | * they have distance 0 to each other. Keeps track of its children.
|
---|
13 | */
|
---|
14 | class CoverTreeNode{
|
---|
15 | private:
|
---|
16 | //_childMap[i] is a vector of the node's children at level i
|
---|
17 | std::map<int,std::vector<CoverTreeNode*> > _childMap;
|
---|
18 | //_observations is all of the points with distance 0 which are not equal.
|
---|
19 | std::vector<Observation> _observations;
|
---|
20 | public:
|
---|
21 | CoverTreeNode(const Observation& o);
|
---|
22 | /**
|
---|
23 | * Returns the children of the node at level i. Note that this means
|
---|
24 | * the children exist in cover set i-1, not level i.
|
---|
25 | *
|
---|
26 | * Does not include the node itself, though technically every node
|
---|
27 | * has itself as a child in a cover tree.
|
---|
28 | */
|
---|
29 | std::vector<CoverTreeNode*> getChildren(int level) const;
|
---|
30 | void addChild(int level, CoverTreeNode* p);
|
---|
31 | void removeChild(int level, CoverTreeNode* p);
|
---|
32 | void addObservation(const Observation& o);
|
---|
33 | void removeObservation(const Observation& o);
|
---|
34 | const std::vector<Observation>& getObservations() { return _observations; }
|
---|
35 | double distance(const CoverTreeNode& p) const;
|
---|
36 |
|
---|
37 | bool isSingle() const;
|
---|
38 | bool hasObservation(const Observation& o) const;
|
---|
39 |
|
---|
40 | const Observation& getObservation() const;
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * Return every child of the node from any level. This is handy for
|
---|
44 | * the destructor.
|
---|
45 | */
|
---|
46 | std::vector<CoverTreeNode*> getAllChildren() const;
|
---|
47 | }; // CoverTreeNode class
|
---|
48 | private:
|
---|
49 | typedef std::pair<double, CoverTreeNode*> distNodePair;
|
---|
50 |
|
---|
51 | CoverTreeNode* _root;
|
---|
52 | unsigned int _numNodes;
|
---|
53 | int _maxLevel;//base^_maxLevel should be the max distance
|
---|
54 | //between any 2 points
|
---|
55 | int _minLevel;//A level beneath which there are no more new nodes.
|
---|
56 |
|
---|
57 | std::vector<CoverTreeNode*>
|
---|
58 | kNearestNodes(const Observation& o, const unsigned int& k) const;
|
---|
59 | /**
|
---|
60 | * Recursive implementation of the insert algorithm (see paper).
|
---|
61 | */
|
---|
62 | bool insert_rec(const Observation& p,
|
---|
63 | const std::vector<distNodePair>& Qi,
|
---|
64 | const int& level);
|
---|
65 |
|
---|
66 | /**
|
---|
67 | * Finds the node in Q with the minimum distance to p. Returns a
|
---|
68 | * pair consisting of this node and the distance.
|
---|
69 | */
|
---|
70 | distNodePair distance(const Observation& p,
|
---|
71 | const std::vector<CoverTreeNode*>& Q);
|
---|
72 |
|
---|
73 |
|
---|
74 | void remove_rec(const Observation& p,
|
---|
75 | std::map<int,std::vector<distNodePair> >& coverSets,
|
---|
76 | int level,
|
---|
77 | bool& multi);
|
---|
78 |
|
---|
79 | public:
|
---|
80 | double base;
|
---|
81 |
|
---|
82 | /**
|
---|
83 | * Constructs a cover tree which begins with all points in points.
|
---|
84 | *
|
---|
85 | * maxDist should be the maximum distance that any two points
|
---|
86 | * can have between each other. IE p.distance(q) < maxDist for all
|
---|
87 | * p,q that you will ever try to insert. The cover tree may be invalid
|
---|
88 | * if an inaccurate maxDist is given.
|
---|
89 | */
|
---|
90 |
|
---|
91 | Covertree(const double& maxDist,
|
---|
92 | const std::vector<Observation>& points=std::vector<Observation>());
|
---|
93 | ~Covertree();
|
---|
94 |
|
---|
95 | /**
|
---|
96 | * Just for testing/debugging. Returns true iff the cover tree satisfies the
|
---|
97 | * the covering tree invariants (every node in level i is greater than base^i
|
---|
98 | * distance from every other node, and every node in level i is less than
|
---|
99 | * or equal to base^i distance from its children). See the cover tree
|
---|
100 | * papers for details.
|
---|
101 | */
|
---|
102 | bool isValidTree() const;
|
---|
103 |
|
---|
104 | /**
|
---|
105 | * Insert newPoint into the cover tree. If newPoint is already present,
|
---|
106 | * (that is, newPoint==p for some p already in the tree), then the tree
|
---|
107 | * is unchanged. If p.distance(newPoint)==0.0 but newPoint!=p, then
|
---|
108 | * newPoint WILL be inserted and both points may be returned in k-nearest-
|
---|
109 | * neighbor searches.
|
---|
110 | */
|
---|
111 | void insert(const Observation& newObservation);
|
---|
112 |
|
---|
113 | /**
|
---|
114 | * Remove point p from the cover tree. If p is not present in the tree,
|
---|
115 | * it will remain unchanged. Otherwise, this will remove exactly one
|
---|
116 | * point q from the tree satisfying p==q.
|
---|
117 | */
|
---|
118 | void remove(const Observation& p);
|
---|
119 |
|
---|
120 | /**
|
---|
121 | * Returns the k nearest points to p in order (the 0th element of the vector
|
---|
122 | * is closest to p, 1th is next, etc). It may return greater than k points
|
---|
123 | * if there is a tie for the kth place.
|
---|
124 | */
|
---|
125 | std::vector<Observation> kNearestNeighbors(const Observation& p, const unsigned int& k) const;
|
---|
126 |
|
---|
127 | int get_numberofobs();
|
---|
128 |
|
---|
129 | CoverTreeNode* getRoot() const;
|
---|
130 |
|
---|
131 | /**
|
---|
132 | * Print the cover tree.
|
---|
133 | */
|
---|
134 | void print() const;
|
---|
135 | };
|
---|
136 | #endif //_COVERTREE_H
|
---|