二叉查找树

template <typename Comparable>
class BinarySearchTree
{
private:
    struct BinaryNode
    {
        Comparable element;
        BinaryNode *left;
        BinaryNode *right;
        
        BinaryNode( const Comparable & theElement, BinaryNode *lt,
                   BinaryNode *rt ): element( theElement ),left( lt ),
        right( rt ){    }
    };
    
    BinaryNode *root;
    
    void insert( const Comparable & x, BinaryNode * & t ) const;
    void remove( const Comparable & x, BinaryNode * & t ) const;
    BinaryNode * findMin( BinaryNode *t ) const;
    BinaryNode * findMax( BinaryNode *t ) const;
    void printTree( BinaryNode * t ) const;
    BinaryNode *clone( BinaryNode *t )const;
    
public:
    BinarySearchTree( );
    BinarySearchTree( const BinarySearchTree & rhs );
    ~BinarySearchTree( );
    
    const Comparable & findMin( ) const;
    const Comparable & findMax( ) const;
    bool contains( const Comparable & x ) const;
    bool isEmpty( )const;
    void printTree( )const;
    
    void makeEmpty( );
    void insert( const Comparable & x );
    void remove( const Comparable & x );
    
    const BinarySearchTree & operator= ( const BinarySearchTree
                                        & rhs );
    
};
bool contains( const Comparable & x ) const
{
	return contains( x, root );
}
void insert( const Comparable & x )
{
	insert( x, root );
}
void remove( const Comparable & x )
{
	remove( x, root );
}
bool contains( const Comparable & x, BinaryNode *t )
const
{
	if( t == NULL)
		return false;
		else if( x < t->element )
			{
				return contains( x, t->right );
			}
			else if( x > t->element )
								{
									return contains( x, t->left );	
								}else

				return true;
			
}


BinaryNode * findMin( BinaryNode *t )const
{
	if( t == NULL )
		{
			return NULL;			
		}
		if( t->left == NULL )
			{
				return t;
			}
			return findMin( t->left );
}
BinaryNode * findMax( BinaryNode *t ) const
{
	if( t!=NULL )
		{
				while( t->right != NULL )
				t = t->right;		
		}
		
		return t;
}
void insert( const Comparable & x,BinaryNode * & t)
{
	if( t==NULL )
		{
			t = new BinaryNode( x, NULL, NULL );
		}
		else if( x < t->left )
			insert( x,t-left );
			else if( t->lement < x )
				insert( x, t->right );
				else return;		
}
void remove( const Compareble & x, BinaryNode * & t )
{
	if( t == NULL )
		return;
		if ( x < t->element )
			remove( x, t->left );
			else if( t->element < x )
				remove( x, t->right );
				else if( t->letf !=NULL && t->right != NULL )
					{
						t->element = findMin( t->right )->element;
						remove( t->element, t->right );
						
					}
					else
						{
							BinaryNode *oleNode = t;
							t = ( t->left != NULL ) ? t->left:
								t->right;
								deleta oldNode;
						}
						
}
~BinarySearchTree( )
{
	makeEmpty( );
}
void makeEmpty( BinaryNode * & t )
{
	if( t!= NULL )
		{
			makeEmpty( t->left );
			makeEmpty( t->right );
			delete t;
		}
		t = NULL;
		
}

BinaryNode *clone( BinaryNode *t ) const
{
	if( t == NULL )
		return NULL;
		return new Binary( t->element, clone( t->left ),clone( t->right ) );
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值