二叉查找树的性质是,对于树中的每一个结点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<<" ";
}