I'm using a binary tree, and each time I insert into it, it says that there is nothing there. here is my code:
this is only the insert method. Does this look right, or did I do something wrong?
Code:
#ifndef BST_H
#define BST_H
#include <stdlib.h>
class Node{
int data;
public:
Node(int d){data = d; left = NULL; right = NULL;}
~Node(){}
Node * left;
Node * right;
int getData() {return data;}
};
class BST{
private:
Node * top;
void cleanup(Node * h);
public:
BST(){top=NULL;}
~BST(){cleanup(top);}
int Insert(const int& data);
int iinsert(Node * t, const int& data);
};
void BST::cleanup(Node * t)
{
if(t != NULL)
{
cleanup(t->left);
cleanup(t->right);
delete t;
}
}
int BST::Insert(const int& data)
{
return iinsert(top, data);
}
int BST::iinsert(Node * t, const int& data)
{
if(t == NULL)
{
t = new Node(data);
return 1;
}
else if(data == t->getData())
{
return 0;
}
else if(data < t->getData())
{
return iinsert(t->right, data);
}
else if(data > t->getData())
{
return iinsert(t->right, data);
}
}
#endif
Comment