[mlpack-svn] r14362 - mlpack/trunk/src/mlpack/core/tree/cover_tree

fastlab-svn at coffeetalk-1.cc.gatech.edu fastlab-svn at coffeetalk-1.cc.gatech.edu
Thu Feb 21 17:23:20 EST 2013


Author: rcurtin
Date: 2013-02-21 17:23:20 -0500 (Thu, 21 Feb 2013)
New Revision: 14362

Modified:
   mlpack/trunk/src/mlpack/core/tree/cover_tree/cover_tree.hpp
   mlpack/trunk/src/mlpack/core/tree/cover_tree/cover_tree_impl.hpp
Log:
Add Parent() function.


Modified: mlpack/trunk/src/mlpack/core/tree/cover_tree/cover_tree.hpp
===================================================================
--- mlpack/trunk/src/mlpack/core/tree/cover_tree/cover_tree.hpp	2013-02-21 22:04:35 UTC (rev 14361)
+++ mlpack/trunk/src/mlpack/core/tree/cover_tree/cover_tree.hpp	2013-02-21 22:23:20 UTC (rev 14362)
@@ -121,6 +121,8 @@
    * @param base Base to use during tree building.
    * @param pointIndex Index of the point this node references.
    * @param scale Scale of this level in the tree.
+   * @param parent Parent of this node (NULL indicates no parent).
+   * @param parentDistance Distance to the parent node.
    * @param indices Array of indices, ordered [ nearSet | farSet | usedSet ];
    *     will be modified to [ farSet | usedSet ].
    * @param distances Array of distances, ordered the same way as the indices.
@@ -135,6 +137,7 @@
             const double base,
             const size_t pointIndex,
             const int scale,
+            CoverTree* parent,
             const double parentDistance,
             arma::Col<size_t>& indices,
             arma::vec& distances,
@@ -154,6 +157,7 @@
    * @param pointIndex Index of the point in the dataset which this node refers
    *      to.
    * @param scale Scale of this node's level in the tree.
+   * @param parent Parent node (NULL indicates no parent).
    * @param parentDistance Distance to parent node point.
    * @param furthestDescendantDistance Distance to furthest descendant point.
    */
@@ -161,6 +165,7 @@
             const double base,
             const size_t pointIndex,
             const int scale,
+            CoverTree* parent,
             const double parentDistance,
             const double furthestDescendantDistance);
 
@@ -262,6 +267,11 @@
   //! Returns true: this tree does have self-children.
   static bool HasSelfChildren() { return true; }
 
+  //! Get the parent node.
+  CoverTree* Parent() const { return parent; }
+  //! Modify the parent node.
+  CoverTree*& Parent() { return parent; }
+
   //! Get the distance to the parent.
   double ParentDistance() const { return parentDistance; }
   //! Modify the distance to the parent.
@@ -292,6 +302,9 @@
   //! The instantiated statistic.
   StatisticType stat;
 
+  //! The parent node (NULL if this is the root of the tree).
+  CoverTree* parent;
+
   //! Distance to the parent.
   double parentDistance;
 

Modified: mlpack/trunk/src/mlpack/core/tree/cover_tree/cover_tree_impl.hpp
===================================================================
--- mlpack/trunk/src/mlpack/core/tree/cover_tree/cover_tree_impl.hpp	2013-02-21 22:04:35 UTC (rev 14361)
+++ mlpack/trunk/src/mlpack/core/tree/cover_tree/cover_tree_impl.hpp	2013-02-21 22:23:20 UTC (rev 14362)
@@ -25,6 +25,7 @@
     dataset(dataset),
     point(RootPointPolicy::ChooseRoot(dataset)),
     base(base),
+    parent(NULL),
     parentDistance(0),
     furthestDescendantDistance(0)
 {
@@ -62,8 +63,8 @@
   size_t childFarSetSize = (dataset.n_cols - 1) - childNearSetSize;
   size_t usedSetSize = 0;
   children.push_back(new CoverTree(dataset, base, point, scale - 1,
-      0, indices, distances, childNearSetSize, childFarSetSize, usedSetSize,
-      *metric));
+      this, 0, indices, distances, childNearSetSize, childFarSetSize,
+      usedSetSize, *metric));
 
   furthestDescendantDistance = children[0]->FurthestDescendantDistance();
 
@@ -118,7 +119,7 @@
       size_t childNearSetSize = 0;
       size_t childFarSetSize = 0;
       children.push_back(new CoverTree(dataset, base,
-          indices[0], scale - 1, distances[0], indices, distances,
+          indices[0], scale - 1, this, distances[0], indices, distances,
           childNearSetSize, childFarSetSize, usedSetSize, *metric));
 
       // And we're done.
@@ -146,8 +147,8 @@
     childUsedSetSize = 1; // Mark self point as used.
     childFarSetSize = ((nearSetSize - 1) - childNearSetSize);
     children.push_back(new CoverTree(dataset, base, indices[0],
-        scale - 1, distances[0], childIndices, childDistances, childNearSetSize,
-        childFarSetSize, childUsedSetSize, *metric));
+        scale - 1, this, distances[0], childIndices, childDistances,
+        childNearSetSize, childFarSetSize, childUsedSetSize, *metric));
 
     // If we created an implicit node, take its self-child instead (this could
     // happen multiple times).
@@ -194,6 +195,7 @@
     const double base,
     const size_t pointIndex,
     const int scale,
+    CoverTree* parent,
     const double parentDistance,
     arma::Col<size_t>& indices,
     arma::vec& distances,
@@ -205,6 +207,7 @@
     point(pointIndex),
     scale(scale),
     base(base),
+    parent(parent),
     parentDistance(parentDistance),
     furthestDescendantDistance(0)
 {
@@ -231,14 +234,16 @@
     // This should not modify farSetSize or usedSetSize.
     size_t tempSize = 0;
     children.push_back(new CoverTree(dataset, base, pointIndex,
-        INT_MIN, 0, indices, distances, 0, tempSize, usedSetSize, metric));
+        INT_MIN, this, 0, indices, distances, 0, tempSize, usedSetSize,
+        metric));
 
     // Every point in the near set should be a leaf.
     for (size_t i = 0; i < nearSetSize; ++i)
     {
       // farSetSize and usedSetSize will not be modified.
       children.push_back(new CoverTree(dataset, base, indices[i],
-          INT_MIN, 0, indices, distances, 0, tempSize, usedSetSize, metric));
+          INT_MIN, this, distances[i], indices, distances, 0, tempSize,
+          usedSetSize, metric));
       usedSetSize++;
     }
 
@@ -267,7 +272,7 @@
   size_t childFarSetSize = nearSetSize - childNearSetSize;
   size_t childUsedSetSize = 0;
   children.push_back(new CoverTree(dataset, base, pointIndex,
-      nextScale, 0, indices, distances, childNearSetSize, childFarSetSize,
+      nextScale, this, 0, indices, distances, childNearSetSize, childFarSetSize,
       childUsedSetSize, metric));
 
   // The self-child can't modify the furthestChildDistance away from 0, but it
@@ -335,7 +340,7 @@
     {
       size_t childNearSetSize = 0;
       children.push_back(new CoverTree(dataset, base,
-          indices[0], nextScale, distances[0], indices, distances,
+          indices[0], nextScale, this, distances[0], indices, distances,
           childNearSetSize, farSetSize, usedSetSize, metric));
 
       // Because the far set size is 0, we don't have to do any swapping to
@@ -375,8 +380,8 @@
     // Build this child (recursively).
     childUsedSetSize = 1; // Mark self point as used.
     children.push_back(new CoverTree(dataset, base, indices[0],
-        nextScale, distances[0], childIndices, childDistances, childNearSetSize,
-        childFarSetSize, childUsedSetSize, metric));
+        nextScale, this, distances[0], childIndices, childDistances,
+        childNearSetSize, childFarSetSize, childUsedSetSize, metric));
 
     // If we created an implicit node, take its self-child instead (this could
     // happen multiple times).
@@ -421,12 +426,14 @@
     const double base,
     const size_t pointIndex,
     const int scale,
+    CoverTree* parent,
     const double parentDistance,
     const double furthestDescendantDistance) :
     dataset(dataset),
     point(pointIndex),
     scale(scale),
     base(base),
+    parent(parent),
     parentDistance(parentDistance),
     furthestDescendantDistance(furthestDescendantDistance)
 {
@@ -441,12 +448,16 @@
     scale(other.scale),
     base(other.base),
     stat(other.stat),
+    parent(other.parent),
     parentDistance(other.parentDistance),
     furthestDescendantDistance(other.furthestDescendantDistance)
 {
   // Copy each child by hand.
   for (size_t i = 0; i < other.NumChildren(); ++i)
+  {
     children.push_back(new CoverTree(other.Child(i)));
+    children[i]->Parent() = this;
+  }
 }
 
 template<typename MetricType, typename RootPointPolicy, typename StatisticType>




More information about the mlpack-svn mailing list