Я пытаюсь преобразовать представление многопутевого дерева из родительского массива в представление узла, где каждый узел имеет ключ и список дочерних элементов.
Структура узла: р>
node2* transformToMultiway(int parent[], int n)
{
node2* root = nullptr;
// Create nodes and link children
for (int i = 1; i < n; ++i) // Index from 1 to n because we don't use key 0
{
// Create the node for the current index if it doesn't exist
if (r2Const[i] == nullptr)
{
r2Const[i] = (node2*)calloc(1, sizeof(node2));
r2Const[i]->key = i;
}
// If the node has a parent
if (parent[i] != -1)
{
// Create the parent node if it doesn't exist
if (r2Const[parent[i]] == nullptr)
{
r2Const[parent[i]] = (node2*)calloc(1, sizeof(node2));
r2Const[parent[i]]->key = parent[i];
}
// Add current node as a son of its parent
r2Const[parent[i]]->
sons.push_back(r2Const[i]); // Segfaults here
}
else
{
// Else this is the root
root = r2Const[i];
}
}
return root;
}
Родительский вектор объявляется и инициализируется глобально, так же, как r2Const[]. При отладке r2Const не имеет значения nullptr.
Я пытаюсь преобразовать представление многопутевого дерева из родительского массива в представление узла, где каждый узел имеет ключ и список дочерних элементов. Структура узла: р> [code]struct node2 { int key; list sons; }; [/code] И моя функция: [code]node2* transformToMultiway(int parent[], int n) { node2* root = nullptr;
// Create nodes and link children for (int i = 1; i < n; ++i) // Index from 1 to n because we don't use key 0 { // Create the node for the current index if it doesn't exist if (r2Const[i] == nullptr) { r2Const[i] = (node2*)calloc(1, sizeof(node2)); r2Const[i]->key = i; }
// If the node has a parent if (parent[i] != -1) { // Create the parent node if it doesn't exist if (r2Const[parent[i]] == nullptr) { r2Const[parent[i]] = (node2*)calloc(1, sizeof(node2)); r2Const[parent[i]]->key = parent[i]; }
// Add current node as a son of its parent
r2Const[parent[i]]-> sons.push_back(r2Const[i]); // Segfaults here } else { // Else this is the root root = r2Const[i]; } } return root; } [/code] Родительский вектор объявляется и инициализируется глобально, так же, как r2Const[]. При отладке r2Const[i] не имеет значения nullptr.