//- Recursively find members intersecting this range box (called by "find") template <class Z> void KDDTree<Z>::recursive_find ( KDDTreeNode<Z> *rect_tree, const CubitBox &range_box, DLIList <Z> &range_members ) { //// Check for overlap with the safety box if ( ! range_box.overlap (myTolerance, rect_tree->safetyBox) ) { return; // no overlap, return } //// Check for overlap with the bounding box if ( range_box.overlap (myTolerance, rect_tree->boundingBox) ) { if (rect_tree->valid == CUBIT_TRUE) { range_members.append (rect_tree->data); // append the data to the list. } } if (rect_tree->left != NULL) { recursive_find (rect_tree->left, range_box, range_members); } if (rect_tree->right != NULL) { recursive_find (rect_tree->right, range_box, range_members); } return; }
OcrBSTNode * OcrBST::Find(const Occurrence & v) const { if(root != NULL) return recursive_find(root, v); else return NULL; }
/* * The back-end for find. Recursively locates a range * of values that includes the target, and returns the range. * If an exact match is found, its index is stored in the * 3rd location of the array returned (this is -1 otherwise). * * NOTE: The array returned MUST be freed. */ int* recursive_find(PMA* pma, int elem, int b_lim, int t_lim){ int index = -1; int cur; if (t_lim - b_lim <= 1){ return make_triple(b_lim, t_lim, -1); } cur = (b_lim + t_lim) / 2; cur = closest_in_range(pma, cur, b_lim, t_lim); if (pma->array[cur] == elem || cur == -1){ return make_triple(b_lim, t_lim, cur); }else if (pma->array[cur] > elem){ return recursive_find(pma, elem, b_lim, cur); } else{ return recursive_find(pma, elem, cur, t_lim); } }
OcrBSTNode * OcrBST :: recursive_find(OcrBSTNode * current_node, const Occurrence & v) const { int compare_result; compare_result = v.compare(current_node->GetValue()); if(compare_result < 0) { //go left if(current_node->GetLeft() == NULL) { return NULL; } else { return recursive_find(current_node->GetLeft(), v); } } else if(compare_result > 0) { //go right if(current_node->GetRight() == NULL) { return NULL; } else { return recursive_find(current_node->GetRight(), v); } } else { return current_node; } }
template <class Z> MY_INLINE CubitStatus RStarTree<Z>::recursive_find(RStarTreeNode<Z> *rect_tree, const CubitBox &range_box, DLIList <Z> &range_members ) { CubitBox rect_box = rect_tree->bounding_box(); if ( !range_box.overlap(myTolerance, rect_box ) ) return CUBIT_SUCCESS; //Now see if this is a data member. If it is, append the data to the //list. if (rect_tree->is_data() ) { range_members.append(rect_tree->get_data()); return CUBIT_SUCCESS; } //Now if this is anything else we need to keep iterating... int loop_size = rect_tree->num_children(); //We are doing a depth-first search of the tree. Not //all branches will need to be followed since they won't //all overlap... int ii; RStarTreeNode<Z> *curr_node; CubitStatus stat; for ( ii = 0; ii < loop_size; ii++ ) { curr_node = rect_tree->get_child(ii); if ( curr_node == NULL ) { PRINT_ERROR("Problems finding boxes in range.\n"); assert(curr_node != NULL); return CUBIT_FAILURE; } stat = recursive_find(curr_node, range_box, range_members); if ( stat != CUBIT_SUCCESS ) return stat; } return CUBIT_SUCCESS; }
//- Find members intersecting this range box template <class Z> CubitStatus KDDTree<Z>::find (const CubitBox &range_box, DLIList <Z> &range_members) { //// If the Add List is not empty, action must be taken if (myAddList.size() > 0) { if (mySelfBalancingOn == CUBIT_TRUE) // self-balancing is on, so rebalance the tree { balance (); } else // self-balancing is off, so put everything in the Add List onto the tree { dump_list (); } } //// Find all of the members of the tree that intersect this range_box if (root != NULL) { recursive_find (root, range_box, range_members); } return CUBIT_SUCCESS; }
/* * Finds an element in the packed memory array. * If exact is 1, attempts to find the exact element and * returns -1 on failure. If exact is 0, attempts to find * a place to insert the element. */ int find(PMA* pma, int elem, int exact){ int* range = recursive_find(pma, elem, -1, pma->size); int index, next; if (exact){ index = range[2]; } else{ /* If an exact match was found, find the next used slot and put it between the two. Otherwise, place it in the middle of the range */ if (range[2] != -1){ next = next_greater(pma, range[2]); /* Ensure that next is not -1 */ next = (next == -1) ? pma->size - 1 : next; index = (range[2] + next) / 2; } else{ index = (range[0] + range[1]) / 2; } } free(range); return index; }