我寫C++代碼:如何在函數正確執行後修復Core Dumped錯誤?
#include <iostream>
#include <queue>
#include <stack>
#include <vector>
#include <unordered_map>
#include <algorithm>
using namespace std;
template <class T>
class TreeNode {
public:
T data;
TreeNode<T> *left, *right;
TreeNode() {
data = {};
left = right = NULL;
}
TreeNode(T data) {
this->data = data;
}
};
template <class T>
class BinaryTree {
public:
TreeNode<T> *root;
vector<T> _largestIndependentSet(TreeNode<T> *root) {
static unordered_map< TreeNode<T>*, vector<T> > table;
if(!root)
return {};
if(table.find(root) != table.end())
return table[root];
vector<T> lis = {}, lis_left = {}, lis_right = {},
lis_nrl_left = {}, lis_nrl_right = {}, lis_nrr_left = {}, lis_nrr_right = {};
// Leaf
if(!root->left && !root->right) {
lis.push_back(root->data);
}else{
if(root->left){
lis_left = _largestIndependentSet(root->left);
lis_nrl_left = _largestIndependentSet(root->left->left);
lis_nrl_right = _largestIndependentSet(root->left->right);
}
if(root->right){
lis_right = _largestIndependentSet(root->right);
lis_nrr_left = _largestIndependentSet(root->right->left);
lis_nrr_right = _largestIndependentSet(root->right->right);
}
if( lis_left.size() + lis_right.size() >
lis_nrl_left.size() + lis_nrl_right.size() +
lis_nrr_left.size() + lis_nrr_right.size() + 1 ){ // don't keep root
lis.insert(lis.end(), lis_left.begin(), lis_left.end());
lis.insert(lis.end(), lis_right.begin(), lis_right.end());
}
else {
lis.insert(lis.end(), lis_nrl_left.begin(), lis_nrl_left.end());
lis.insert(lis.end(), lis_nrl_right.begin(), lis_nrl_right.end());
lis.insert(lis.end(), lis_nrr_left.begin(), lis_nrr_left.end());
lis.insert(lis.end(), lis_nrr_right.begin(), lis_nrr_right.end());
lis.push_back(root->data);
}
}
cout<<"Calculated Results for: "<<root->data<<": ";
for_each(lis.begin(), lis.end(), [](T data) {
cout<<data<<" ";
});
cout<<"\n";
table[root] = lis;
return table[root];
}
void largestIndependentSet() {
vector<T> lis = _largestIndependentSet(this->root);
for_each(lis.begin(), lis.end(), [](T data) {
cout<<data<<" ";
});
}
};
int main() {
BinaryTree<int> bt;
TreeNode<int> *root = new TreeNode<int>(10);
root->left = new TreeNode<int>(7);
root->right = new TreeNode<int>(15);
root->left->left = new TreeNode<int>(9);
root->left->right = new TreeNode<int>(12);
root->right->left = new TreeNode<int>(6);
root->right->right = new TreeNode<int>(11);
root->left->left->left = new TreeNode<int>(20);
root->right->left->right = new TreeNode<int>(5);
root->left->left->left->left = new TreeNode<int>(22);
root->left->left->left->right = new TreeNode<int>(21);
root->right->left->right->left = new TreeNode<int>(4);
root->right->left->right->right = new TreeNode<int>(3);
bt.root = root;
bt.largestIndependentSet();
return 0;
}
我它Cygwin
使用g++ 5.4.0
編譯:
g++ binary_tree.cpp -std=c++11
的問題是遞歸函數_largestIndependentSet()
完成後,最後打印給我正確的答案。但之後,我得到這個錯誤:中止(核心轉儲),並在largestIndependentSet()
打印不執行。
這是莫名其妙的,因爲我的邏輯似乎是正確的。這是什麼造成的?
PS:如果我有c++14
標誌編譯運行良好O_O:
g++ binary_tree.cpp -std=c++14
您需要展示如何創建添加到地圖的指針。'vector v = foo(some_apple);'沒有足夠的信息。如果你正在存儲指向本地/臨時對象的指針,那麼這些指針在某個時刻將會失效。 –
NathanOliver
您是否嘗試用調試器逐步執行代碼? –
@NathanOliver指針已經存在(這是一個自定義二叉樹)。該地圖用於存儲附加信息以及矢量。我確信載體不會超出範圍。 – prakharsingh95