Missing a line from the output
I'm not too sure why I'm missing a line from the output. I've been going over my code like crazy and something seems to cause a line which would be size = 0: [nothing here] missing in my output. Here's my code:
Code:
class binSTree: public binTree<T>{
public:
void insert(const T& x); //insert node with value x
bool search(const T& x); //searches leaf with value x
bool remove(const T& x); //removes leaf with value x
private: //implemented recursively
void insert(binTreeNode<T>*&, const T&); //private version of insert
bool search(binTreeNode<T>*, const T&) const; //private version of search
void remove(binTreeNode<T>*&, const T&); //private version of remove
bool leaf(binTreeNode<T>* node) const; //checks if node is leaf
};
//public
template <class T>
void binSTree<T>::insert(const T& x)
{
insert(this->root, x);
}
template <class T>
bool binSTree<T>::search(const T& x)
{
return search(this->root, x);
}
template <class T>
bool binSTree<T>::remove(const T& x)
{
//search(x);
/*if( this->size() == 0 )
{
this->root = NULL;
return false;
}*/
if( this->size() > 1 )
{
if( search(x) )
remove( this->root, x );
return true;
}
else
{
return false;
}
}
/**********************************
private
***********************************/
template <class T>
void binSTree<T>::insert(binTreeNode<T>*& p, const T& x)
{
if (p == NULL)
{
binTreeNode<T>* newNode;
newNode = new binTreeNode<T>(x);
p = newNode;
}
else
{
if (x < p -> data)
insert(p -> left, x);
if (x > p -> data)
insert(p -> right, x);
}
}
template <class T>
bool binSTree<T>::search(binTreeNode<T>*p, const T& x) const
{
//x is data value of a leaf to be searched for
if( p == NULL ) return false;
else
{
if(x == p->data)
{
if( leaf(p) ) return true;
else return false;
}
if( x < p->data )
{
return search( p->left, x );
}
else
{
return search( p->right, x );
}
}
}//end search
template <class T>
void binSTree<T>::remove(binTreeNode<T>*& p, const T& x)
{
binTreeNode<T>* curr;
binTreeNode<T>* parent;
curr = p;
while( curr != NULL )
{
if( curr->data == x )
{
break;
}
else
{
parent = curr;
if( x > curr->data ) curr = curr->right;
else curr = curr->left;
}
}
if( curr != NULL )
{
if( parent->right == curr ) parent->right = NULL;
else parent->left = NULL;
delete curr;
curr = 0;
}
}
template <class T>
bool binSTree<T>::leaf(binTreeNode<T>* node) const
{
if(node->left == NULL && node->right == NULL)
return true;
else
return false;
}
Other files are included but they are 100% correct as I used them in my past couple of assignments. The public version of remove is suppose to call the search function and determine the result of the search for a leaf with value x. Calls private remove and returns true. Otherwise it returns false if the search isn't successful. Any tips?