I am experiencing a strange error with the constructor for a binary search tree. The project is for an Hoffman Encoding scheme however the issue is whenever i insert more than two items into the STL priority queue, the copy constructor crashes.
void copyTree(myTreeNode* & copy,myTreeNode* originalTree)
{
cout<<"Copy tree was just called"<<endl;
if(originalTree==NULL)
{
copy=NULL;
}
else
{
copy=new myTreeNode();
//the constructor crashes on the third insertion here at this point
copy->data=originalTree->data;
copyTree(copy->left, originalTree->left);
copyTree(copy->right,originalTree->right);
}
}
myTree (const myTree & copy)
{
this->root=NULL;
this->copyTree(this->root, copy.root);
}
//main
priority_queue<myTree, vector<myTree>, treeCompare> treeQueue;
map<char, int> occurrenceTracker;
char character;
cin>>character;
occurrenceTracker[character]=1;
while(cin>>character&&character!='e')
{
if(occurrenceTracker.find(character)!=occurrenceTracker.end())
{
occurrenceTracker[character]++;
}
else
{
occurrenceTracker[character]=1;
}
}
map<char,int>::iterator itr;
for(itr=occurrenceTracker.begin();itr!=occurrenceTracker.end();itr++)
{
myTree characterTree;
treeNodeClass mapItem(itr->first,itr->second);
characterTree.insert(mapItem);
treeQueue.push(characterTree);
}