I've been working on a binary search tree and I've written out most of what I think the code should look like but I can't test it because of an error I'm getting. I have no idea if what I have is even correct. My professor gave us the class, the structure, and the Main to use. My problem is in the getRoot(void) function. When I try compiling my code, it tells me that ptr is undeclared. I can't figure out what to change. I would appreciate it if someone could help me.
I don't know exactly what your professor gave you, but the code you have is probably not
going to get you where you want to go. You don't really need the 'findNode' or 'getRoot'
functions (methods).
Here's some commented code that should get you started on the right track.
I finally got it to build a tree using what he gave us. I did, however, end up changing one of his method declarations a bit. This project was just to build the tree. The next one we have to do actually adds on to what we had to do here; adding a destructor, deletion, and traversals. Well this is what I ended up with:
In the above thread, the OP has used recursion to insert an item in the list. (I assume that his 'professor' gave the OP this structure).
Is this an approved method? I generally use a while loop for that type of insertion. It seems to me that the recursion method would be incredibably resource intensive.
>Is this an approved method?
It depends on the programmer and the application.
>It seems to me that the recursion method would be incredibably resource intensive.
For a basic binary search tree, it can easily be. If the tree is suitably random then it should be appropriately balanced and recursion won't cause too much trouble. If the tree gets near any of the degenerate cases then the recursion could get too deep for the system to handle, and the application will break.
No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.