| mistabob (29) | |||||||
|
Hello, I'm writing a binary search tree for a school project, but whenever I try to call insert(), I get an access violation. I don't know why. I have test cout statements written into insert, but none of them get triggered. Could someone explain what I'm doing wrong? My code, BST.h:
And here's Node.h:
and finally ,here's how I'm calling insert:
I would very much appreciate any help! Thank you! | |||||||
|
|
|||||||
| Peter87 (3691) | |
| When the tree is empty root is NULL but in the insert function you assume that root is not NULL. | |
|
|
|
| freddy92 (191) | |||
You declare newNode and then immediately try to set data for it. I don't see you allocating space for it anywhere. | |||
|
|
|||
| mistabob (29) | |||
|
@Peter87, Since it's initially NULL, am I allowed to later change root from NULL to something else? i.e, is it necessary to initialize root to NULL, since I allready include a check for if the BST is empty? @freddy92, I didn't think about that at all, does this fix the problem:
| |||
|
Last edited on
|
|||
| freddy92 (191) | |
|
Yes, I think that that fixes the problem I posted, but Peter's is still there. I think you could fix it by testing if (root == NULL) before you try anything else, and if it's true then just declare a new node, set its data to the parameter, and assign it to root. Been a while since I worked with trees so hopefully what I'm saying is right. Edit: actually, you didn't quite fix the problem. it needs to be newNode = new Node<T>;. I just removed the "*". You don't want a pointer to a node, you want an actual node.
| |
|
Last edited on
|
|
| mistabob (29) | |||
Ok, would this fix it? I removed the check for both left and right being NULL, since that would be redundant:
| |||
|
|
|||
| mistabob (29) | |||||
|
Awesome, it did fix it! Thank you! I moved on to find(), and find isn't working either. I insert something, and find won't... find it. Any ideas? Note: there's a find in BST and a find in Node, with the method in Node doing all the heavy lifting. I'm also getting another NULL dereference somewhere, I'm looking for that as well. BST find:
Node find:
| |||||
|
Last edited on
|
|||||
| freddy92 (191) | |||
|
That looks good, assuming insertHelper does what I'm thinking it does. Does it give you an error now? Edit: woops, that was a late response to your previous post. Edit2: Gonna repost my edit from my last post cause that still needs fixed: Edit: actually, you didn't quite fix the problem. it needs to be newNode = new Node<T>;. I just removed the "*". You don't want a pointer to a node, you want an actual node. This would also need fixed for isempty now too. It should make sense that it needs to be new Node<T> instead of new Node <T>*, because you don't want your pointer to a Node to point to a pointer to a Node, you want it to point to an actual Node. Hope that mess of a sentence made sense. Ok, now as for find(), you have 2 cases where nothing is being returned. I think it should be changed to this:
Edit again: You actually have more cases where nothing is returned. You should have an "else" at the end of your 2nd if...else if block that covers not finding the data at all (which will surely happen sometimes). | |||
|
Last edited on
|
|||
| mistabob (29) | |||
Yep, that was necessary;
I still have no idea about my find function though, it doesn't work at all, but the algorithm's straight from the textbook. EDIT: saw your edits | |||
|
Last edited on
|
|||
| freddy92 (191) | |||
I changed your find function a bit and I think it may work now:
OK, so without the first 2 returns I added, any recursive call to find would be pretty pointless, because whenever you got back to the one that called it, it wouldn't do anything with the found data. It needs to keep returning it until we get back to the initial caller of find(). As for the 3rd return, we need to have a case for if the data was not found at all. As I said in the comment, -1 only works as a default for ints, and only if we only want positive ones, so it isn't a very good solution. Since this tree is a template, there could be any data type in it, and you would need to return a default value for it. One solution to this would be to accept a default value as part of the constructor, then just return that if nothing was found. This gives the client the power of deciding what will be returned when nothing is found, so they will know how to check for that. Edit: Is that what return T(); is? If so that's pretty convenient, and I didn't know you could do it that way. | |||
|
Last edited on
|
|||
| mistabob (29) | |||
|
Ok, wouldn't your code work if, instead of returning a -1, return a default type, like you said? in other words:
EDIT: wait, the case where the data isn't found is allready covered. Each time it returns a T() is when it reaches the end of the relevant sub tree and encounters a NULL pointer, i.e. nothing more to search through. | |||
|
Last edited on
|
|||
| freddy92 (191) | |
|
Yeah, I had no idea you could use T(); like that. When we did stuff like this in my class a few years ago, we never did that. Does that actually work? What gets returned? Edit in response to your edit: that may be the case, but many compilers will be angry if there's a possible way for it to get through the function without finding a return. Since your last if...else if doesn't have an else, it's gonna get pretty sad about that and probably give you an error. | |
|
Last edited on
|
|
| mistabob (29) | |
|
To be honest I'm not sure, one of the reasons I'm having trouble in my own class is we get told to do stuff like this without any explanation. That, and online submission, which is literally the opposite of the bee's knees. Speaking of which, I don't suppose you see another NULL pointer de-reference anywhere? Because Web-Cat does.... | |
|
|
|
| freddy92 (191) | |
| I don't have any more time now, but I threw your code into codepad and have been testing it there; here's the current error it's finding: http://codepad.org/PfZnrXoA. It again is reaching the end of a function without returning anything. You need to make sure you will return something in ALL cases, even if logically it is covered. Once you fix that it should point out your null pointer problem, which I think is in remove. If not, keep trying your other functions until it gives you errors. | |
|
|
|
| mistabob (29) | |
| Hey, thanks so much! | |
|
|
|
| mistabob (29) | |||
So it looks like my final problem is in remove(). I'm not reattaching the nodes above the node I remove properly. Here's the code:
| |||
|
|
|||