I don't understand... I thought that it would be functional based on the logic, and I was fairly careful with my syntax but it's still not working.
Sometimes values will compare to as being "equal" even though they're not. I'm using troolean compareTo method (an int, returns -1, 0 or 1) and if two values are equal the value isn't set in the tree but it's testing two values incorrectly at some times...
Furthermore the pointer temp in the insert method doesn't seem to be reassigning itself properly.
Can someone please help me understand my error?
#include <cstdlib>
#include <iostream>
using namespace std;
/*
*/
struct Base
{
};
template<typename T>
class Tree
{
private:
struct Node
{
friend class Tree;
Node *left;
Node *right;
T value;
Node(){left = 0; right = 0;};
T &getValue(){
return value;
};
void setValue(T &arg){value = arg;};
Node &getLeft(){return *left;};
Node &getRight(){return *right;};
bool setLeft(Node &l){
if(&l != 0){
left = &l;
return true;
}
else return false;
};
bool setRight(Node &r){
if(&r != 0){
right = &r;
return true;
}
else return false;
};
int compareTo(Node &other){
if(other.getValue() == getValue())
return 0;
else if(other.getValue() < getValue())
return 1;
else return -1;
}
};
Node *root;
void displayInOrder(Node &arg)
{
if(&arg == 0)
return;
displayInOrder(arg.getLeft());
std::cout << arg.getValue() << std::endl;
displayInOrder(arg.getRight());
}
void displayPreOrder(Node &arg)
{
if(&arg == 0)
return;
std::cout << arg.getValue() << std::endl;
displayInOrder(arg.getLeft());
displayInOrder(arg.getRight());
}
void displayPostOrder(Node &arg)
{
if(&arg == 0)
return;
displayInOrder(arg.getLeft());
displayInOrder(arg.getRight());
std::cout << arg.getValue() << std::endl;
}
public:
Tree(){root = 0;};
bool insert(T &value)
{
Node *myNode = new Node;
myNode->setValue(value);
if(&value != 0)
{
if(root == 0)
{
root = myNode;
return true;
}
else
{
cout << "Checking else statement..." << endl;
Node *temp = root;
while(&temp->getLeft() != 0 && &temp->getRight() != 0){
if(temp->compareTo(*myNode) == -1)
{
cout << "Shifting to the right of: " << temp->getValue() << endl;
temp = &temp->getRight();
}
else if(temp->compareTo(*myNode) == 1)
{
cout << "Shifting to the left of: " << temp->getValue() << endl;
temp = &temp->getLeft();
}
else
{
cout << "No duplicates allowed! Aborting." << endl;
delete myNode;
return false;
}
}
if(temp->compareTo(*myNode) == -1){
cout << "Setting next right node to: " << myNode->getValue() << endl;
temp->setRight(*myNode);
return true;
}
else if(temp->compareTo(*myNode) == 1){
cout << "Setting next left node to: " << myNode->getValue() << endl;
temp->setLeft(*myNode);
return true;
}
else{
cout << "No duplicates allowed! " << temp->getValue();
cout << " == "<< myNode->getValue() <<" -- Aborting." << endl;
delete myNode;
return false;
}
}
}
else
{
delete myNode;
return false;
}
}
void displayTree(char *condition)//this will be one complex method
{
// if(std::strcmp(condition, "Inorder"))
// {
std::cout << "Displaying Tree...\n" << std::endl;
displayPreOrder(*root);
// }
}
};
int main(int argc, char *argv[])
{
Tree<int> myTree;
int a = 10, b = 2, c = 11, d = 7, e = 3, f = 19;
myTree.insert(a);
myTree.insert(b);
myTree.insert(c);
myTree.insert(d);
myTree.insert(e);
myTree.insert(f);
myTree.insert(e);
char *con = "Inorder";
// std::cout << std::strcmp("") << std::endl;
myTree.displayTree(con);
cin.get();
return 0;
}