我遇到了二进制搜索树的构造函数的奇怪错误。该项目用于Hoffman编码方案,但问题是每当我向STL优先级队列插入两个以上的项目时,复制构造函数崩溃。
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);
}
我在调试器中完成了两个或更少的项目,一切都很好。复制构造函数被调用大约八次,我不确定这是否是完全正常的行为。这是程序的堆栈跟踪
Hoffman Encoding.exe!`main'::`4'::myTree::copyTree(main::__l3::myTreeNode * & copy, main::__l3::myTreeNode * originalTree) Line 195 + 0x8 bytes C++
Hoffman Encoding.exe!`main'::`4'::myTree::copyTree(main::__l3::myTreeNode * & copy, main::__l3::myTreeNode * originalTree) Line 197 C++
Hoffman Encoding.exe!`main'::`4'::myTree::myTree(const main::__l4::myTree & copy) Line 205 C++
Hoffman Encoding.exe!??$_Debug_lt_pred@VtreeCompare@?4?main@VmyTree@?3?main@V2?3?main@@std@@YA_NVtreeCompare@?4??main@@9@AAVmyTree@?3??2@9@1PB_WI@Z(main::__l5::treeCompare _Pred, main::__l4::myTree & _Left, main::__l4::myTree & _Right, const wchar_t * _File, unsigned int _Line) Line 674 + 0x14 bytes C++
Hoffman Encoding.exe!??$_Debug_heap@V?$_Deque_unchecked_iterator@VmyTree@?3?main@V?$allocator@VmyTree@?3?main@@std@@@std@@VtreeCompare@?4?main@@std@@YAXV?$_Deque_unchecked_iterator@VmyTree@?3?main@V?$allocator@VmyTree@?3?main@@std@@@0@0VtreeCompare@?4??main@@9@@Z(std::?$_Deque_unchecked_iterator@VmyTree@?3?main@V?$allocator@VmyTree@?3?main@@std@@ _First, std::?$_Deque_unchecked_iterator@VmyTree@?3?main@V?$allocator@VmyTree@?3?main@@std@@ _Last, main::__l5::treeCompare _Pred) Line 2386 + 0x26 bytes C++
Hoffman Encoding.exe!??$push_heap@V?$_Deque_iterator@VmyTree@?3?main@V?$allocator@VmyTree@?3?main@@std@@@std@@VtreeCompare@?4?main@@std@@YAXV?$_Deque_iterator@VmyTree@?3?main@V?$allocator@VmyTree@?3?main@@std@@@0@0VtreeCompare@?4??main@@9@@Z(std::?$_Deque_iterator@VmyTree@?3?main@V?$allocator@VmyTree@?3?main@@std@@ _First, std::?$_Deque_iterator@VmyTree@?3?main@V?$allocator@VmyTree@?3?main@@std@@ _Last, main::__l5::treeCompare _Pred) Line 2490 + 0x82 bytes C++
Hoffman Encoding.exe!?push@?$priority_queue@VmyTree@?3?main@V?$deque@VmyTree@?3?main@V?$allocator@VmyTree@?3?main@@std@@@std@@VtreeCompare@?4?main@@std@@QAEXABVmyTree@?3??main@@9@@Z(const main::__l4::myTree & _Val) Line 343 + 0x5b bytes C++
Hoffman Encoding.exe!main(int argv, char * * argc) Line 333 C++
Hoffman Encoding.exe!__tmainCRTStartup() Line 555 + 0x19 bytes C
Hoffman Encoding.exe!mainCRTStartup() Line 371 C
kernel32.dll!76dc3677()
[Frames below may be incorrect and/or missing, no symbols loaded for kernel32.dll]
ntdll.dll!772e9f02()
ntdll.dll!772e9ed5()
当我取消注释析构函数时,代码工作正常,任何想法为什么会发生这种情况?
析构函数在二进制树的树根节点上调用此函数
void destroy(myTreeNode* aTreeNode)
{
if(aTreeNode!=NULL)
{
this->destroy(aTreeNode->left);
this->destroy(aTreeNode->right);
delete aTreeNode;
}
}