Applied Reviewed Changes

This commit is contained in:
Manish
2018-02-11 11:42:28 +05:30
parent 9d7d14f658
commit 2d4a81fe47
3 changed files with 30 additions and 31 deletions
@@ -40,8 +40,8 @@ class GreedySingleTreeTraverser
//! Get the number of prunes.
size_t NumPrunes() const { return numPrunes; }
//! Set value of k.
void K(size_t K) { k = K; }
//! Set value of minBaseCases.
void MinBaseCases(size_t baseCases) { minBaseCases = baseCases; }
private:
//! Reference to the rules with which the tree will be traversed.
@@ -50,9 +50,9 @@ class GreedySingleTreeTraverser
//! The number of nodes which have been pruned during traversal.
size_t numPrunes;
//! The number of results required. For example number of nearest
//! neighbours in case of knn.
size_t k;
//! The number of base cases required. For example the number of nearest
//! neighbours(k) in case of knn.
size_t minBaseCases;
};
} // namespace tree
@@ -25,7 +25,7 @@ GreedySingleTreeTraverser<TreeType, RuleType>::GreedySingleTreeTraverser(
RuleType& rule) :
rule(rule),
numPrunes(0),
k(0)
minBaseCases(0)
{ /* Nothing to do. */ }
template<typename TreeType, typename RuleType>
@@ -33,38 +33,37 @@ void GreedySingleTreeTraverser<TreeType, RuleType>::Traverse(
const size_t queryIndex,
TreeType& referenceNode)
{
if(referenceNode.IsLeaf())
{
// Run the base case as necessary for all the points in the reference node.
for (size_t i = 0; i < referenceNode.NumPoints(); ++i)
rule.BaseCase(queryIndex, referenceNode.Point(i));
return;
}
// Run the base case as necessary for all the points in the reference node.
for (size_t i = 0; i < referenceNode.NumPoints(); ++i)
rule.BaseCase(queryIndex, referenceNode.Point(i));
size_t bestChild = rule.GetBestChild(queryIndex, referenceNode);
size_t numDescendants;
// Check that referencenode is not a leaf node while calculating number of
// descendants of it's best child.
if(!referenceNode.IsLeaf())
if (!referenceNode.IsLeaf())
numDescendants = referenceNode.Child(bestChild).NumDescendants();
else
numDescendants = referenceNode.NumPoints();
// If number of descendants are more than k than we can go along with
// best child otherwise we need to traverse for each descendant to
// ensure that we get at least k nearest neighbors..
if (numDescendants > k)
// If number of descendants are more than minBaseCases than we can go along
// with best child otherwise we need to traverse for each descendant to
// ensure that we calculate at least minBaseCases number of base cases.
if (!referenceNode.IsLeaf())
{
// We are prunning all but one child.
numPrunes += referenceNode.NumChildren() - 1;
// Recurse the best child.
Traverse(queryIndex, referenceNode.Child(bestChild));
}
else
{
for (size_t i = 0; i < referenceNode.NumChildren(); ++i)
Traverse(queryIndex, referenceNode.Child(i));
if (numDescendants > minBaseCases)
{
// We are prunning all but one child.
numPrunes += referenceNode.NumChildren() - 1;
// Recurse the best child.
Traverse(queryIndex, referenceNode.Child(bestChild));
}
else
{
for (size_t i = 0; i < referenceNode.NumDescendants(); ++i)
rule.BaseCase(queryIndex, referenceNode.Descendant(i));
}
}
}
@@ -662,8 +662,8 @@ DualTreeTraversalType, SingleTreeTraversalType>::Search(
// Create the traverser.
tree::GreedySingleTreeTraverser<Tree, RuleType> traverser(rules);
// Set the value of K.
traverser.K(k);
// Set the value of minBaseCases.
traverser.MinBaseCases(k);
// Now have it traverse for each point.
for (size_t i = 0; i < querySet.n_cols; ++i)
@@ -962,8 +962,8 @@ DualTreeTraversalType, SingleTreeTraversalType>::Search(
// Create the traverser.
tree::GreedySingleTreeTraverser<Tree, RuleType> traverser(rules);
// Set the value of K.
traverser.K(k);
// Set the value of minBaseCases.
traverser.MinBaseCases(k);
// Now have it traverse for each point.
for (size_t i = 0; i < referenceSet->n_cols; ++i)