1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
|
template <class Record>
void AVL_tree<Record>::range_search(const Record &r1, const Record &r2, vector<Record> &results) const {
recursive_range_search(this->root, r1, r2, results);
}
template <class Record>
Binary_node<Record>* AVL_tree<Record>::recursive_range_search(Binary_node<Record>* sub_root, const Record &r1,
const Record &r2, vector<Record> &results)const
{
int i = 0;
/* base case */
if ( sub_root == NULL)
return NULL;
/* Since the desired tree is sorted, recurse for left subtree first
If sub_root->data is greater than r1, then only we can get keys
in left subtree */
if ( r1 < sub_root->data )
{
sub_root = sub_root->left;
recursive_range_search(sub_root,r1, r2, results);
}
/* if sub_root's data lies in range, then prints sub_root's data */
if ( r1 <= sub_root->data && r2 >= sub_root->data )
{
std::cout << sub_root->data << endl;
results.push_back(sub_root->data);
//results[i] = sub_root->data;
std::cout << "I am target" << endl;
i++;
sub_root = sub_root->right;
recursive_range_search(sub_root,r1,r2,results);
}
/* If sub_root->data is smaller than r2, then only we can get keys
in right subtree */
if ( r2 > sub_root->data )
{
sub_root = sub_root->right;
recursive_range_search(sub_root,r1, r2, results);
}
return 0;
}
|