I am really new to c++ ... (Came from Java and C# so I'm really used to everything being handled as a reference). If someone could help me understand my problem that would be awesome. I'm trying to create your basic BST (OOD structured using classes)
I am having tons of trouble with my Add function in Tree.cpp but I'll post all my code.
The problem is that I am just not comprehending the differences of reference Vs. pointer and how assigning values to them works. Any help with this mess would be great
I am having tons of trouble with my Add function in Tree.cpp but I'll post all my code.
Code:
//Node.h
class Node
{
private:
char* data;
public:
Node();
Node(char* incPtr);
~Node();
char* getData();
int CompareTo(Node* nodePtr);
bool isEqualTo(Node* nodePtr);
bool isGreaterThan(Node* nodePtr);
bool isLessThan(Node* nodePtr);
void setData(char* change);
void ToString();
};
//Node.cpp
#include <iostream>
#include "string.h"
#include "Node.h"
using namespace std;
Node::Node()
{
data = NULL;
}
Node::Node(char* incPtr)
{
data = incPtr;
}
Node::~Node()
{
delete[](data);
}
char* Node::getData()
{
return(data);
}
void Node::setData(char *change)
{
data = change;
}
int Node::CompareTo(Node *nodePtr)
{
char* nodeData = nodePtr->data;
return(strcmp(data, nodeData));
}
bool Node::isEqualTo(Node* nodePtr)
{
return( (CompareTo(nodePtr) == 0 ) );
}
bool Node::isGreaterThan(Node* nodePtr)
{
return( (CompareTo(nodePtr) > 0 ) );
}
bool Node::isLessThan(Node* nodePtr)
{
return( (CompareTo(nodePtr) < 0 ) );
}
void Node::ToString()
{
size_t size = strlen(data);
int pos = 0;
while (data[pos] != NULL)
{
cout << data[pos];
pos++;
}
cout << endl;
}
Code:
//TreeNode.h
#include "Node.h"
class TreeNode
{
private:
Node* nodeData;
TreeNode* left;
TreeNode* right;
public:
TreeNode();
TreeNode(Node* incPtr);
~TreeNode();
Node* getNode();
TreeNode* getLeft();
TreeNode* getRight();
bool hasLeft();
bool hasRight();
int CompareTo(TreeNode* treeNodePtr);
bool isEqualTo(TreeNode* treeNodePtr);
bool isGreaterThan(TreeNode* treeNodePtr);
bool isLessThan(TreeNode* treeNodePtr);
void setNodeData(Node* change);
void setLeft(TreeNode* change);
void setRight(TreeNode* change);
};
//TreeNode.cpp
#include <iostream>
#include "TreeNode.h"
TreeNode::TreeNode()
{
nodeData = NULL;
left = NULL;
right = NULL;
}
TreeNode::TreeNode(Node* incPtr)
{
nodeData = incPtr;
left = NULL;
right = NULL;
}
TreeNode::~TreeNode()
{
delete(nodeData);
delete(left);
delete(right);
}
Node* TreeNode::getNode()
{
return(nodeData);
}
TreeNode* TreeNode::getLeft()
{
return(left);
}
TreeNode* TreeNode::getRight()
{
return(right);
}
bool TreeNode::hasLeft()
{
return( (left != NULL) );
}
bool TreeNode::hasRight()
{
return( (right != NULL) );
}
bool TreeNode::isEqualTo(TreeNode* treeNodePtr)
{
return( CompareTo(treeNodePtr) == 0 );
}
bool TreeNode::isGreaterThan(TreeNode* treeNodePtr)
{
return( CompareTo(treeNodePtr) > 0 );
}
bool TreeNode::isLessThan(TreeNode* treeNodePtr)
{
return( CompareTo(treeNodePtr) < 0 );
}
int TreeNode::CompareTo(TreeNode* treeNodePtr)
{
Node* incNode = treeNodePtr->getNode();
return( nodeData->CompareTo(incNode) );
}
void TreeNode::setNodeData(Node* change)
{
nodeData = change;
}
void TreeNode::setLeft(TreeNode* change)
{
left = change;
}
void TreeNode::setRight(TreeNode* change)
{
right = change;
}
Code:
//Tree.h
#include "TreeNode.h"
class Tree
{
private:
TreeNode* root;
public:
Tree();
Tree(TreeNode* incPtr);
Tree(char* incData);
~Tree();
TreeNode* Add(TreeNode* root,TreeNode* incNodePtr);
TreeNode* Add(char* incData);
bool Find(TreeNode* top, char* data);
TreeNode* getRoot();
void setRoot(TreeNode* change);
void printPrefix(TreeNode* top);
void printEnfix(TreeNode* top);
void printPostfix(TreeNode* top);
};
//Tree.cpp
#include <iostream>
#include "Tree.h"
using namespace std;
Tree::Tree()
{
root = NULL;
}
Tree::Tree(TreeNode* incPtr)
{
root = incPtr;
}
Tree::Tree(char* incData)
{
root = NULL;
Node* tempNode = new Node(incData);
TreeNode* tempTreeNode = new TreeNode(tempNode);
root = Add(root, tempTreeNode);
}
Tree::~Tree()
{
delete(root);
}
TreeNode* Tree::Add(TreeNode* tempRoot, TreeNode* incTreeNodePtr)
{
if (tempRoot == NULL)
{
*tempRoot = *incTreeNodePtr;
/*cout << "root = " << endl;
cout << "temp = " << tempRoot << endl;
cout << "temp * = " << *tempRoot << endl;
cout << "temp *& = " << *&tempRoot << endl;
root->getNode()->ToString();*/
}
else
{
if (tempRoot->hasLeft())
{
if ( (root->isGreaterThan(incTreeNodePtr)) ||
(root->isEqualTo(incTreeNodePtr)) )
{
//TreeNode* temp =
*tempRoot = *Add(tempRoot->getLeft(), incTreeNodePtr);;
}
}
if (tempRoot->hasRight())
{
if (tempRoot->isLessThan(incTreeNodePtr))
{
//TreeNode* temp=
*tempRoot = *Add(tempRoot->getRight(), incTreeNodePtr);
}
}
}
return(tempRoot);
}
TreeNode* Tree::Add(char* incData)
{
Node* tempNode = new Node(incData);
TreeNode* tempTreeNode = new TreeNode(tempNode);
Add(root, tempTreeNode);
return(root);
}
bool Tree::Find(TreeNode* top, char* data)
{
bool flag = false;
if (top != NULL)
{
Node* tempNode = new Node(data);
TreeNode* tempTreeNode = new TreeNode(tempNode);
if (top->hasLeft())
{
if (top->isGreaterThan(tempTreeNode))
Find(top->getLeft(), data);
}
else if (top->hasRight())
{
if (top->isLessThan(tempTreeNode))
Find(top->getRight(), data);
}
else if (top->isEqualTo(tempTreeNode))
flag = true;
delete(tempNode);
delete(tempTreeNode);
}
return(flag);
}
void Tree::printEnfix(TreeNode* top)
{
if (top != NULL)
{
printEnfix(top->getLeft());
top->getNode()->ToString();
printEnfix(top->getRight());
}
}
TreeNode* Tree::getRoot()
{
return(root);
}
void Tree::setRoot(TreeNode* change)
{
delete(root);
root = change;
}
Code:
//Driver.cpp
#include <iostream>
#include "Tree.h"
using namespace std;
int main()
{
Tree tree;
tree.Add("Matt");
tree.Add("Kyle");
tree.Add("Stacey");
tree.printEnfix(tree.getRoot());
char t;
cin >> t;
}
The problem is that I am just not comprehending the differences of reference Vs. pointer and how assigning values to them works. Any help with this mess would be great