diff --git a/src/mlpack/core/tree/rectangle_tree/rectangle_tree.hpp b/src/mlpack/core/tree/rectangle_tree/rectangle_tree.hpp index 2d60f392c9..77021cdffb 100644 --- a/src/mlpack/core/tree/rectangle_tree/rectangle_tree.hpp +++ b/src/mlpack/core/tree/rectangle_tree/rectangle_tree.hpp @@ -144,6 +144,27 @@ class RectangleTree const size_t minNumChildren = 2, const size_t firstDataIndex = 0); + /** + * Construct this as the root node of a rectangle tree type using the given + * dataset, and taking ownership of the given dataset. + * + * @param data Dataset from which to create the tree. + * @param maxLeafSize Maximum size of each leaf in the tree. + * @param minLeafSize Minimum size of each leaf in the tree. + * @param maxNumChildren The maximum number of child nodes a non-leaf node may + * have. + * @param minNumChildren The minimum number of child nodes a non-leaf node may + * have. + * @param firstDataIndex The index of the first data point. UNUSED UNLESS WE + * ADD SUPPORT FOR HAVING A "CENTERAL" DATA MATRIX. + */ + RectangleTree(MatType&& data, + const size_t maxLeafSize = 20, + const size_t minLeafSize = 8, + const size_t maxNumChildren = 5, + const size_t minNumChildren = 2, + const size_t firstDataIndex = 0); + /** * Construct this as an empty node with the specified parent. Copying the * parameters (maxLeafSize, minLeafSize, maxNumChildren, minNumChildren, diff --git a/src/mlpack/core/tree/rectangle_tree/rectangle_tree_impl.hpp b/src/mlpack/core/tree/rectangle_tree/rectangle_tree_impl.hpp index cbf870ef85..f8650cb0d5 100644 --- a/src/mlpack/core/tree/rectangle_tree/rectangle_tree_impl.hpp +++ b/src/mlpack/core/tree/rectangle_tree/rectangle_tree_impl.hpp @@ -56,6 +56,45 @@ RectangleTree(const MatType& data, root->InsertPoint(i); } +template +RectangleTree:: +RectangleTree(MatType&& data, + const size_t maxLeafSize, + const size_t minLeafSize, + const size_t maxNumChildren, + const size_t minNumChildren, + const size_t firstDataIndex) : + maxNumChildren(maxNumChildren), + minNumChildren(minNumChildren), + numChildren(0), + children(maxNumChildren + 1), // Add one to make splitting the node simpler. + parent(NULL), + begin(0), + count(0), + maxLeafSize(maxLeafSize), + minLeafSize(minLeafSize), + bound(data.n_rows), + splitHistory(bound.Dim()), + parentDistance(0), + dataset(new MatType(std::move(data))), + ownsDataset(true), + points(maxLeafSize + 1), // Add one to make splitting the node simpler. + localDataset(new MatType(arma::zeros(data.n_rows, + maxLeafSize + 1))) +{ + stat = StatisticType(*this); + + // For now, just insert the points in order. + RectangleTree* root = this; + + for (size_t i = firstDataIndex; i < data.n_cols; i++) + root->InsertPoint(i); +} + template(3, 1000); + typedef RTree TreeType; + + TreeType tree(std::move(dataset)); + + BOOST_REQUIRE_EQUAL(dataset.n_elem, 0); + BOOST_REQUIRE_EQUAL(tree.Dataset().n_rows, 3); + BOOST_REQUIRE_EQUAL(tree.Dataset().n_cols, 1000); +} + BOOST_AUTO_TEST_SUITE_END();