示例#1
0
void BT<generic>::clear ()
{
	PostOrder k ;
	Btn<generic> * temp ;
	
	if (!empty ())
	{
		
		for ( k = post_begin() ; k != post_end() ; )
		{
			temp = k.current() ;
			k++ ;
			//cerr << *(temp->data) << endl ;
			delete temp->data ;
			delete temp->instance ;
			delete temp;
		}
		
		temp = k.current() ;
		//cerr << *(temp->data) << endl ;
		delete temp->data ;
		delete temp->instance ;
		delete temp;

		
		m_root = NULL;
		m_size = 0 ;
	}
	
}
示例#2
0
文件: bt.hpp 项目: mhs294/CPP
////////////////////////////////////////////////////////////////////// 
/// @fn clear () 
/// @brief Deletes all values in binary search tree
/// @pre (none)
/// @post m_size = 0
/// @return void 
//////////////////////////////////////////////////////////////////////
void BT<generic>::clear ()
{
	if(empty() == true)
	{
		return;
	}
	PostOrder i;
	string path = "";
	Btn<generic> * temp;
	Btn<generic> * temp2;
	i = (* this).post_begin();
	while(i != (* this).post_end())
	{			
		temp2 = i.current();
		while(temp2 -> p != NULL)
		{
			if(temp2 -> p -> l == temp2)
			{
				path += "l";
				temp2 = temp2 -> p;
			}
			else if(temp2 -> p -> r == temp2)
			{
				path += "r";
				temp2 = temp2 -> p;
			}
		}
		temp2 = NULL;
		temp = m_root;
		for(int j = (path.length() - 1); j >= 0 ; j--)
		{
			if(path[j] == 'l')
			{
				temp = temp -> l;
			}
			else if(path[j] == 'r')
			{
				temp = temp -> r;
			}
		}
		i++;
		if(temp -> p -> l != NULL)
		{
			if(temp -> p -> l == temp) //left-child of parent
			{
				temp -> p -> l = NULL;
				delete temp -> data;
				delete temp;
				temp = NULL;
				m_size--;
			}
		}
		else if(temp -> p -> r != NULL)
		{
			if(temp -> p -> r == temp) //right-child of parent
			{
				temp -> p -> r = NULL;
				delete temp -> data;
				delete temp;
				temp = NULL;
				m_size--;
			}
		}
		path = "";
	}
	i = (* this).post_end();
	temp = m_root;
	delete temp -> data;
	delete temp;
	m_root = NULL;
	m_size--;
	temp = NULL;
}