I want a list of balanced binary search trees so that I can answer queries for insert, delete and median in each tree. Based on this post I coded the following:
struct Tree{
multiset<int> ms;
multiset<int>::iterator med_it;
int med_idx;
};
int Ceil(int a, int b){
return (a+1)/b;
}
void initializeTree(Tree (&tree), int el){
(tree.ms).insert(el);
(tree.med_it) = (tree.ms).begin();
(tree.med_idx) = 1;
}
void TreeInsert(Tree (&tree), int el){
(tree.ms).insert(el);
if(el < *(tree.med_it)) (tree.med_idx)++;
int idx = Ceil((tree.ms).size(),2);
if(tree.med_idx < idx) (tree.med_it)++;
else if(tree.med_idx > idx) (tree.med_it)--;
tree.med_idx = idx;
}
void TreeDelete(Tree (&tree), int el){
multiset<int>::iterator aux;
int idx = Ceil((int)(tree.ms).size() - 1,2);
if(el == *(tree.med_it)){
aux = (tree.med_it);
if((tree.med_idx) == idx) aux++;
else aux--;
(tree.ms).erase((tree.med_it));
(tree.med_it) = aux;
}
else{
aux = (tree.ms).find(el);
(tree.ms).erase(aux);
if(el < *(tree.med_it)) (tree.med_idx)--;
if((tree.med_idx) < idx) (tree.med_it)++;
else if((tree.med_idx) > idx) (tree.med_it)--;
}
(tree.med_idx) = idx;
}
int TreeMerge(Tree (&tree1), Tree (&tree2)){
multiset<int>::iterator it;
if((tree1.ms).size() >= (tree2.ms).size()){
for(it = (tree2.ms).begin(); it != (tree2.ms).end(); it++) TreeInsert(tree1, *it);
return 1;
}
else{
for(it = (tree1.ms).begin(); it != (tree1.ms).end(); it++) TreeInsert(tree2, *it);
return 2;
}
}
I tested all the functions and they seem to work well. But then I created a list L to be my list and inserted some trees on it to test the functions on the members of the list. The following code shows that:
Tree tree1, tree2;
initializeTree(tree1,9);
initializeTree(tree2,4);
list<Tree> L;
L.push_back(tree1);
L.push_back(tree2);
Then I acessed the members of the list with list::iterator as shown in the code below:
list<Tree>::iterator L_cur = L.begin(), L_next = L.begin();
L_next++;
So I applied the functions to *L_cur and *L_next and the med_it pointer got some bug. For example, when running the code below:
int merge = TreeMerge(*L_cur,*L_next);
When I access the content of the L_cur and L_next pointers all seems to work as expected except for the med_it pointer of the tree in L_cur, which continues pointing to 9 instead of 4.
When I run the same function with two Tree objects it seems to work well, as shown in the code below:
Tree tree1, tree2;
initializeTree(tree1,9);
initializeTree(tree2,4);
int merge = TreeMerge(tree1,tree2);
Why it occurs?
I'm not pretty sure (not compiler) but your $$$Tree$$$ is just a struct without explicitly defined copy/move constructor. That means your iterator will be copied too during push_back, which uses copy constructor.
Order of actions:
Thanks for answer. I run it here and tree1_copy.ms1_copy and tree2_copy.ms2_copy don't seem to be empty. As you can see, the problem is only with the med_it pointer. How could I correct it to run as expected?
Yes, my mistake: copy of nonempty multiset is nonempty too.
There are a lot of options:
And you should mark copy/move constructors of Tree as delete. Then compiler permit any move/copy.
Tree(Tree&& other) : ms(move(other.ms)), med_it(move(other.med_it)), med_idx(move(other.med_idx)) {}
and do:Tree(const Tree& other) : ms(other.ms) { med_idx = other.med_idx; med_it = next(ms.begin(), med_idx); }
then no edit of your code is needed.