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.
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)?
Edit: You would call this by starting it with
search(x, root).