二叉查找树的类模板实现

二叉查找树的性质是,对于树中的每一个结点X,它的左子树中所有的项的值小于X中的项,而它的右子树中所有项的值大于X中的项。


#include <iostream>
using namespace std;

template <typename T>
class BinarySearch
{
public:
    BinarySearch();
    BinarySearch ( const BinarySearch & rhs );
    ~BinarySearch();

    const T &findMin() const;
    const T &findMax() const;
    bool contains ( const T &x ) const;
    bool isEmpty() const;
    void printTree() const;

    void makeEmpty();
    void insert ( const T &x );
    void remove ( const T &x );

    const BinarySearch &operator= ( const BinarySearch &rhs );
private:
    struct BinaryNode
    {
        T element;
        BinaryNode *left;
        BinaryNode *right;

        BinaryNode ( const T & e, BinaryNode *l , BinaryNode *r )
            : element ( e ), left ( l ), right ( r ) {}
    };

    BinaryNode *root; //头结点

    void insert ( const T & x, BinaryNode * &t ) ;
    void remove ( const T & x, BinaryNode * &t ) ;

    const T & findMin ( BinaryNode * t ) const;
    const T & findMax ( BinaryNode * t ) const;

    bool contain ( const T &x, BinaryNode *t ) const;
    void printtree ( BinaryNode *t ) const;
    void makeEmpty ( BinaryNode * &t );
    BinaryNode *clone ( BinaryNode *t ) const
    {
        if ( t == NULL )
            return NULL;
        return new BinaryNode ( t->element, clone ( t->left ), clone ( t->right ) );
    }
};

template<typename T>
BinarySearch<T>::BinarySearch()
{
    root = NULL;
}

template<typename T>
BinarySearch<T>::BinarySearch ( const BinarySearch &rhs )
{
    root = clone ( rhs.root );
}


template<typename T>
const BinarySearch<T> & BinarySearch<T>::operator= ( const BinarySearch &rhs )
{
    if ( this != &rhs )
    {
        makeEmpty();
        root = clone ( rhs.root );
    }
    return *this;
}


template<typename T>
BinarySearch<T>::~BinarySearch()
{
    makeEmpty();
}

template<typename T>
void BinarySearch<T>::makeEmpty()
{
    return makeEmpty ( root );
}

template<typename T>
void BinarySearch<T>::makeEmpty ( BinaryNode * &t )
{
    if ( t != NULL )
    {
        makeEmpty ( t->left );
        makeEmpty ( t->right );
        delete t;
    }
    t = NULL;
}
template <typename T>
void BinarySearch<T>::insert ( const T &x )
{
    insert ( x, root );
}

template <typename T>
void BinarySearch<T>::insert ( const T &x, BinaryNode * &t )
{
    if ( t == NULL )
        t = new BinaryNode ( x, NULL, NULL );
    else if ( x < t->element )
        insert ( x, t->left );
    else if ( t->element < x )
        insert ( x, t->right );
    else
        ;//x已经存在,不做处理
}

template <typename T>
bool BinarySearch<T>::contains ( const T &x ) const
{
    return contain ( x, root );
}

template <typename T>
bool BinarySearch<T>::contain ( const T &x, BinaryNode *t ) const
{
    if ( t == NULL )
        return false;
    else if ( x < t->element )
        return contain ( x, t->left );
    else if ( t->element < x )
        return contain ( x , t->right );
    else
        return true;
}


template <typename T>
void BinarySearch<T>::remove ( const T &x )
{
    remove ( x, root );
}

template<typename T>
void BinarySearch<T>::remove ( const T &x, BinaryNode  * &t )
{
    if ( t == NULL )
        return;
    else if ( x < t->element )
        remove ( x, t->left );
    else if ( t->element < x )
        remove ( x, t->right );
    else if ( t->left != NULL && t->right != NULL )
    {
        t->element = findMin ( t->right );
        remove ( t->element, t->right );
    }
    else
    {
        BinaryNode *old = t;
        t = ( t->left != NULL ) ? t->left : t->right;
        delete old;
    }
}

template<typename T>
const T & BinarySearch<T>::findMax() const
{
    return findMax ( root );
}

template<typename T>
const T & BinarySearch<T>::findMax ( BinaryNode *t ) const
{
    if ( t != NULL )
        while ( t->right != NULL )
            t = t->right;
    return t->element;
}


template<typename T>
const T & BinarySearch<T>::findMin() const
{
    return findMin ( root );
}

template<typename T>
const T & BinarySearch<T>::findMin ( BinaryNode *t ) const
{
    if ( t == NULL )
        return t->element;
    if ( t->left == NULL )
        return t->element;
    return findMin ( t->left );
}

template<typename T>
bool BinarySearch<T>::isEmpty() const
{
    if ( root == NULL )
        return true;
    else
        return false;
}

template<typename T>
void BinarySearch<T>::printTree() const
{
    printtree ( root );
    cout << endl;
}

template<typename T>
void BinarySearch<T>::printtree ( BinaryNode *t ) const
{
    /*前序输出*/
    //if ( t != NULL )
    //    cout << t->element << " ";
    //if ( t->left != NULL )
    //    printtree ( t->left );
    //if ( t->right != NULL )
    //    printtree ( t->right );

    /*中序输出*/
    if ( t->left != NULL )
    {
        printtree ( t->left );
    }
    cout << t->element << " ";
    if ( t->right != NULL )
        printtree ( t->right );

    /*后序输出*/
    //if(t->left!=NULL)
    //    printtree(t->left);
    //if(t->right!=NULL)
    //    printtree(t->right);
    //cout<<t->element<<" ";

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值