Структура узла: р>
Код: Выделить всё
struct node2
{
int key;
list sons;
};
Код: Выделить всё
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;
}
Подробнее здесь: https://stackoverflow.com/questions/791 ... ush-back-c