コード例 #1
0
ファイル: bt.hpp プロジェクト: Mr-Anderson/james_mst_hw
BT<generic>::BT( BT & y)
{
	m_size = 0 ;
	m_root = NULL;
	
	Btn<generic> * temp ;
	
	PreOrder k ;

	for ( k = y.pre_begin() ; k != y.pre_end() ; k++)
	{
		i_give_up_insert (*k,k.current()->balance) ;
	}
	i_give_up_insert (*k,k.current()->balance) ;
	
}
コード例 #2
0
ファイル: bt.hpp プロジェクト: Mr-Anderson/james_mst_hw
BT<generic> & BT<generic>:: operator= (const BT & y)
{
	
	clear ();
	
	
	Btn<generic> * temp ;
	
	PreOrder k ;

	for ( k = y.pre_begin() ; k != y.pre_end() ; k++)
	{
		i_give_up_insert (*k,k.current()->balance) ;
	}
	i_give_up_insert (*k,k.current()->balance) ;
	
	
	
	return *this ;
	
}
コード例 #3
0
ファイル: bt.hpp プロジェクト: mhs294/CPP
////////////////////////////////////////////////////////////////////// 
/// @fn operator= (const BT & x) 
/// @brief Constructs an object of type BT that is a duplicate of x in 
/// terms of size and contents
/// @pre x is a valid BT
/// @post m_size = x.size()
/// @param x BT to be copied onto target BT
/// @return copy of BT on right side of operator
////////////////////////////////////////////////////////////////////// 
BT<generic>& BT<generic>::operator= (const BT & x)
{
	PreOrder i;
	string path = "";
	Btn<generic> * temp;
	Btn<generic> * temp2;
	m_root = NULL;
	m_size = 0;
	for(i = x.pre_begin(); i != x.pre_end(); i++)
	{	
		if(empty() == true) //root of BT
		{
			temp = new Btn<generic>;
			temp -> l = NULL;
			temp -> r = NULL;
			temp -> p = NULL;
			temp -> data = new generic(* i);
			m_root = temp;
			m_size++;
		}
		else
		{
			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 = i.current();
			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;
				}
			}
			if(temp2 -> p -> l == temp2) //left-child of parent
			{
				temp2 = NULL;
				temp2 = new Btn<generic>;
				temp2 -> l = NULL;
				temp2 -> r = NULL;
				temp2 -> p = temp;
				temp2 -> data = new generic(* i);
				temp -> l = temp2;
				m_size++;
			}
			else if(temp2 -> p -> r == temp2) //right-child of parent
			{
				temp2 = NULL;
				temp2 = new Btn<generic>;
				temp2 -> l = NULL;
				temp2 -> r = NULL;
				temp2 -> p = temp;
				temp2 -> data = new generic(* i);
				temp -> r = temp2;
				m_size++;
			}			
		}
		path = "";
	}
	i = x.pre_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 = i.current();
	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;
		}
	}
	if(temp2 -> p -> l == temp2) //left-child of parent
	{
		temp2 = NULL;
		temp2 = new Btn<generic>;
		temp2 -> l = NULL;
		temp2 -> r = NULL;
		temp2 -> p = temp;
		temp2 -> data = new generic(* i);
		temp -> l = temp2;
		m_size++;
	}
	else if(temp2 -> p -> r == temp2) //right-child of parent
	{
		temp2 = NULL;
		temp2 = new Btn<generic>;
		temp2 -> l = NULL;
		temp2 -> r = NULL;
		temp2 -> p = temp;
		temp2 -> data = new generic(* i);
		temp -> r = temp2;
		m_size++;
	}
	return (* this);
}