arr1,arr2 are two arrays of pointers, I have to merge these into arr3. When the program gets arr3[1] (when k=1) the program is closes , I don't get why.
Please help.
class Node {
public:
Node* left;
T data;
Node* right;
int height;
};
void mergeArrays(Node<T>** arr1,Node<T>** arr2,int len1,int len2){
int p=0,q=0,k=0;
Node<T>** arr3 = new Node<T>*[len1+len2];
while ( p < len1 && q < len2) {
if ((arr1[p])->data< (arr2[q])->data) {
(arr3[k++])->data = (arr1[p++])->data;
} else {
(arr3[k++])->data = (arr2[q++])->data;
}
}
while ( p < len1) {
(arr3[k++])->data = (arr1[p++])->data;
}
while ( q < len2) {
(arr3[k++])->data = (arr2[q++])->data;
}
}
cout << arr3[c] << " ";, since all that does is drop a pointer intocoutunless you have some secret operator overload buried the the code you're not showing us (but should be, as part of a proper minimal reproducible example).datadata-member. Otherwise, if you intend to implement a deep-copy, then do a separate heap-allocation for each element ofarr3usingnewusing a suitable constructor that takesdataas an argument.std::merge? It is quite flexible, less bug-prone, easier to maintain and possibly faster (not to mention there is now a parallel implementation available).