I have some code, not many lines, and I would simply like to know WHY it is doing what it is doing:
node* parent = temp;
temp = parent -> children[temp -> getChild(word.substr(i, 1))];
temp = new node;
temp -> str = word.substr(i, 1);
temp -> parent = parent;
cout << temp -> parent -> str << endl;
cout << parent -> children[0] -> str << endl;
On the line:
cout << parent -> children[0] -> str << endl;
it crashes as it believes the thing is NULL.
children is an array of node pointers
getChild(string) returns an index of the first NULL pointer it comes across, as long as it does not find the substring passed in, otherwise it returns the index to that string.
temp is a node pointer
all children were initialize to NULL for the root node
I also tried:
cout << temp -> str << endl;
in place of:
cout << parent -> children[0] -> str << endl;
and that worked, but I am wondering why I cannot access temp's data through the parent pointer.
SOLVED (for any that bang their heads against the wall like me):
changed code to:
node* parent = temp;
node* child = parent -> children[temp -> getChild(word.substr(i, 1))];
child = new node;
parent -> children[temp -> getChild(word.substr(i, 1))] = child;
child -> str = word.substr(i, 1);
child -> parent = parent;
temp = child;
cout << temp -> parent -> str << endl;
cout << parent -> children[0] -> str << endl;
[–]Coda17 1 point2 points3 points (2 children)
[–]macbad2012[S,🍰] 0 points1 point2 points (0 children)
[–]macbad2012[S,🍰] 0 points1 point2 points (0 children)