Advertisement
Guest User

Untitled

a guest
Mar 24th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. template <int Dim>
  2. Point<Dim> KDTree<Dim>::findNearestNeighbor(const Point<Dim>& query) const
  3. {
  4. /**
  5. * @todo Implement this function!
  6. */
  7. KDTreeNode* oof = findNearestNeighbor(this->root, this->root, query, 0);
  8. std::cout<<oof->point<<std::endl;
  9. return oof->point;
  10. }
  11. template <int Dim>
  12. typename KDTree<Dim>::KDTreeNode* KDTree<Dim>::findNearestNeighbor(KDTreeNode* root, KDTreeNode* currBest, const Point<Dim>& query, int dim) const
  13. {
  14. /**
  15. * @todo Implement this function!
  16. */
  17. Point<Dim> best = root->point;
  18. if((root->left == NULL && root->right == NULL)){
  19. return root;
  20. }
  21. if(root->point == query)
  22. return root;
  23.  
  24. else if(smallerDimVal(query, root->point, dim)){
  25. if(root->left != NULL)
  26. currBest = findNearestNeighbor(root->left, currBest, query, (dim+1)%Dim);
  27. else
  28. currBest = root;
  29. //second step of the john
  30. if(shouldReplace(query, currBest->point, best))
  31. {
  32. currBest = root;
  33. }
  34. if((root->point[dim] - query[dim])*(root->point[dim] - query[dim]) <= dist(query, currBest->point) && root->right != NULL){
  35. KDTreeNode* newNode = findNearestNeighbor(root->right, currBest, query, (dim+1) % Dim);
  36. if(shouldReplace(query, currBest->point, newNode->point))
  37. currBest = newNode;
  38. }
  39. }
  40. else{
  41. if(root->right != NULL)
  42. currBest = findNearestNeighbor(root->right, currBest, query, (dim+1)%Dim);
  43. else
  44. currBest = root;
  45. //second step of the john
  46. if(shouldReplace(query, currBest->point, best))
  47. {
  48. currBest = root;
  49. }
  50. if((root->point[dim] - query[dim])*(root->point[dim] - query[dim]) <= dist(query, currBest->point) && root->left != NULL){
  51. KDTreeNode* newNode = findNearestNeighbor(root->left, currBest, query, (dim+1) % Dim);
  52. if(shouldReplace(query, currBest->point, newNode->point))
  53. currBest = newNode;
  54. }
  55. }
  56. return currBest;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement