How can I get one of my functions to return a pointer to a struct? I would like my function called insertHelper to return a pointer to a Node struct. I have tried to implement this function header as:
Node* insertHelper(Node *rootPointer, Node *nodePointer);
but when I try and compile, I get an error and am told that Node does not name a type. I have the Node struct defined in my .h file.
How can I get the above function to return a pointer to a Node?
Here is my .h file:
#ifndef Bintree_H
#define Bintree_H
#include "nodedata.h"
using namespace std;
class BinTree{ // you add class/method comments and assumptions
friend ostream& operator<<(ostream& out, const BinTree& T); //Used for output printing of BinTree objects
public:
BinTree(); // constructor
BinTree(const BinTree &); // copy constructor
~BinTree(); // destructor, calls makeEmpty
bool isEmpty() const; // true if tree is empty, otherwise false
void makeEmpty(); // make the tree empty so isEmpty returns true
BinTree& operator=(const BinTree &);
bool operator==(const BinTree &) const;
bool operator!=(const BinTree &) const;
bool insert(NodeData*);
bool retrieve(const NodeData &, NodeData* &) const; //I got these parameters from the PDF.
void displaySideways() const; // provided below, displays the tree sideways
int getHeight(const NodeData &) const;
void bstreeToArray(NodeData* []);
void arrayToBSTree(NodeData* []);
private:
struct Node {
NodeData* data; // pointer to data object
Node* left; // left subtree pointer
Node* right; // right subtree pointer
};
Node* root; // root of the tree
//Utility functions
void sideways(Node*, int) const; //provided below, helper for displaySideways()
//bool insertHelper(Node *rootPointer, Node *nodePointer);
Node* insertHelper(Node *rootPointer, Node *nodePointer);
void postOrderDeleteNode(const Node *node);
void inorderHelper(Node *startNode) const;
};
#endif
Here is the location in my .cpp file where I am trying to implement the function(ignore all the print statements that I use for debug):
Node* BinTree::insertHelper(Node *rootPointer, Node *nodePointer){
cout << "insertHelper function" << endl;
cout << "rootPointer: " << rootPointer << endl;
cout << "nodePointer: " << nodePointer << endl;
if(rootPointer==nullptr){
cout << "weve reached a base case in the insertHelper" << endl;
rootPointer = nodePointer;
}
else{
if(nodePointer->data <= root->data){
cout << "if statement" << endl;
insertHelper(root->left,nodePointer);
}
else if(nodePointer->data >= root->data){ //we go here if the NodeData being added is larger than the current node.
cout << "else if statement" << endl;
insertHelper(root->right, nodePointer);
}
}
}