Quote (DeadlySanity @ Apr 3 2013 04:13pm)
Wow, nevermind. My find_node and delete_node function are WAYYY off base. I really need to stop coding at 4am without sleep...
Mods can close this thread.
In case anyone actually read this thread and was interested, here is the fixed code:Code
template< typename T >
inline void Node< T >::find_node( Node< T > *&root, const T &val)
{
if( val < _value ) {
if( _left ) {
_left->find_node( _left, val ); //go left if val < node
}
} else if( val > _value ) {
if( _right ) {
_right->find_node( _right, val ); //go right if val > node
}
} else {
delete_node( root ); //if not > or < must be ==
}
}
template< typename T >
inline void Node< T >::delete_node( Node< T > *&p )
{
Node< T > *temp; //safe pointer to use instead of *&
if( !p->_left ) { //if p has no left (or is leaf)
temp = p;
p = p->_right;
delete temp;
} else if( !p->_right ) { //if p has no right
temp = p;
p = p->_left;
delete temp;
} else { //if p has two children
temp = p->_right;
while( temp->_left ) { //find p's successor (the left...
temp = temp->_left; //... most node to the right of p)
}
temp->_left = p->_left; //point p's successor to p's left
temp = p;
p = p->_right;
delete temp; //delete what p pointed to
}
}
This post was edited by DeadlySanity on Apr 3 2013 06:55pm