views:

103

answers:

2

I'm trying to create a recurring struct method in my code to walk through a binary search tree. But I'm getting errors on compile and unsure what is the cause.

I've got Node* findNode(const Node *, const Object &); in the private section of the .h file

and

Node* BSTree::findNode(const Node* current, const Object &target){
if(*current->item == target)
    return current;

Node* temp = NULL;

if(current->nodeLeft != NULL){
    temp = findNode(current->nodeLeft, target);

    if(temp != NULL)
        return temp;
}

if(current->nodeRight != NULL){
    temp = findNode(current->nodeRight, target);

    if(temp != NULL)
        return temp;
}
return NULL;

}

in the cpp.

I'm generating the following errors:

-error C2143: syntax error : missing ';' before '*'
-error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
-error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
-error C2556: 'int *BSTree::findNode(const BSTree::Node *,const Object &)' : overloaded function differs only by return type from 'BSTree::Node *BSTree::findNode(const BSTree::Node *,const Object &)'

The compiler errors are all pointing at the first line of the code in the cpp. I tried looking up the error codes, but I didn't find anything that answered my question as to the cause.

What is causing the errors and why is my compiler reading it at 'int BSTree' not Node* BSTree? Am I making a syntax error or forgetting an include? Currently I just have included iostream and fstream.

My thanks in advance for anyone who takes the time to read this.

Edit:

To answer Colin's question.

I have #include "BSTree.h" in my .cpp

And in the .h I have:

 #ifndef BSTREE_H  
 #define BSTREE_H  

 #include <iostream>  
 #include <fstream>  
+3  A: 

Judging from the error it looks like you have declared Node within the BSTree struct. I think your issue lies in your return type. Try declaring the return type to be BSTree::Node*.

linuxuser27
… which is to say, `BSTree::Node* BSTree::findNode(const Node* current`
Potatoswatter
Ah, that fixed it. Thank you. And thank you for the clarification Potatoswatter
Moniker
+2  A: 

You've already gotten an answer to the question you asked. I'll add just a bit about how I'd write the code. I prefer something like this:

Node* BSTree::findNode(const Node* current, const Object &target){
    if (current == NULL)
        return NULL;

    if (target < current->item)
        return findNode(current->left, target);
    if (current->item < target)
        return findNode(current->right, target);
    return current;
}

This (potentially) continues recursing until current == NULL, rather than attempting to stop when current->left == NULL, or current->right == NULL, depending on which direction is chosen. Although that can save one level of recursion, it requires duplicating almost all the logic for recursion into the left and right branches to do so. Unless you're really sure recursion is quite expensive, checking for the current node being NULL lets you simplify the code by consolidating the two. This version of the code also have the advantage that (like most C++ containers) it only requires Object to define operator<, not operator==.

If you want to go a step further, you can put the pointers to the left and right branches in an array, and simplify the code even more:

// presumes BSTree::Node is defined something like this:
class BSTree {
    class Node { 
        // subnodes[0] == left subtree
        // subnodes[1] == right subtree
        Node subnodes[2];
    };
};

BSTree::Node* BSTree::findNode(const Node* current, const Object &target){
    if (current == NULL)
        return NULL;

    if (current->item == target) 
        return current;

    return findNode(subnodes[target < current->item]);
}
Jerry Coffin