I'm going to guess that it starts failing after you try to delete nodes.
1 2 3 4 5 6
|
else if (p -> right == nullptr)
{
tempP = p;
tempP = p-> left;
delete tempP;
}
| |
What are you trying to do here, in English?
Say what each line is doing out loud to yourself
(like, actually, say it out loud to yourself -- do it), along with the
intended purpose of each line). And then tell us.
You aren't deleting p, that's for sure. You also aren't restructuring your tree after the deletion; the parent will be looking at junk data, as far as I can tell.
Questions to ask yourself:
* Why do you assign tempP a value, and then immediately re-assign it? That's a red flag right there.
* Why do check for p->right being nullptr, but then assume that p->left isn't a nullptr? Seems like a weird setup.
Also, for bug testing, try inserting data that isn't
already ordered. That will help you hit the most conditional branches.
Most importantly, general advice:
Start small.
Don't start with 10 items, and then try to figure out what went wrong from the wreckage. Start with 1 item. Maybe 2 items. And see if that works. If 1 or 2 items works, try to insert 3 items. If there is a problem, it will be much easier to track by hand when you are only dealing with a few items.