0

I am making a class to represent a LCRS tree, and I'm having difficulty with my search function. Here's what I've got so far:

lcrs.h:

    using namespace std;
    #include <cstdlib>
    #include <iostream>

    class node{
            public:
            int data;
            node *right;
            node *below;
    };

    class lcrs{
            public:
            int height;
            node *root;
            bool search(int);
            void print(lcrs);
            void insert(int);
            void del(int);

            lcrs()
             {
                    root = NULL;
            }
    };

And this is lcrs.cpp:

using namespace std;
#include "lcrs.h"

bool lcrs::search(int x)
{
        if(root == NULL)
                return false;
        else
        {
                if(root->data == x)
                        return true;
                else
                {
                while(right != NULL && below != NULL)
                {
                        if(right->data == x || below->data == x)
                                return true;
                }
                return false;
                }
        }
}

This is my error message:

    lcrs.cpp: In member function ‘bool lcrs::search(int)’:
    lcrs.cpp:21:26: error: ‘below’ was not declared in this scope
    lcrs.cpp:23:15: error: request for member ‘data’ in ‘std::right’, which is of non-class type ‘std::ios_base&(std::ios_base&)’

I understand that I cannot access members of "right" and "below" without first creating an object, but is there another way for me to access them? I'm simply trying to see what is in "data". I do not see how to do this without first instantiating a node.

Your help is greatly appreciated.

2
  • Since your lcrs::search() method is a member of the lcrs class and not a method in the node class, when accessing "right" or "below" you need to specify which node you are talking about, e.g. specify "root->right->data" or "root->below->data" rather than just "right->data" or "below->data". Commented Nov 12, 2012 at 23:50
  • @Jeremy But I want to check all of the nodes. How would I specify which nodes if I am unsure of how many nodes will be in this object? Commented Nov 12, 2012 at 23:54

1 Answer 1

1

Are you trying to do this with recursion? You need to have your current node as a function argument.

Is this what you're trying to do (in pseudocode)?

search(int x, node* current)
    if node == NULL return false
    else
    if node->data == x return true
    else
        return search(x, node->right) || search(x, node->below);

Edit: You would call this by starting it with search(x, root).

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, that was just what I've been looking for. It compiles! Now to see if it actually does what I want it to do.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.